我在打印到网络打印机时遇到问题。下面的代码在我自己的笔记本电脑和 Visual Studio 上运行良好,但是当站点在 IIS 8 网站(在 Windows Server 2008 R2 上)上运行时,单击打印机按钮没有任何作用.. 页面刚刚开始加载(永远加载并且超时),但没有任何反应,也没有作业添加到打印机的队列中。
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int i = 0;
foreach (string s in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
{
Button b = new Button();
b.ID = "Print_" + i;
b.Text = s.ToString();
b.Click += b_Click;
test.Controls.Add(b);
i++;
}
}
void b_Click(object sender, EventArgs e)
{
string printer = ((Button)sender).Text;
PrintDocument p = new PrintDocument();
p.PrinterSettings.PrinterName = printer;
p.PrintPage += p_PrintPage;
p.Print();
}
void p_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString("testpage", new System.Drawing.Font("Arial", 12), Brushes.Black, new PointF(200, 200));
}
}