Complete Java examples for common PDF to Image tasks. See the Java SDK guide for installation and result handling.
You can run the examples below with the displayed API credentials. Update the input URLs and filenames as needed. Each Java example can be copied and run independently.
Choose file, in-memory, or output-stream handling for the ZIP archive. Stream examples use a binary file stream as their destination.
These basic examples produce a ZIP archive containing PNG images of the PDF pages. The archive variable holds ZIP bytes in the memory variants.
Upload an existing invoice.pdf file.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.PdfToImageClient client = new Pdfcrowd.PdfToImageClient("demo", "demo"); client.setForceZip(true); client.setOutputFormat("png"); client.convertFileToFile("invoice.pdf", "invoice.zip"); } }
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.PdfToImageClient client = new Pdfcrowd.PdfToImageClient("demo", "demo"); client.setForceZip(true); client.setOutputFormat("png"); byte[] archive = client.convertFile("invoice.pdf"); System.out.println(archive.length); } }
import com.pdfcrowd.Pdfcrowd; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.PdfToImageClient client = new Pdfcrowd.PdfToImageClient("demo", "demo"); try (OutputStream output_stream = Files.newOutputStream(Path.of("invoice.zip"))) { client.setForceZip(true); client.setOutputFormat("png"); client.convertFileToStream("invoice.pdf", output_stream); } } }
Fetch a PDF from its URL.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.PdfToImageClient client = new Pdfcrowd.PdfToImageClient("demo", "demo"); client.setForceZip(true); client.setOutputFormat("png"); client.convertUrlToFile( "https://your-server.com/invoice.pdf", "invoice.zip" ); } }
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.PdfToImageClient client = new Pdfcrowd.PdfToImageClient("demo", "demo"); client.setForceZip(true); client.setOutputFormat("png"); byte[] archive = client.convertUrl( "https://your-server.com/invoice.pdf" ); System.out.println(archive.length); } }
import com.pdfcrowd.Pdfcrowd; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.PdfToImageClient client = new Pdfcrowd.PdfToImageClient("demo", "demo"); try (OutputStream output_stream = Files.newOutputStream(Path.of("invoice.zip"))) { client.setForceZip(true); client.setOutputFormat("png"); client.convertUrlToStream( "https://your-server.com/invoice.pdf", output_stream ); } } }
Read invoice.pdf as binary data. In an application, pass the bytes you already hold in memory instead.
import com.pdfcrowd.Pdfcrowd; import java.nio.file.Files; import java.nio.file.Path; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.PdfToImageClient client = new Pdfcrowd.PdfToImageClient("demo", "demo"); byte[] pdf_data_1 = Files.readAllBytes(Path.of("invoice.pdf")); client.setForceZip(true); client.setOutputFormat("png"); client.convertRawDataToFile(pdf_data_1, "invoice.zip"); } }
import com.pdfcrowd.Pdfcrowd; import java.nio.file.Files; import java.nio.file.Path; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.PdfToImageClient client = new Pdfcrowd.PdfToImageClient("demo", "demo"); byte[] pdf_data_1 = Files.readAllBytes(Path.of("invoice.pdf")); client.setForceZip(true); client.setOutputFormat("png"); byte[] archive = client.convertRawData(pdf_data_1); System.out.println(archive.length); } }
import com.pdfcrowd.Pdfcrowd; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.PdfToImageClient client = new Pdfcrowd.PdfToImageClient("demo", "demo"); try (OutputStream output_stream = Files.newOutputStream(Path.of("invoice.zip"))) { byte[] pdf_data_1 = Files.readAllBytes(Path.of("invoice.pdf")); client.setForceZip(true); client.setOutputFormat("png"); client.convertRawDataToStream(pdf_data_1, output_stream); } } }
Open invoice.pdf as a binary input stream and save the result to invoice.zip.
import com.pdfcrowd.Pdfcrowd; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.PdfToImageClient client = new Pdfcrowd.PdfToImageClient("demo", "demo"); try (InputStream input_stream = Files.newInputStream(Path.of("invoice.pdf"))) { client.setForceZip(true); client.setOutputFormat("png"); client.convertStreamToFile(input_stream, "invoice.zip"); } } }
These examples use report.pdf, a three-page document with text, a chart, and vector graphics. The CropBox variant uses cropbox.pdf, a page with a predefined crop boundary.
Render page 1 of report.pdf directly as a PNG. Use 72 DPI for a compact preview or 300 DPI for more detail at higher pixel dimensions and file size. Selecting one page produces an image file without ZIP packaging.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.PdfToImageClient client = new Pdfcrowd.PdfToImageClient("demo", "demo"); client.setDpi(72); client.setOutputFormat("png"); client.setPrintPageRange("1"); client.convertFileToFile("report.pdf", "preview.png"); } }
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.PdfToImageClient client = new Pdfcrowd.PdfToImageClient("demo", "demo"); client.setDpi(300); client.setOutputFormat("png"); client.setPrintPageRange("1"); client.convertFileToFile("report.pdf", "page-300dpi.png"); } }
Render pages 1 and 3 of report.pdf as PNG images in selected-pages.zip. Force ZIP packaging so the result stays an archive even if you later select just one page.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.PdfToImageClient client = new Pdfcrowd.PdfToImageClient("demo", "demo"); client.setForceZip(true); client.setOutputFormat("png"); client.setPrintPageRange("1,3"); client.convertFileToFile("report.pdf", "selected-pages.zip"); } }
Convert page 1 of report.pdf into page.jpg for applications that need JPEG input. JPEG is lossy and is not always smaller than PNG, especially for pages dominated by text and flat graphics.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.PdfToImageClient client = new Pdfcrowd.PdfToImageClient("demo", "demo"); client.setOutputFormat("jpg"); client.setPrintPageRange("1"); client.convertFileToFile("report.pdf", "page.jpg"); } }
Extract the chart from page 1 of report.pdf into chart.png, or honor the predefined CropBox in cropbox.pdf to remove its production margins.
The chart example fixes 144 DPI and uses pixel coordinates from the rendered page's top-left corner: X = 96, Y = 370, width = 1024, height = 384. Adjust the coordinates when changing the page layout or DPI. The CropBox variant uses the boundary already stored in the PDF.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.PdfToImageClient client = new Pdfcrowd.PdfToImageClient("demo", "demo"); client.setCropAreaHeight(384); client.setCropAreaWidth(1024); client.setCropAreaX(96); client.setCropAreaY(370); client.setDpi(144); client.setOutputFormat("png"); client.setPrintPageRange("1"); client.convertFileToFile("report.pdf", "chart.png"); } }
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.PdfToImageClient client = new Pdfcrowd.PdfToImageClient("demo", "demo"); client.setOutputFormat("png"); client.setPrintPageRange("1"); client.setUseCropbox(true); client.convertFileToFile("cropbox.pdf", "cropped-page.png"); } }
Render page 1 of report.pdf into grayscale.png, replacing colors with shades of gray while preserving the page dimensions.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.PdfToImageClient client = new Pdfcrowd.PdfToImageClient("demo", "demo"); client.setOutputFormat("png"); client.setPrintPageRange("1"); client.setUseGrayscale(true); client.convertFileToFile("report.pdf", "grayscale.png"); } }