PDFBox - Adding Rectangles


Advertisements


This chapter teaches you how to create color boxes in a page of a PDF document.

Creating Boxes in a PDF Document

You can add rectangular boxes in a PDF page using the addRect() method of the PDPageContentStream class.

Following are the steps to create rectangular shapes in a page of a PDF document.

Step 1: Loading an Existing PDF Document

Load an existing PDF document using the static method load() of the PDDocument class. This method accepts a file object as a parameter, since this is a static method you can invoke it using class name as shown below.

File file = new File("path of the document") 
PDDocument document = PDDocument.load(file);

Step 2: Getting the Page Object

You need to retrieve the PDPage object of the required page where you want to add rectangles using the getPage() method of the PDDocument class. To this method you need to pass the index of the page where you want to add rectangles.

PDPage page = document.getPage(0);

Step 3: Preparing the Content Stream

You can insert various kinds of data elements using the object of the class named PDPageContentStream. You need to pass the document object and the page object to the constructor of this class therefore, instantiate this class by passing these two objects created in the previous steps as shown below.

PDPageContentStream contentStream = new PDPageContentStream(document, page);

Step 4: Setting the Non-stroking Color

You can set the non-stroking color to the rectangle using the setNonStrokingColor() method of the class PDPageContentStream. To this method, you need to pass the required color as a parameter as shown below.

contentStream.setNonStrokingColor(Color.DARK_GRAY);

Step 5: Drawing the rectangle

Draw the rectangle with required dimensions using the addRect() method. To this method, you need to pass the dimensions of the rectangle that is to be added as shown below.

contentStream.addRect(200, 650, 100, 100);

Step 6: Filling the Rectangle

The fill() method of the PDPageContentStream class fills the path between the specified dimensions with the required color as shown below.

contentStream.fill();

Step 7: Closing the Document

Finally close the document using close() method of the PDDocument class as shown below.

document.close();

Example

Suppose we have a PDF document named blankpage.pdf in the path C:\PdfBox_Examples\ and this contains a single blank page as shown below.

Blankpage

This example demonstrates how to create/insert rectangles in a PDF document. Here, we will create a box in a Blank PDF. Save this code as AddRectangles.java.

import java.awt.Color;
import java.io.File;
  
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
public class ShowColorBoxes {

   public static void main(String args[]) throws Exception {

      //Loading an existing document
      File file = new File("C:/PdfBox_Examples/BlankPage.pdf");
      PDDocument document = PDDocument.load(file);
        
      //Retrieving a page of the PDF Document
      PDPage page = document.getPage(0);

      //Instantiating the PDPageContentStream class
      PDPageContentStream contentStream = new PDPageContentStream(document, page);
       
      //Setting the non stroking color
      contentStream.setNonStrokingColor(Color.DARK_GRAY);

      //Drawing a rectangle 
      contentStream.addRect(200, 650, 100, 100);

      //Drawing a rectangle
      contentStream.fill();

      System.out.println("rectangle added");

      //Closing the ContentStream object
      contentStream.close();

      //Saving the document
      File file1 = new File("C:/PdfBox_Examples/colorbox.pdf");
      document.save(file1);

      //Closing the document
      document.close();
   }
}

Compile and execute the saved Java file from the command prompt using the following commands.

javac AddRectangles.java 
java AddRectangles

Upon execution, the above program creates a rectangle in a PDF document displaying the following image.

Rectangle created

If you verify the given path and open the saved document — colorbox.pdf, you can observe that a box is inserted in it as shown below.

Coloredbox

Advertisements