Java Draw Shapes in PDF Document
Tags: java, pdf
Published: 2020-07-20 06:51:37

In the process of manipulating PDF documents, we sometimes need to draw some shapes such as polygons, rectangles, and ellipses to the document. In this article, I will show you how to use Free Spire. PDF for Java to draw shapes in a PDF document, as well as set shape border colors and fill colors.

**Installation**
**Method 1:** Download the [Free Spire.PDF for Java](https://www.e-iceblue.com/Download/pdf-for-java-free.html) and unzip it.Then add the Spire.Pdf.jar file to your project as dependency.

**Method 2:** You can also add the jar dependency to maven project by adding the following configurations to the pom.xml.

```java
<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <name>e-iceblue</name>
            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
        </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf.free</artifactId>
        <version>2.6.3</version>
    </dependency>
</dependencies>
```

**Draw Shapes**

```java
import java.awt.*;
import java.awt.geom.Rectangle2D;


import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.*;


public class DrawShapes {

    public static void main(String[] args) {

        //create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //add a page
        PdfPageBase page = doc.getPages().add();

        //draw a rectangle
        PdfPen pen =new PdfPen(new PdfRGBColor(Color.black),0.1);
        Rectangle2D.Float rect1 = new Rectangle2D.Float(0, 20, 120, 50);
        PdfLinearGradientBrush linearGradientBrush = new PdfLinearGradientBrush(rect1,new PdfRGBColor(Color.pink),new PdfRGBColor(Color.yellow),PdfLinearGradientMode.Horizontal);
        page.getCanvas().drawRectangle(pen, linearGradientBrush, rect1);

        //draw an ellipse
        Point centerStart= new Point(205,45);
        Point centerEnd= new Point(205,45);
        PdfRadialGradientBrush radialGradientBrush = new PdfRadialGradientBrush(centerStart,0,centerEnd,25,new PdfRGBColor(Color.white),new PdfRGBColor(Color.cyan));
        Rectangle2D.Float rect2= new Rectangle2D.Float(180, 20, 50, 50);
        page.getCanvas().drawEllipse(pen,radialGradientBrush,rect2);

        //draw a polygon
        Point p1=new Point(290,70);
        Point p2=new Point(310,45);
        Point p3=new Point(325,60);
        Point p4=new Point(340,20);
        Point p5=new Point(370,70);
        Point[] points = {p1, p2, p3, p4, p5};
        PdfBrush brush= PdfBrushes.getGreenYellow();
        page.getCanvas().drawPolygon(pen,brush, points);

        //draw an arc
        float startAngle = 0;
        float sweepAngle = 270;
        Rectangle2D.Float rect3= new Rectangle2D.Float(0, 110, 50, 50);
        page.getCanvas().drawArc(pen, rect3, startAngle, sweepAngle);

        //draw a pie graph
        Rectangle2D.Float rect4= new Rectangle2D.Float(70, 110, 50, 50);
        page.getCanvas().drawPie(pen, rect4, startAngle, sweepAngle);

        //draw a line
        Point pStart=new Point(205,110);
        Point pEnd=new Point(205,160);
        page.getCanvas().drawLine(pen, pStart, pEnd);

        //draw a Bezier curve
        Point startPoint = new Point(290, 135);
        Point firstControlPoint = new Point(330, 70);
        Point secondControlPoint = new Point(330, 200);
        Point endPoint = new Point(370, 135);
        page.getCanvas().drawBezier(pen, startPoint, firstControlPoint, secondControlPoint, endPoint);

        //save to file
        doc.saveToFile("DrawShapes.pdf", FileFormat.PDF);
    }

}
```
![](https://dev-to-uploads.s3.amazonaws.com/i/qm6zqflo180mt5l6tosh.jpg)