【问题标题】:Print a file(PDf) by using PrintDialog in C#在 C# 中使用 PrintDialog 打印文件(PDf)
【发布时间】:2016-08-06 11:28:51
【问题描述】:

大家好,这是我的代码,我尝试使用打开文件对话框获取文件并打印文件 但它打印一个空页面:( :(

请帮帮我:)

   private void tsmprint_Click(object sender, EventArgs e)
    {
        try
        {
            if (openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                PrintDocument Pd = new PrintDocument();

                Pd.DocumentName = openFileDialog1.FileName;

                 printDialog1.Document = Pd;
                if (printDialog1.ShowDialog()==DialogResult.OK)
                {
                    Pd.Print();
                }

            }
        }
        catch (Exception)
        {

        }
    }

【问题讨论】:

标签: c# openfiledialog printdialog


【解决方案1】:

最简单的方法是使用外部库,使用以下msdnexample,您可以使用默认打印机或任何其他网络连接的打印机打印PDF文件以及选择要打印的页面:

        PdfDocument doc = new PdfDocument(); 
        doc.LoadFromFile(FilePathandFileName); 

        //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) 
        { 
            //Set the pagenumber which you choose as the start page to print 
            doc.PrintFromPage = dialogPrint.PrinterSettings.FromPage; 
            //Set the pagenumber which you choose as the final page to print 
            doc.PrintToPage = dialogPrint.PrinterSettings.ToPage; 
            //Set the name of the printer which is to print the PDF 
            doc.PrinterName = dialogPrint.PrinterSettings.PrinterName; 

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

【讨论】:

  • 这仅适用于打印 pdf 文件吗?有没有更通用的方法?
  • 您使用的是哪个外部库?链接已经失效了...
  • 这个“PDF 文档”类来自哪里?
【解决方案2】:

PrintDocument 不会自行打印 PDF。试试this other SO post,它解释了如何使用与 PDF 文件和“打印”动词相关的任何应用程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多