Draw Shape in PDF with iText


How to draw shapes in PDF using iText Java Library?

✍: FYIcenter.com


Here is a tutorial for creating a PDF document with some drawing shapes using iText Java Library.

1. Create a Java file, DrawingShape.java:

 * Drawing shapes in PDF with iText
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.kernel.pdf.canvas.*;
import com.itextpdf.layout.*;
import java.io.*;

public class DrawingShape {
  public static void main(String args[]) throws IOException {
    PdfWriter writer = new PdfWriter("shape.pdf");
    PdfDocument pdf = new PdfDocument(writer);

    PdfCanvas canvas = new PdfCanvas(pdf.addNewPage());
    canvas.moveTo(72, 72).lineTo(360, 72).stroke();
    canvas.rectangle(144, 144, 72, 72).fill();
    canvas.circle(360, 144, 72).fill();

2. Run the Java program iText 7 Java Library.

fyicenter$ java -cp java -cp kernel-7.1.4.jar:layout-7.1.4.jar: \
  io-7.1.4.jar:slf4j-api-1.7.31.jar \

fyicenter$ ls -l *.pdf 
  1043 shape.pdf

3. View the new PDF, shape.pdf, in a browser. You see a PDF page with some drawing shapes.

Drawing Shape in PDF with iText Java Library
Drawing Shape in PDF with iText Java Library


⇒ Get PDF Document Info with iText

⇐ Embed Image in PDF with iText

⇑ Using iText Library in Java Programs

⇑⇑ iText for PDF Generation

2021-11-13, 312👍, 0💬