Java: Generating PDF and Previewing it as an Image – iText and PDF Renderer

Lucas Jellema 6
0 0
Read Time:2 Minute, 56 Second

Inspired by a blog article by Edwin Biemond, I decided to try it out myself: previewing an PDF document in an image. However, Edwin used jPedal (, a commercial product. It is not extremely expensive – but not freely available., So I decided to look a little further for a truly open source and free product. I found PDF Renderer, published by Sun Microsystems: .It could easily do for me what I was looking for.

As an additional step, I also used iText – – an open source project that support programmatic generation of PDF document from Java.

Within half an hour, I had a program running that generated the following preview of the first page of a dynamically produced PDF document:


The steps for achieving this are amazingly simple:


1. Download iText

Go to and download iText-2.1.4.jar.

2. Download PDF Renderer

Go to and download PDFRenderer.jar

3. Create Project

In your favorite IDE, create an Application/Workspace/Project – whatever you need to get going on an application. Add the two JAR-files you just downloaded as libraries.

4. Create Class

Create a class that creates a PDF document on the fly and then has its first page turned into an image that can be previewed, for example in a Swing Panel:

package pdfimagegenerator;

import com.lowagie.text.Document;

import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;

import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;

import java.awt.Image;
import java.awt.Rectangle;



import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import javax.swing.*;

public class PdfToImage {
    public PdfToImage() {

    private static ByteArrayOutputStream createPDF() throws DocumentException,
                                                            IOException {
        Document doc = new Document();
        ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
        PdfWriter docWriter = null;
        docWriter = PdfWriter.getInstance(doc, baosPDF);;
        URL imageUrl = new URL("");
        com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(imageUrl);

        doc.add(new Paragraph("This special PDF document was created on " +
                              new java.util.Date()));

        return baosPDF;

    public static void previewPDFDocumentInImage() throws IOException {
        ByteBuffer buf = null;

        try {
            buf = ByteBuffer.wrap(createPDF().toByteArray());
        } catch (DocumentException e) {
        // use the PDF Renderer library on the buf which contains the in memory PDF document
        PDFFile pdffile = new PDFFile(buf);
        PDFPage page = pdffile.getPage(1);

        //get the width and height for the doc at the default zoom
        Rectangle rect =
            new Rectangle(0, 0, (int)page.getBBox().getWidth(), (int)page.getBBox().getHeight());

        //generate the image
        Image img = page.getImage(rect.width, rect.height, //width & height
                rect, // clip rect
                null, // null for the ImageObserver
                true, // fill background with white
                true) // block until drawing is done

        //show the image in a frame
        JFrame frame = new JFrame("My incredible PDF document");
        frame.add(new JLabel(new ImageIcon(img)));

    public static void main(final String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    try {
                    } catch (IOException ex) {

5. Run the class

The outcome looks like this:


About Post Author

Lucas Jellema

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director and Oracle Developer Champion. Solution architect and developer on diverse areas including SQL, JavaScript, Kubernetes & Docker, Machine Learning, Java, SOA and microservices, events in various shapes and forms and many other things. Author of the Oracle Press book Oracle SOA Suite 12c Handbook. Frequent presenter on user groups and community events and conferences such as JavaOne, Oracle Code, CodeOne, NLJUG JFall and Oracle OpenWorld.
0 %
0 %
0 %
0 %
0 %
0 %

Average Rating

5 Star
4 Star
3 Star
2 Star
1 Star

6 thoughts on “Java: Generating PDF and Previewing it as an Image – iText and PDF Renderer

  1. This example works like a charm, for one page. In case of multiple pages, doc.newPage();  after  doc.add(image); do the stuff.

  2. This is a good way of generating PDF dynamically. The only problem with this means is that there are no controls similar to Adobe Reader’s controls. How could that be added to the application.

Comments are closed.

Next Post

ADF 11g - TreeTable with sub totals - how the SQL query can make life easier for the View developer

The ADF 11g Tree Table component can be used for the compact presentation of fairly complex data sets. It allows the user to quickly drill down to a specific area of interest. For example to find all Roles within a specific Department and for any Role all Employees in that […]
%d bloggers like this: