JavaRanch Home    
This page:         last edited 11 May 2015         What's Changed?         Edit

PDFBox example   

This example shows how to create a document using the Apache PDFBox library. The first page contains text in various styles, fonts and colors. The second page adds an image, draws a couple of lines, and a colored rectangle. While PDFBox can do many things with an existing PDF, its API is somewhat low-level. For example, it's not easily possible to add underline or strikethrough effects to text.

To get started, download the latest pdfbox-app-X.Y.Z.jar file; it contains everything in one convenient file.

import java.awt.Color;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

public class Simple {

    public static void main (String[] args) throws Exception {
        String outputFileName = "Simple.pdf";
        if (args.length > 0)
            outputFileName = args[0];

        // Create a document and add a page to it
        PDDocument document = new PDDocument();
        PDPage page1 = new PDPage(PDPage.PAGE_SIZE_A4);
            // PDPage.PAGE_SIZE_LETTER is also possible
        PDRectangle rect = page1.getMediaBox();
            // rect can be used to get the page width and height

        // Create a new font object selecting one of the PDF base fonts
        PDFont fontPlain = PDType1Font.HELVETICA;
        PDFont fontBold = PDType1Font.HELVETICA_BOLD;
        PDFont fontItalic = PDType1Font.HELVETICA_OBLIQUE;
        PDFont fontMono = PDType1Font.COURIER;

        // Start a new content stream which will "hold" the to be created content
        PDPageContentStream cos = new PDPageContentStream(document, page1);

        int line = 0;

        // Define a text content stream using the selected font, move the cursor and draw some text
        cos.setFont(fontPlain, 12);
        cos.moveTextPositionByAmount(100, rect.getHeight() - 50*(++line));
        cos.drawString("Hello World");

        cos.setFont(fontItalic, 12);
        cos.moveTextPositionByAmount(100, rect.getHeight() - 50*(++line));

        cos.setFont(fontBold, 12);
        cos.moveTextPositionByAmount(100, rect.getHeight() - 50*(++line));

        cos.setFont(fontMono, 12);
        cos.moveTextPositionByAmount(100, rect.getHeight() - 50*(++line));
        cos.drawString("Monospaced blue");

        // Make sure that the content stream is closed:

        PDPage page2 = new PDPage(PDPage.PAGE_SIZE_A4);
        cos = new PDPageContentStream(document, page2);

        // draw a red box in the lower left hand corner

        // add two lines of different widths

        // add an image
        try {
            BufferedImage awtImage = File("Simple.jpg"));
            PDXObjectImage ximage = new PDPixelMap(document, awtImage);
            float scale = 0.5f// alter this value to set the image size
            cos.drawXObject(ximage, 100400, ximage.getWidth()*scale, ximage.getHeight()*scale);
        } catch (FileNotFoundException fnfex) {
            System.out.println("No image for you");

        // close the content stream for page 2

        // Save the results and ensure that the document is properly closed:;

OtherOpenSourceProjectsFaq CategoryCodeSamples CodeBarn

JavaRanchContact us — Copyright © 1998-2014 Paul Wheaton