Skip to main content
Skip table of contents

pdfRender: BufferedImage as output format

The pdfRender add-on enables you to easily display (render) any PDF as an image. Before version 1.0.2 of the library you were able to convert PDF pages to JPG, PNG or other image file formats.
As of pdfRender 1.0.2 this has been extended to include BufferedImage as well. Meaning it is now possible to create an in memory image object when converting from PDF using pdfRender, which in some cases will be more efficient to work with.

In order to get a BufferedImage as output you will have to provide an implementation of the PdfToImageRenderer.BufferedImageReadyListener class. This class has the bufferedImageReady()method which should be overridden with logic to decide how and where the BufferedImage object should be stored. Take a look at the example below:

JAVA

JAVA
import com.itextpdf.kernel.utils.PageRange;
import com.itextpdf.licensekey.LicenseKey;
import com.itextpdf.pdfrender.PdfToImageRenderer;

import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static final String SRC = "src/main/resources/pdf.pdf";
    private static final String KEY = "src/main/resources/itextkey_pdfRender.xml";

    public static void main(String[] args) throws IOException {
        LicenseKey.loadLicenseFile(KEY);
        final List<BufferedImage> resultantImages = new ArrayList<>();
        PdfToImageRenderer.renderPdf(new FileInputStream(SRC), new PageRange("1"), new PdfToImageRenderer.BufferedImageReadyListener() {
            @Override
            public void bufferedImageReady(BufferedImage bufferedImage, int i) {
                // Here is where you provide logic on how to process the BufferedImage object
                // In this case we simply add it to an ArrayList for later use
                resultantImages.add(bufferedImage);
            }
        });
    }
}

We can also use this method if we want to edit the image before displaying it. For example; imagine you want to preview the first page of a PDF document, however you want to view it in black and white. By providing the right implementation to BufferedImageReadyListener#bufferedImageReady() you are able to alter the BufferedImage before displaying it (displaying it in our example below simply means adding it to a PDF file).

JAVA

JAVA
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.utils.PageRange;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.licensekey.LicenseKey;
import com.itextpdf.pdfrender.PdfToImageRenderer;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class Main {
    public static final String DEST = "results/result.pdf";
    public static final String SRC = "src/main/resources/pdf.pdf";
    private static final String KEY = "src/main/resources/itextkey_pdfRender.xml";

    static {
        new File(DEST).getParentFile().mkdir();
    }

    public static void main(String[] args) throws Exception {
        LicenseKey.loadLicenseFile(KEY);
        PdfDocument pdfDocument = new PdfDocument(new PdfWriter(DEST));
        Document document = new Document(pdfDocument);

        PdfToImageRenderer.renderPdf(new FileInputStream(SRC), new PageRange("1"), new PdfToImageRenderer.BufferedImageReadyListener() {
            @Override
            public void bufferedImageReady(BufferedImage bufferedImage, int i) {
                BufferedImage blackAndWhite = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
                Graphics2D graphics = blackAndWhite.createGraphics();
                graphics.drawImage(bufferedImage, 0, 0, null);

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                try {
                    ImageIO.write(blackAndWhite, "png", baos);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Image image = new Image(ImageDataFactory.create(baos.toByteArray()));
                document.add(image);
            }
        });

        pdfDocument.close();
    }
}




JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.