Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created January 19, 2021 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/1950ab4331c2c33e324754772996eada to your computer and use it in GitHub Desktop.
Save aspose-com-gists/1950ab4331c2c33e324754772996eada to your computer and use it in GitHub Desktop.
Print PDF Files Programmatically using Java | PDF Printer API
// Create PdfViewer object
PdfViewer viewer = new PdfViewer();
// Open input PDF file
viewer.bindPdf(dataDir + "test.pdf");
// 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
PrintPageSettings pgs = new PrintPageSettings();
PdfPrinterSettings ps = new PdfPrinterSettings();
// Set printer name
ps.setPrinterName("Microsoft Print to PDF");
// Resultant Printout name
ps.setPrintFileName(dataDir + "ResultantPrintout.xps");
// Print the output to file
ps.setPrintToFile(true);
// Print the document with settings specified above
viewer.printDocumentWithSettings(pgs, ps);
// Check the print status
if (viewer.getPrintStatus() != null)
{
// An exception was thrown
Exception ex = (Exception)viewer.getPrintStatus();
if (ex != null)
{
// Get exception message
}
}
else
{
// No errors were found. Printing job has completed successfully
System.out.println("printing completed without any issue..");
}
// Initialize a list of String type
List<String> files = new ArrayList<>();
// Add multiple files to be printed
files.add(dataDir + "First.pdf");
files.add(dataDir + "Second.pdf");
// Process each file to print
for (String file : files)
{
//Create PdfViewer object
PdfViewer viewer = new PdfViewer();
//Open input PDF file
viewer.bindPdf(file);
//Print PDF document
viewer.printDocument();
//Close PDF file
viewer.close();
}
//Create PdfViewer object
PdfViewer viewer = new PdfViewer();
//Open input PDF file
viewer.bindPdf(dataDir + "Test.pdf");
//Print PDF document
viewer.printDocument();
//Close PDF file
viewer.close();
//Load secure PDF document while specifying User or Owner password
Document document = new Document(dataDir + "Password.pdf" , "userORowner");
//Create PdfViewer object
PdfViewer viewer = new PdfViewer();
//Open input PDF file
viewer.bindPdf(document);
//Print PDF document
viewer.printDocument();
//Close PDF file
viewer.close();
// Create PdfViewer object
PdfViewer viewer = new PdfViewer();
// Open input PDF file
viewer.bindPdf(dataDir + "test.pdf");
// 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
PrintPageSettings pgs = new PrintPageSettings();
PdfPrinterSettings ps = new PdfPrinterSettings();
// Set printer name
ps.setPrinterName("Microsoft Print to PDF");
// ps.setPrintRange(2); // 0 = all pages; 1 = selection; 2 = some pages
// ps.setFromPage(1);
// ps.setToPage(2);
ps.setPrintRange(1); // 0 = all pages; 1 = selection; 2 = some pages
ps.setSelectedPages(new int[]{1, 3, 5});
// Print document using printer and page settings
viewer.printDocumentWithSettings(pgs, ps);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment