Skip to content

Instantly share code, notes, and snippets.

@Maxhodges
Created April 6, 2017 12:02
Show Gist options
  • Save Maxhodges/77856f5f53dff95cf0755a0984522df0 to your computer and use it in GitHub Desktop.
Save Maxhodges/77856f5f53dff95cf0755a0984522df0 to your computer and use it in GitHub Desktop.
public void PrintPDF(Stream PDFtoPrint, string jobName)
{
//Create PdfViewer object
PdfViewer viewer = new PdfViewer();
logger.Info("PrintPDF");
//Open input PDF file
viewer.BindPdf(PDFtoPrint);
// string pdffilename = @"c:\temp\blue.pdf";
// viewer.BindPdf(pdffilename);
//Set attributes for printing
viewer.AutoResize = true; //Print the file with adjusted size
viewer.AutoRotate = true; //Print the file with adjusted rotation
viewer.PrintPageDialog = false; //Do not produce the page number dialog when printing
viewer.PrinterJobName = jobName;
//Create objects for printer and page settings and PrintDocument
System.Drawing.Printing.PrinterSettings ps = new System.Drawing.Printing.PrinterSettings();
System.Drawing.Printing.PageSettings pgs = new System.Drawing.Printing.PageSettings();
System.Drawing.Printing.PrintDocument prtdoc = new System.Drawing.Printing.PrintDocument();
//Set XPS PDF printer name
prtdoc.PrinterSettings.PrinterName = roboAnna.Properties.Settings.Default.LaserPrinter;
ps.PrinterName = prtdoc.PrinterSettings.PrinterName;
//or set the PDF printer
//ps.PrinterName = "Adobe PDF";
//Set PageSize (if required)
pgs.PaperSize = new System.Drawing.Printing.PaperSize("A4", 827, 1169);
//Set PageMargins (if required)
pgs.Margins = new System.Drawing.Printing.Margins(0, 0, 0, 0);
ps.Duplex = System.Drawing.Printing.Duplex.Simplex;
//Print document using printer and page settings
viewer.PrintDocumentWithSettings(pgs, ps);
// clean up, Close the PDF file after priting
ps = null;
pgs = null;
viewer.Close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment