0

Hello everybody this is my code that I try get a file with Open File Dialog And print the file But its printing an Empty page :( :(

please help me :)

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

        }
    }
Muhammadreza
  • 37
  • 1
  • 1
  • 4
  • 2
    Related : http://stackoverflow.com/questions/5566186/print-pdf-in-c-sharp You cannot directly print PDF using PrintDocument. Use PDF Viewer or third party library. –  Aug 06 '16 at 11:33

2 Answers2

1

The easiest way is to use external library, with the following msdn example you can print the PDF files with the default printer or any other network connected printer as well as select the pages you want to print:

        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(); 
        } 
Dheeraj Malik
  • 703
  • 1
  • 4
  • 8
-1

PrintDocument won't print a PDF by itself. Try this other SO post which explains how to use whatever application is associated with PDF files and the 'print' verb.

Community
  • 1
  • 1
leetibbett
  • 843
  • 4
  • 16