使用asp.net中显示的打印对话框打印各种pdf文件

时间:2015-03-15 19:11:00

标签: c# asp.net pdf printing

我在asp.net项目中创建pdf文件,该项目存储在文件夹中的服务器上。当用户想要打印此文件时,我需要用户指定网络上所有标签打印机中的哪台打印机,因为它也可能包含数字激光打印机。 我尝试过创建一个打印流程,但这会将pdf文件直接发送到默认打印机。有没有办法显示打印对话框,以便用户可以选择所需的打印机?

            printjob.StartInfo.FileName = pdfFileName;<br/>
            printjob.StartInfo.Verb = "Print";<br/>
            printjob.StartInfo.CreateNoWindow = false;<br/>
            printjob.StartInfo.WindowStyle =         System.Diagnostics.ProcessWindowStyle.Normal;
            PrinterSettings setting = new PrinterSettings();<br/>
            printjob.Start();

1 个答案:

答案 0 :(得分:1)

这可以通过Spire.pdf.dll引用来实现。 要安装此Open包管理器控制台并键入Install-Package Spire.pdf。这将安装spire.pdf。现在,以下代码将帮助您打印pdf文件。

        PdfDocument doc = new PdfDocument();
        doc.LoadFromFile("D:\\sample.pdf");

        //Use the default printer to print all the pages
        //doc.PrintDocument.Print();

        //Set the printer and select the pages you want to print

        PrintDialog dialogPrint = new PrintDialog();
        dialogPrint.AllowPrintToFile = true;
        dialogPrint.AllowSomePages = true;
        dialogPrint.PrinterSettings.MinimumPage = 1;
        dialogPrint.PrinterSettings.MaximumPage = doc.Pages.Count;
        dialogPrint.PrinterSettings.FromPage = 1;
        dialogPrint.PrinterSettings.ToPage = doc.Pages.Count;

        if (dialogPrint.ShowDialog() == DialogResult.OK)
        {
            doc.PrintFromPage = dialogPrint.PrinterSettings.FromPage;
            doc.PrintToPage = dialogPrint.PrinterSettings.ToPage;
            doc.PrinterName = dialogPrint.PrinterSettings.PrinterName;

            PrintDocument printDoc = doc.PrintDocument;
            dialogPrint.Document = printDoc;
            printDoc.Print();
        }