Print PDFs - Java Sample Code

Sample code for using Apryse SDK to print a PDF file using the currently selected default printer; provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, Go and VB. It is possible to use this printing functionality in both client and server applications without dependence on any third party components. Learn more about our Server SDK.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import java.awt.Graphics;
7import java.awt.image.BufferedImage;
8import java.awt.print.PageFormat;
9import java.awt.print.Printable;
10import java.awt.print.PrinterException;
11import java.awt.print.PrinterJob;
12
13import javax.print.attribute.*;
14import javax.print.attribute.standard.MediaPrintableArea;
15
16
17import com.pdftron.common.PDFNetException;
18import com.pdftron.pdf.*;
19
20/// The following sample illustrates how to print PDF document using currently selected
21/// default printer.
22///
23/// The first example uses the new PDF::Print::StartPrintJob function to send a rasterization
24/// of the document with optimal compression to the printer. If the OS is Windows 7, then the
25/// XPS print path will be used to preserve vector quality. For earlier Windows versions
26/// the print path will be used. On other operating systems this will be a no-op
27///
28/// The second example uses PDFDraw send unoptimized rasterized data via awt.print API.
29///
30/// If you would like to rasterize page at high resolutions (e.g. more than 600 DPI), you
31/// should use PDFRasterizer or PDFNet vector output instead of PDFDraw.
32
33public class PDFPrintTest implements Printable {
34 PDFDoc doc;
35 PDFDraw draw;
36 BufferedImage image = null;
37
38 PDFPrintTest() {
39 try {
40 PDFNet.initialize(PDFTronLicense.Key());
41 doc = new PDFDoc("../../TestFiles/tiger.pdf");
42 doc.initSecurityHandler();
43
44 //////////////////////////////////////////////////////////////////////////
45 // Example 1: use the PDF.Print.startPrintJob interface
46 // This is silent (no progress dialog) and blocks until print job is at spooler
47 // The rasterized print job is compressed before sending to printer
48 System.out.println("Printing the input file using PDF.Print.StartPrintJob...");
49
50 // Print.startPrintJob can determine the default printer name for you, just pass an empty printer name
51
52 // Setup printing options:
53 PrinterMode printerMode = new PrinterMode();
54 printerMode.setCollation(true);
55 printerMode.setCopyCount(1);
56 printerMode.setDPI(300); // regardless of ordering, an explicit DPI setting overrides the OutputQuality setting
57 printerMode.setDuplexing(PrinterMode.e_Duplex_Auto);
58 printerMode.setOutputColor(PrinterMode.e_OutputColor_Grayscale);
59 printerMode.setOutputQuality(PrinterMode.e_OutputQuality_Medium);
60
61 // printerMode.setPaperSize(PrinterMode.e_6_3_Quarters_Envelope);
62
63 PageSet pagesToPrint = new PageSet(1, doc.getPageCount(), PageSet.e_all);
64
65 // Print the document on the default printer, name the print job the name of the
66 // file, print to the printer not a file, and use printer options:
67 Print.startPrintJob(doc, "", "tiger.pdf", "", pagesToPrint, printerMode, null);
68 } catch (PDFNetException e) {
69 e.printStackTrace();
70 }
71
72
73 //////////////////////////////////////////////////////////////////////////
74 // Example 2: Use Java.awt.print and PDFDraw rasterizer.
75 System.out.println("Printing the input file using Java.awt.print API...");
76 try {
77 draw = new PDFDraw();
78 draw.setDPI(200);
79
80 PrinterJob job = PrinterJob.getPrinterJob();
81
82 PageFormat pf = job.defaultPage();
83
84 HashPrintRequestAttributeSet psettings = new HashPrintRequestAttributeSet();
85 psettings.add(new MediaPrintableArea(0, 0,
86 (int) pf.getWidth(), (int) pf.getHeight(), MediaPrintableArea.MM));
87
88 job.setPrintable(this);
89 boolean ok = job.printDialog();
90 if (ok) {
91 try {
92 job.print(psettings);
93 } catch (PrinterException ex) {
94 //The Print did not complete successfully
95 ex.printStackTrace();
96 }
97 }
98 doc.close();
99 } catch (PDFNetException e) {
100 e.printStackTrace();
101 }
102 PDFNet.terminate();
103 }
104
105 public static void main(String[] args) {
106 new PDFPrintTest();
107 }
108
109 public int print(Graphics g, PageFormat format, int page_num) throws PrinterException {
110 try {
111 if (page_num < 0 || page_num >= doc.getPageCount())
112 return Printable.NO_SUCH_PAGE;
113
114 draw.drawInRect(g, doc.getPage(page_num + 1), (int) (0), (int) (0),
115 (int) (format.getWidth()), (int) (format.getHeight()));
116
117 return Printable.PAGE_EXISTS;
118 } catch (PDFNetException e) {
119 e.printStackTrace();
120 }
121 return Printable.NO_SUCH_PAGE;
122 }
123}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales