Skip to content

Quick Start

Patrick Corless edited this page Nov 3, 2026 · 2 revisions

Add icepdf to your build, then pick what you need: render pages to images, pull out text, or show documents in a viewer. Each snippet below compiles against 8.0.0 as written.

On this page: Add the dependency · Render a page to PNG · Extract text · Show a viewer (JavaFX) · Show a viewer (Swing) · Next steps

Add the dependency

Maven:

<dependency>
    <groupId>com.github.pcorless.icepdf</groupId>
    <artifactId>icepdf-core</artifactId>
    <version>8.0.0</version>
</dependency>

Gradle:

implementation 'com.github.pcorless.icepdf:icepdf-core:8.0.0'

Core needs Java 11 or later. For a viewer add icepdf-viewer-fx (JavaFX, Java 17+) or icepdf-viewer (Swing); see Dependencies.

Render a page to PNG

getPageImage renders one page; zoom 1 is 72 dpi, so 2 gives 144 dpi. Always dispose() the document when you're done with it.

import org.icepdf.core.pobjects.Document;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.util.GraphicsRenderingHints;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class RenderPage {
    public static void main(String[] args) throws Exception {
        Document document = new Document();
        try {
            document.setFile("input.pdf");
            float zoom = 2f;   // 2x = 144 dpi
            BufferedImage image = (BufferedImage) document.getPageImage(0,
                    GraphicsRenderingHints.PRINT, Page.BOUNDARY_CROPBOX, 0f, zoom);
            ImageIO.write(image, "png", new File("page-1.png"));
            image.flush();
        } finally {
            document.dispose();
        }
    }
}

To render many pages, render them in parallel from a thread pool: a Document is safe to share between threads. The PNG capture example does this.

Extract text

getPageText returns the page's text, organised into lines and words.

import org.icepdf.core.pobjects.Document;

public class ExtractText {
    public static void main(String[] args) throws Exception {
        Document document = new Document();
        try {
            document.setFile("input.pdf");
            for (int i = 0; i < document.getNumberOfPages(); i++) {
                System.out.println(document.getPageText(i));
            }
        } finally {
            document.dispose();
        }
    }
}

For words with their positions, walk the PageText lines and words. For search, see the search examples.

Show a viewer (JavaFX)

PdfViewer is the complete viewer: tabs, side panels, annotations, forms, search, printing and signing. Add icepdf-viewer-fx and org.openjfx:javafx-controls (22 or later).

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import org.icepdf.fx.ri.PdfViewer;

import java.nio.file.Path;

public class FxViewer extends Application {
    @Override
    public void start(Stage stage) {
        PdfViewer viewer = PdfViewer.create();
        stage.setScene(new Scene(viewer, 1200, 900));
        stage.show();
        viewer.open(Path.of("input.pdf"));
    }

    public static void main(String[] args) {
        launch(args);
    }
}

To build your own chrome around just the page view, see Embedding the Viewer.

Show a viewer (Swing)

Add icepdf-viewer. SwingViewBuilder builds the full viewer panel, and SwingController drives it.

import org.icepdf.ri.common.SwingController;
import org.icepdf.ri.common.SwingViewBuilder;

import javax.swing.*;

public class SwingViewer {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            SwingController controller = new SwingController();
            JPanel viewer = new SwingViewBuilder(controller).buildViewerPanel();

            JFrame frame = new JFrame("My application");
            frame.getContentPane().add(viewer);
            frame.setSize(1000, 800);
            frame.setVisible(true);
            controller.openDocument("input.pdf");
        });
    }
}

Next steps

Clone this wiki locally