In different companies and organizations, printing PDF documents is usually a key function, which can be configured using Aspose.PDF for Java API in the environment. In this article, different PDF printing scenarios that are possible in Java applications will be described.
- Print PDF files using Java
- Print multiple PDF documents using Java
- Print a specific page or range of pages using Java
- Print secure PDF using Java
- Check the status of the print job when printing PDF using Java
Print PDF files using Java
Follow the steps below to easily print PDF files using Java code: (qun: 761297826)
- Create a PdfViewer object
- Load input PDF file
- print PDF file
The code snippet below shows how to programmatically print a PDF file using Java:
//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();
Print multiple PDF documents using Java
Using the Java language, printing multiple files or batch printing PDF files is a simple process. You can print many PDF files by following the steps below:
- initialize a list of string types
- Add multiple PDF files to print
- print PDF file
The following code shows how to print multiple PDF files using Java language:
// Initialize a list of String type Listfiles = 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(); }
Print a specific page or range of pages using Java
The API gives you a lot of control when printing PDF files. You can use different fields and methods when printing the file according to your needs. For example, if you don't want to print all pages of a PDF file, but only a few pages. It can be a contiguous range of pages, such as pages 3 to 7, or random pages, such as pages 1, 5, and 6. Both cases can be covered with the following steps:
- Initialize the PdfViewer object
- Set print properties
- Create printer and page setup objects
- Specifies whether to print all pages, specific pages, or a contiguous range of pages
- Print PDF documents using printer and page setup
The following code shows how to print a specific page or range of pages in a PDF document using Java:
// 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);
Print secure PDF using Java
Sometimes PDF files are password protected or encrypted to ensure authorized access to the data. Secure or encrypted PDF files can be easily accessed and printed using Java code and print files on demand. The following steps need to be followed:
- Load password-protected PDF input files with a password
- Initialize the PdfViewer object
- print PDF file
The following code is based on the following steps, which illustrate how to print a protected PDF file using Java code:
//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();
Check the status of the print job when printing PDF using Java
After sending the file to the printer's print queue, you can follow the status of the print job. This allows your application to determine whether printing was successful. PdfViewer class exposes getPrintStatus method which helps you to check the status of your print job. For example, when printing a PDF to XPS format, you can get the status as follows:
- Load input PDF file
- Set print properties
- Create printer and page setup objects
- set printer name
- print the output to a file
- Check printing status
The following code shows how to check the status of a print job during PDF printing using Java code:
// 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.."); }