Print a PDF document

<< Click to Display Table of Contents >>

Navigation:  Gnostice Document Studio .NET > Going Deeper > Document Engines > Working with PDF > Processing >

Print a PDF document

In this section we present example code to print a PDF document. Before proceeding further please make sure that the prerequisite steps as listed in the Document Engines parent topic are followed.

In addition to the above prerequisite, the Gnostice.DocumentStudio.Printer NuGet package needs to added to the project.

 

Given below is the sample code.

 

static void Main(string[] args)
{
   using (var docPrinter = new DocumentPrinter())
   {
      // Print page selection 1 and 2 only
      docPrinter.PageSelection.SelectionType = Gnostice.Core.Printer.PageSelectionType.CustomRange;
      docPrinter.PageSelection.CustomRange = "1,3";
 
      // Auto rotate and center the page on paper
      docPrinter.AutoRotate = true;
      docPrinter.PagePositioning.Vertical = VPagePosition.Center;
      docPrinter.PagePositioning.Horizontal = HPagePosition.Center;
 
      // Scale the page to fit to paper size
      docPrinter.PageScaling = PageScalingOptions.Fit;
 
      // Set the margins
      docPrinter.PrintDocument.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(10, 10, 10, 10);
 
      // Set the printer name
      docPrinter.PrintDocument.PrinterSettings.PrinterName = GetPrinterName("Brother DCP-L2530DW");
 
      // Finally, print the document
      docPrinter.Print(@"invoice.pdf");
   }
}
 
static string GetPrinterName(string partialName)
{
   var installedPrinters = System.Drawing.Printing.PrinterSettings.InstalledPrinters;
   foreach (var printer in installedPrinters)
   {
      var printerName = printer as string;
      if (printerName.Contains(partialName))
      {
         return printerName;
      }
   }
   return installedPrinters[0];
}