Print PDF Files in Java
JPedal is a pure Java PDF library that gives Java developers the ability to print PDF files programmatically, from the command line or embedded directly in a Java application. For more advanced control over paper size, page scaling, and printer selection, see the 5 step printing tutorial.
Requirements
| Requirement | Value |
|---|---|
| Minimum Java version | Java 17 |
| Third-party dependencies | None |
Print a PDF file from the command line or another language
# Print page 1 of a document
java -jar jpedal.jar --print "inputFile.pdf" PRINTER_NAME 1
# Print the whole document
java -jar jpedal.jar --print "inputFile.pdf" PRINTER_NAME
The command line interface can be invoked from any language that supports child processes, including Python, Node.js, C#, and shell scripts.
Print a PDF file in Java
PrintPdfPages print = new PrintPdfPages("inputFile.pdf");
if (print.openPDFFile()) {
print.printAllPages("Printer Name");
// or print a single page:
// print.printPage("Printer Name", pageNumber);
}
View the full Java source code for this example, including additional printing features.
Control paper size, scaling and printer selection
JPedal supports setting the paper size, page scaling and target printer programmatically. See the 5 step printing tutorial for the full API.
Server-side and headless printing
JPedal supports silent server-side printing without a display. Configure the JVM for headless mode where required.
Frequently Asked Questions
How do I print a PDF in Java?
Use the PrintPdfPages class to open the file with openPDFFile(), then call printAllPages() with the printer name.
How do I find the printer name to pass to JPedal?
PrintServiceLookup.lookupPrintServices() from the Java Print Service API returns all printers available to the JVM. Pass the printer’s getName() value to JPedal’s print methods.
Can JPedal print a single page rather than the whole document?
Yes. Use print.printPage("Printer Name", pageNumber) where page numbering starts at 1.
Can JPedal print password-protected PDF files?
Yes. Set the password before opening the file. See the 5 step printing tutorial for the encrypted file API.
Can I print a PDF without writing Java code?
Yes. JPedal’s command line interface supports printing directly using java -jar jpedal.jar --print "inputFile.pdf" PRINTER_NAME. See the command line section above for syntax and language examples.
Does JPedal require any third-party libraries for printing?
No. JPedal has no required third-party Java dependencies, no native binaries and no external print drivers required.
What Java version is required?
JPedal requires Java 17 as a minimum. Details at Which Java versions does JPedal support?