Java draws shapes in PDF documents

The PDF document editing process may require you to add graphics such as polygons, rectangles, and ellipses to your document. Free Spire.PDF for Java helps you draw shapes in PDF documents using Java code, set the edge color and fill color of the shape.

** Import JAR package ** ** Method 1: ** Download Free Spire.PDF for Java, unzip and unzip the jar package Import directly into your Java application as a dependency on the lib folder.

** Method 2: ** Install the jar package from the Maven repository and configure the code in the pom.xml file as follows:


** Java code **

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

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

public class DrawShapes {

    public static void main(String[] args) {

        //Create a PDF document and add a page
        PdfDocument doc = new PdfDocument();
        PdfPageBase page = doc.getPages().add();

        //Draw a rectangle
        PdfPen pen =new PdfPen(new PdfRGBColor(,0.1);
        Rectangle2D.Float rect1 = new Rectangle2D.Float(0, 20, 120, 50);
        PdfLinearGradientBrush linearGradientBrush = new PdfLinearGradientBrush(rect1,new PdfRGBColor(,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);

        //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 chart
        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);



Recommended Posts

Java draws shapes in PDF documents
I created a PDF in Java.
Add footnotes to Word documents in Java
Program PDF headers and footers in Java
Partization in Java
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
Read a string in a PDF file with Java
Text extraction in Java from PDF with pdfbox-2.0.8
Java adds and removes watermarks in word documents
Java adds page numbers to existing PDF documents
Compare PDF output in Java for snapshot testing
Launch Docker from Java to convert Office documents to PDF
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
[Java] Convert PDF version
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
"Hello World" in Java
Callable Interface in Java
Java compressed PDF document
Java extracts text content of SmartArt shapes in PowerPoint
Comments in Java source
Azure functions in java
Format XML in Java
Simple htmlspecialchars in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
[Java] PDF viewing settings
Java applications convert Word (DOC / DOCX) documents to PDF
Ping commands in Java
Merge Java Word documents
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
Java joins and splitting table cells in Word documents
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Try using RocksDB in Java
Read binary files in Java 1
Java adds table to PDF
Avoid Yubaba's error in Java
Get EXIF information in Java
[Neta] Sleep Sort in Java
Java history in this world
Let Java segfault in 6 lines