In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to draw shapes in PDF with Java". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
In the process of editing PDF documents, sometimes we need to add some graphics such as polygons, rectangles, ellipses in the document, and Free Spire PDF for Java can just help us draw shapes in PDF documents through code in Java programs, and set shape border colors and fill colors.
Jar package import
Method 1: Download the Free Spire.PDF for Java package and unzip it, then import the Spire.Pdf.jar package from the lib folder into the Java application as a dependency
Method 2: Install the JAR package directly through the Maven repository. The code for configuring the pom.xml file is as follows:
com.e-iceblue http://repo.e-iceblue.cn/repository/maven-public/ e-iceblue spire.pdf.free 2.6.3
Java code
import com.spire.pdf.*; import com.spire.pdf.graphics.*; import java.awt.*; import java.awt.geom.Rectangle2D;public class DrawShapes { public static void main(String[]args){ //Create PDF document and add a page PdfDocument pdf = new PdfDocument(); PdfPageBase page = pdf.appendPage(); //Create brushes, brushes PdfPen pen =new PdfPen(new PdfRGBColor(Color.black),0.3); PdfBrush brush= PdfBrushes.getGreenYellow(); //Draw rectangle Rectangle2D.Float rect1 = new Rectangle2D.Float(0, 20, 120, 50);//Create a Rectangle2D object and specify the size and position of the graphic on the PDF page PdfLinearGradient Brush linearGradient Brush = new PdfLinearGradient Brush (rect1,new PdfrGBColor(Color.pink),new PdfrGBColor (Color.YELLOW), PdfLinearGradient Mode.Horizontal);//Fill graphic color page.getCanvas().drawRectangle(linearGradient Brush, rect1);//Draw rectangle to page //Draw 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); page.getCanvas().drawEllipse(radialGradientBrush,rect2); //Draw 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 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 fan Rectangle2D.Float rect4= new Rectangle2D.Float(70, 110, 50, 50); page.getCanvas().drawPie(pen, rect4, startAngle, sweepAngle); //Draw two perpendicular intersecting lines Point pStart1=new Point(205,110); Point pEnd1=new Point(205,160); page.getCanvas().drawLine(pen, pStart1, pEnd1); Point pStart2=new Point(180,135); Point pEnd2=new Point(230,135); page.getCanvas().drawLine(pen, pStart2, pEnd2); //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 pdf.saveToFile("DrawShapes.pdf",FileFormat.PDF); }}"How to draw shapes in PDF with Java" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.