You can read all details at: Print PDF Files Programmatically using Java
Created
January 19, 2021 19:58
-
-
Save aspose-com-gists/1950ab4331c2c33e324754772996eada to your computer and use it in GitHub Desktop.
Print PDF Files Programmatically using Java | PDF Printer API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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.."); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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