問題描述
如何在 C# 中使用 PrintDialog 打印文檔 (How to print a document using PrintDialog in C#)
這是我的示例代碼。但它打印一個空頁面
printDocument1.DocumentName = "C:\a.pbf";// PrintDocument printDocument1
printDialog1.Document = printDocument1;
printDialog1.AllowPrintToFile = true;
printDialog1.AllowSelection = true;
printDialog1.AllowSomePages = true;
printDialog1.PrintToFile = true;
if (printDialog1.ShowDialog() == DialogResult.OK)
printDocument1.Print();
這有什麼問題嗎?請幫幫我
參考解法
方法 1:
You need to handle the PrintPage
event to actually provide the contents; MSDN has a full example. The DocumentName
is purely something to show to the user ‑ it is not the path of an existing file to magically print.
For printing an existing PDF, maybe look at this question
方法 2:
do this :
public static void PrintToASpecificPrinter()
{
using (PrintDialog printDialog=new PrintDialog ())
{
printDialog.AllowSomePages = true;
printDialog.AllowSelection = true;
if (printDialog.ShowDialog() == DialogResult.OK)
{
var StartInfo = new ProcessStartInfo();
StartInfo.CreateNoWindow = true;
StartInfo.UseShellExecute = true;
StartInfo.Verb = "printTo";
StartInfo.Arguments = "\"" + printDialog.PrinterSettings.PrinterName + "\"";
StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
StartInfo.FileName = fileName;
Process.Start(StartInfo);
}
}
}
(by Thomas Anderson、Marc Gravell、bigtheo)