I am trying to print pdf files using C# code. When I run the code, I get print dialog that says "printing" but no usable pdf or xps output. I am doing this from a console application not gui. Here is my current code:
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
public class PrintFile {
public static void printFilePath(String filePath, String printer) {
try {
var streamToPrint = new StreamReader(filePath);
try {
PrintDocument pd = new PrintDocument();
pd.PrinterSettings.PrinterName = printer;
// set landscape to false
pd.DefaultPageSettings.Landscape = false;
// set margins to zero
Margins margins = new Margins(0,0,0,0);
pd.DefaultPageSettings.Margins = margins;
// print
pd.Print();
} catch (Exception ex) {
Console.WriteLine(ex);
}
finally {
Console.WriteLine("closing");
streamToPrint.Close();
}
} catch (Exception ex) {
Console.WriteLine(ex);
}
}
public static void Main(string[] args) {
String filePath = "C:\\Projects\\C\\girl.pdf";
String printer = "Microsoft Print to PDF";
printFilePath(filePath, printer);
}
}
Any help is appreciated. I would also like to set number of copies, paper size (A4,e.t.c) and black and white/color also. Thanks.
UPDATE:
Based on comments below, I updated my code. Still cannot get it to print the exact document but at least it seems to print something. Would really appreciate any further constructive feedback
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
public class PrintFile {
private static Font printFont;
private static StreamReader streamToPrint;
// The PrintPage event is raised for each page to be printed.
private static void pd_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;
}
public static void printFilePath(String filePath, String printer) {
try {
streamToPrint = new StreamReader(filePath);
try {
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrinterSettings.PrinterName = printer;
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
// set landscape to false
pd.DefaultPageSettings.Landscape = false;
// set margins to zero
Margins margins = new Margins(0,0,0,0);
pd.DefaultPageSettings.Margins = margins;
// print
pd.Print();
} catch (Exception ex) {
Console.WriteLine(ex);
}
finally {
Console.WriteLine("closing");
streamToPrint.Close();
}
} catch (Exception ex) {
Console.WriteLine(ex);
}
}
public static void Main(string[] args) {
String filePath = "C:\\Projects\\C\\nnkl.pdf";
String printer = "Microsoft Print to PDF";
printFilePath(filePath, printer);
}
}