iText Rectangle Example
In the past examples,we studied about the PDFWriter and PDFReader examples. In this example, we will demonstrate how we can create an Itext Rectangle
and use it in our PDF document.
1. Setup the Project
Let’s setup the project by creating a simple Maven project and selecting the skip archetype selection. Update the contents of pom.xml
with the contents of the file below:
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>ITextExample</groupId> <artifactId>ITextExample</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.6</version> </dependency> <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version>1.52</version> </dependency> </dependencies> </project>
Now that the project is setup, let’s create a Rectangle in the PDF Document using the IText
library.
CreateRectangle.java
package com.jcg.examples; import java.io.FileNotFoundException; import java.io.FileOutputStream; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfWriter; public class CreateRectangle { public static void main(String[] args) { try { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("Rectagled.pdf")); document.open(); PdfContentByte contentByte = writer.getDirectContent(); Rectangle rectangle = new Rectangle(36, 36, 559, 806); rectangle.setBorder(Rectangle.BOX); contentByte.setColorStroke(BaseColor.BLACK); rectangle.setBorderWidth(2); contentByte.rectangle(rectangle); document.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } } }
We create an instance of com.itextpdf.text.Document
class. Then we create a reference to the underlying com.itextpdf.text.pdf.PdfContentByte
object and create a com.itextpdf.text.Rectangle
Object on it. The Rectangle
element can be customized like setting its border color, filing it with a particular color, adjusting text to fit inside the rectangle. In the example above, I have set the border Color to Black. We can create specific colors by using the com.itextpdf.text.BaseColor
class and passing the specified values R-G-B
color values.
Here’s how the Rectangle looks like in a document.
Here’s another code snippet that fills up the rectangle with the specified color :
package com.jcg.examples; import java.io.FileNotFoundException; import java.io.FileOutputStream; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfWriter; public class FillRectangle { public static void main(String[] args) { try { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("Rectangled.pdf")); document.open(); PdfContentByte contentByte = writer.getDirectContent(); contentByte.rectangle(186, 186, 159, 150); contentByte.setColorFill(BaseColor.CYAN); contentByte.fill(); document.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } } }
To fill a rectangle we use the PdfContentByte#setColorFill
method to set the Color to fill the background and then call the PdfContentByte#fill()
method to actually fill up the method.
Here’s how the output looks like :
Similarly, we can use the com.itextpdf.text.pdf.ColumnText#showTextAligned
method to place fonts and other PDF Objects into the Rectangle.
2. Download the Source Code
Here we studied how we can create an IText Rectangle
and modify it to fit our requirements for the document.
You can download the source code of this example here: ItextRectangleExample.zip