Java drawing graphics in PDF documents

This document describes how to draw graphics in PDF documents by Java programming. Including drawing rectangle, ellipse, irregular polygon, line, arc, curve, sector and so on. In view of the ideas provided in the method, we can also transform the graphic design ideas by ourselves, such as diamond, trapezoid or combination graphics.

Use tools: Free flame.pdf for Java (free version)

Jar file import:

Step 1: copy the two files under the Lib folder in the installation package to the new folder in the program (here you can create a new folder in the program and name it LIB)

Step 2: after copying the files, add two references to the program: select the two jar files, right-click and select "Build Path" – "Add to Build Path". Complete the reference.


Java code example (for reference)

import java.awt.Color;
import java.awt.Point;
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) {
        //Establish PdfDocument object
        PdfDocument doc = new PdfDocument();
        //Add a page
        PdfPageBase page = doc.getPages().add();      
        //Create a brush
        PdfPen pen =new PdfPen(new PdfRGBColor(,0.3);
        PdfBrush brush= PdfBrushes.getGreenYellow();
        //draw rectangle
        Rectangle2D.Float rect1 = new Rectangle2D.Float(0, 20, 120, 50);//Establish Rectangle2D Object, and specifies that the drawing PDF Size, position in page
        PdfLinearGradientBrush linearGradientBrush = new PdfLinearGradientBrush(rect1,new PdfRGBColor(Color.PINK),new PdfRGBColor(Color.YELLOW),PdfLinearGradientMode.Horizontal);//Fill color          
        page.getCanvas().drawRectangle(linearGradientBrush, rect1);//Draw rectangle to page
        //Drawing ellipse
        Point centerStart= new Point(205,45);
        Point centerEnd= new Point(205,45);
        PdfRadialGradientBrush radialGradientBrush = new PdfRadialGradientBrush(centerStart,0,centerEnd,60,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};       
        page.getCanvas().drawPolygon(pen,brush, points);
        //draw 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 fan shape     
        Rectangle2D.Float rect4= new Rectangle2D.Float(70, 110, 50, 50);
        page.getCanvas().drawPie(pen, rect4, startAngle, sweepAngle);
        //draw a straight line 
        Point pStart=new Point(205,110);
        Point pEnd=new Point(205,160);
        page.getCanvas().drawLine(pen, pStart, pEnd);
        //Draw 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 document

After the code is completed, run the program and generate the document. (as shown below)


(end of this paper)

Reprint please indicate the source!!

Tags: Java P4 Programming

Posted on Mon, 02 Dec 2019 19:49:26 -0500 by clio-stylers