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.