如何在C#中使用PrintDialog打印文档

时间:2010-11-23 07:05:49

标签: c# printing

这是我的示例代码。但它打印一个空页

            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();

这有什么不对吗?请帮帮我

2 个答案:

答案 0 :(得分:6)

您需要处理PrintPage事件以实际提供内容; MSDN有一个full exampleDocumentName纯粹是向用户显示的内容 - 现有文件的神奇打印路径。

要打印现有PDF,请查看this question

答案 1 :(得分:0)

这样做:

public static void PrintToASpecificPirnter()
        {     
                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);
                }
                    
                }
                

        }