Print PDF file using PrintDocument

3

I'm developing an application, Console Application in Visual Studio 2015, that its function is to print documents.

I can print TXT and DOC without problems using class PrintDocument but I can not print PDF files. Is there any way to print a PDF with PrintDocument ?

     public void Imprimir(string caminhoImpressora)
    {
        streamToPrint = new StreamReader(@"C:\Users13\Desktop\teste.txt");


        printFont = new Font("Arial", 10);
        var documento = new PrintDocument();
        documento.PrinterSettings.PrintFileName = LocalizarImpressora(caminhoImpressora).ToString();
        documento.PrintPage += new PrintPageEventHandler(PrintPage);
        documento.Print();

    }

This is the method that defines the printer path that will be used and creates the StreamReader object from the txt file. And the code below is the event that reads the lines of the file and formats the text:

   private void PrintPage(object sender, PrintPageEventArgs ev)
    {
        float linesPerPage = 0;
        float yPos = 0;
        int count = 0;
        float leftMargin = ev.MarginBounds.Left;
        float topMargin = ev.MarginBounds.Top;
        string line = null;

        // Calculate the number of lines per page.
        linesPerPage = ev.MarginBounds.Height /
           printFont.GetHeight(ev.Graphics);

        // Print each line of the file.
        while (count < linesPerPage &&
           ((line = streamToPrint.ReadLine()) != null))
        {
            yPos = topMargin + (count *
               printFont.GetHeight(ev.Graphics));
            ev.Graphics.DrawString(line, printFont, Brushes.Black,
               leftMargin, yPos, new StringFormat());
            count++;
        }

        // If more lines exist, print another page.
        if (line != null)
            ev.HasMorePages = true;
        else
            ev.HasMorePages = false;
    }

However, when trying to use the same methods to print a pdf, I can even print, but they are illegible code, as if it could not read the contents of the pdf itself.

    
asked by anonymous 18.10.2016 / 19:01

1 answer

3

You will not be able to print PDFs using this routine. In it you are reading, line by line, a text file and "drawing", in hand, using DrawString() line content in a graphical context provided by PrintDocument . A PDF document is not a collection of lines in text, but a complex document, binary (not textual), and full of control constructs. It is, practically, a program in itself, that defines the diagram of what will be extracted on the screen.

So, even if your PDF is very simple, even if it is not completely standardized, it will not be easy to extract the text from within it.

Ideally, you should use some PDF viewer already installed to open the document, and the user then prints it, or displays it on the screen, as he sees fit. You can also request direct printing of the document, which may be more interesting in your case. The code below does this quite simply:

private static void ImprimeDocumento(string caminho) {
    Process process = new Process {
        StartInfo = new ProcessStartInfo {
            CreateNoWindow = true,
            Verb = "print",
            FileName = caminho,
        },
    };
    process.Start();
}

However, in newer versions of Windows, such as 8 or 10, this method no longer works, so we would have to change the verb to "open":

private static void AbreVisualizadorPadrao(string caminho) {
    Process process = new Process {
        StartInfo = new ProcessStartInfo {
            Verb = "open",
            FileName = caminho,
        },
    };
    process.Start();
}

This will open the standard PDF windows reader and allow it to be printed there.

If you want to generate PDF manually and then print (using the above method), you will need a PDF generation library. I strongly suggest the open source library iTextSharp

If you really need to view the PDF in your application and print straight from it, the way it will be to appeal to some ready library (most are not cheap):

18.10.2016 / 21:46