Drawing Geometric Shapes in PDF Documents Using Java

When working with PDF documents programmatically, you often need to add geometric elements such as rectangles, ellipses, polygons, or curved lines. This tutorial demonstrates how to use Java to create various shapes in PDF files and customize their appearance with different stroke colors and fill effects.

Dependency Configuration

To incorporate PDF shape-drawing capabilities into your Java application, you can add the required library through Maven. Add the following configuration to you're project's pom.xml file:

<repositories>
    <repository>
        <id>e-iceblue</id>
        <url>https://repo.e-iceblue.com/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf.free</artifactId>
        <version>4.5.1</version>
    </dependency>
</dependencies>

Implementation Example

The following Java code demonstrates how to draw multiple geometric shapes on a single PDF page. Each shape type utilizes different drawing methods and styling options available in the library.

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.*;
import java.awt.*;

public class ShapeDrawingDemo {
    public static void main(String[] args) {
        PdfDocument doc = new PdfDocument();
        PdfPageBase page = doc.addPage();

        PdfPen outlinePen = new PdfPen(new PdfRGBColor(Color.DARK_GRAY), 0.5f);
        PdfSolidBrush fillBrush = new PdfSolidBrush(new PdfRGBColor(0x90EE90));

        Rectangle2D.Float rectangleArea = new Rectangle2D.Float(30, 50, 150, 60);
        PdfLinearGradientBrush gradientBrush = new PdfLinearGradientBrush(
                rectangleArea,
                new PdfRGBColor(0xFFB6C1),
                new PdfRGBColor(0xFFD700),
                PdfLinearGradientMode.ForwardDiagonal
        );
        page.getCanvas().drawRectangle(gradientBrush, rectangleArea);

        Rectangle2D.Float ellipseArea = new Rectangle2D.Float(220, 50, 70, 70);
        Point ellipseCenter = new Point(255, 85);
        PdfRadialGradientBrush radialGradient = new PdfRadialGradientBrush(
                ellipseCenter, 0,
                ellipseCenter, 35,
                new PdfRGBColor(0xFFFFFF),
                new PdfRGBColor(0x00CED1)
        );
        page.getCanvas().drawEllipse(radialGradient, ellipseArea);

        Point[] polygonVertices = {
                new Point(330, 110),
                new Point(355, 65),
                new Point(385, 85),
                new Point(400, 40),
                new Point(435, 110)
        };
        page.getCanvas().drawPolygon(outlinePen, fillBrush, polygonVertices);

        Rectangle2D.Float arcBounds = new Rectangle2D.Float(30, 150, 60, 60);
        page.getCanvas().drawArc(outlinePen, arcBounds, 45f, 270f);

        Rectangle2D.Float pieBounds = new Rectangle2D.Float(110, 150, 60, 60);
        page.getCanvas().drawPie(outlinePen, pieBounds, 0f, 135f);

        Point lineStart = new Point(210, 150);
        Point lineEnd = new Point(210, 210);
        page.getCanvas().drawLine(outlinePen, lineStart, lineEnd);

        Point hLineStart = new Point(185, 180);
        Point hLineEnd = new Point(235, 180);
        page.getCanvas().drawLine(outlinePen, hLineStart, hLineEnd);

        Point bezierStart = new Point(290, 180);
        Point controlPointA = new Point(340, 120);
        Point controlPointB = new Point(340, 240);
        Point bezierEnd = new Point(390, 180);
        page.getCanvas().drawBezier(outlinePen, bezierStart, controlPointA, controlPointB, bezierEnd);

        doc.saveToFile("GeometricShapes.pdf", com.spire.pdf.FileFormat.PDF);
    }
}

Code Explanation

The example begins by creating a new PDF document and adding a blank page to serve as the canvas. A pen object defines the stroke characteristics for outlined shapes, while brushes handle the fill effects applied to closed shapes.

For the rectangle, the code positions a linear gradient that transitions from pink to gold diagonally across the shape. The ellipse uses a radial gradient centered within its boundaries, creating a spotlight effect from white to cyan. The polygon connects five points in sequence to form an irregular five-sided figure filled with a solid green color.

The arc and pie methods both accept start and sweep angle parameters, where the pie variant extends lines from the arc endpoints to the center point, creating a wedge or slice shape. The line drawing methods create perpendicular crossing lines to demonstrate basic path construction.

Finally, the bezier curve method takes four points: a starting point, two control points that influence the curve's trajectory, and an ending point. The control points act as gravitational pull points that shape the bezier curve between its endpoints.

After rendering all shapes, the document saves to disk in PDF format, preserving all vector graphics at any zoom level without quality loss.

Tags: java PDF graphics geometric-shapes spire-pdf

Posted on Tue, 22 Sep 2026 16:14:25 +0000 by eshban