In order to print PDF, we’ll use Aspose.PDF for Java API which is a feature-rich, powerful and easy to use conversion API for Java platform. You can download its latest version directly from Maven and install it within your Maven-based project by adding the following configurations to the pom.xml.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java AP</name>
<url>https://releases.aspose.com/java/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>
Printing PDF document via Java
You need Aspose.PDF for Java to try the code in your environment.
- Load the PDF with an instance of Document.
- Get DocumentInfo using Document.Info property.
- Access & display different Document.Info properties.
Print PDF - Java
//Create PdfViewer object
PdfViewer viewer = new PdfViewer();
//Open input PDF file
viewer.bindPdf(DATA_DIR.resolve("input.pdf").toString());
//Set attributes for printing
viewer.setAutoResize(true); // Print the file with adjusted size
viewer.setAutoRotate (true); // Print the file with adjusted rotation
viewer.setPrintPageDialog(false); // Do not produce the page number dialog when printing
// Create objects for printer and page settings and PrintDocument
PdfPrinterSettings printerSettings = new PdfPrinterSettings();
PrintPageSettings pageSettings = new PrintPageSettings();
// Set printer name
printerSettings.setPrinterName("Microsoft Print to PDF");
// Set PageSize (if required)
pageSettings.setPaperSize(PrintPaperSizes.A4);
// Set PageMargins (if required)
pageSettings.setMargins(new PrinterMargins(0, 0, 0, 0));
// Print document using printer and page settings
viewer.printDocumentWithSettings(pageSettings, printerSettings);
// Close the PDF file after printing
viewer.close();