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);
}
}
Recommended Posts