使用C#中的PrintDialog打印文件(PDf)



大家好,这是我的代码,我试图通过打开文件对话框获取文件并打印文件但是它打印了一个空页面:

请帮帮我:(

   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)
        {
        }
    }

最简单的方法是使用外部库,使用以下msdn示例,您可以使用默认打印机或任何其他网络连接的打印机打印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(); 
        } 

PrintDocument本身不会打印PDF。试试另一篇SO文章,它解释了如何使用与PDF文件和"打印"动词相关的任何应用程序。

相关内容

  • 没有找到相关文章

最新更新