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:

<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>

** 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;
import com.spire.pdf.graphics.*;


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(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 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);
    }

}

shapes.jpg

Recommended Posts

Java draws shapes in PDF documents
Save Java PDF in Excel
Add footnotes to Word documents in Java
Program PDF headers and footers in Java
Partization in Java
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 page numbers to existing PDF documents
Compare PDF output in Java for snapshot testing
[java] sort in list
Launch Docker from Java to convert Office documents to PDF
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
Java compressed PDF document
Java extracts text content of SmartArt shapes in PowerPoint
Azure functions in java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Hello World in Java
webApi memorandum in java
Type determination in Java
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)
ASCII art in Java
Compare Lists in Java
POST JSON 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
Initializing HashMap in Java
Try using RocksDB in Java
Read binary files in Java 1
Java adds table to PDF
Get EXIF information in Java
[Neta] Sleep Sort in Java
Java history in this world
Let Java segfault in 6 lines
Try calling JavaScript in Java
Try developing Spresense in Java (1)
Try functional type in Java! ①
Java encryption and decryption PDF
I made roulette in Java.