PdfViewer Class
Contents
[
Hide
]
The Java PdfViewerExamples class demonstrates the main viewer workflows available through the Facades API.
Decode all PDF pages
Use this workflow when every page of the source PDF should be rendered as an image.
Steps
- Create and configure a
PdfViewerinstance. - Bind the source PDF with
bindPdf. - Call
decodeAllPages()to render the document into aBufferedImagearray. - Save each decoded page to an output image file.
- Close the bound PDF file.
Java example
public static void decodeAllPages(Path inputFile, Path outputDir) throws Exception {
PdfViewer viewer = createViewer();
try {
viewer.bindPdf(inputFile.toString());
BufferedImage[] pages = viewer.decodeAllPages();
for (int index = 0; index < pages.length; index++) {
ImageIO.write(pages[index], "png", outputDir.resolve("decode_all_pages_" + (index + 1) + ".png").toFile());
}
} finally {
viewer.closePdfFile();
}
}
Decode a specific PDF page
Use this workflow when only one page needs to be rendered to an image.
Steps
- Create and configure a
PdfViewerinstance. - Bind the source PDF.
- Call
decodePage()for the page you want to render. - Save the decoded page to an output image file.
- Close the viewer.
Java example
public static void decodeSpecificPage(Path inputFile, Path outputFile) throws Exception {
PdfViewer viewer = createViewer();
try {
viewer.bindPdf(inputFile.toString());
ImageIO.write(viewer.decodePage(1), "png", outputFile.toFile());
} finally {
viewer.close();
}
}
Inspect PDF metadata
Use this workflow when you need viewer-related document information before rendering or printing.
Steps
- Create and configure a
PdfViewerinstance. - Bind the source PDF.
- Read the page count, coordinate type, and rendering resolution.
- Use or print the retrieved values.
- Close the bound PDF file.
Java example
public static void inspectPdfMetadata(Path inputFile) {
PdfViewer viewer = createViewer();
try {
viewer.bindPdf(inputFile.toString());
System.out.println("Page count: " + viewer.getPageCount());
System.out.println("Coordinate type: " + viewer.getCoordinateType());
System.out.println("Resolution: " + viewer.getResolution());
} finally {
viewer.closePdfFile();
}
}
Inspect bound viewer settings
Use this workflow when you need to confirm or adjust viewer behavior after binding the PDF.
Steps
- Create and configure a
PdfViewerinstance. - Bind the source PDF.
- Set viewer options such as auto-resize, auto-rotate, and print dialog visibility.
- Read the active viewer settings and page count.
- Close the viewer.
Java example
public static void inspectBoundViewerSettings(Path inputFile) {
PdfViewer viewer = createViewer();
try {
viewer.bindPdf(inputFile.toString());
viewer.setAutoResize(true);
viewer.setAutoRotate(true);
viewer.setPrintPageDialog(false);
System.out.println("Page count: " + viewer.getPageCount());
System.out.println("Print as image: " + viewer.getPrintAsImage());
System.out.println("Auto resize: " + viewer.getAutoResize());
System.out.println("Auto rotate: " + viewer.getAutoRotate());
} finally {
viewer.close();
}
}