在我們編輯PDF文檔的過程中,有時候需要在文檔中添加一些如多邊形、矩形、橢圓形之類的圖形,而Free Spire PDF for Java 則正好可以幫助我們在Java程序中通過代碼在PDF文檔中繪制形狀,以及設置形狀邊線顏色和填充色。
Jar包導入
方法一:下載Free Spire.PDF for Java包并解壓縮,然后將lib文件夾下的Spire.Pdf.jar包作為依賴項導入到Java應用程序中
方法二:直接通過Maven倉庫安裝JAR包,配置pom.xml文件的代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
< repositories > < repository > < id >com.e-iceblue</ id > < url >http://repo.e-iceblue.cn/repository/maven-public/</ url > </ repository > </ repositories > < dependencies > < dependency > < groupId >e-iceblue</ groupId > < artifactId >spire.pdf.free</ artifactId > < version >2.6.3</ version > </ dependency > </ dependencies > |
Java代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
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){ //創建PDF文檔,并添加一頁 PdfDocument pdf = new PdfDocument(); PdfPageBase page = pdf.appendPage(); //創建畫筆、畫刷 PdfPen pen = new PdfPen( new PdfRGBColor(Color.black), 0.3 ); PdfBrush brush= PdfBrushes.getGreenYellow(); //繪制矩形 Rectangle2D.Float rect1 = new Rectangle2D.Float( 0 , 20 , 120 , 50 ); //創建Rectangle2D對象,并指定圖形在PDF頁面中的大小、位置 PdfLinearGradientBrush linearGradientBrush = new PdfLinearGradientBrush(rect1, new PdfRGBColor(Color.pink), new PdfRGBColor(Color.YELLOW),PdfLinearGradientMode.Horizontal); //填充圖形顏色 page.getCanvas().drawRectangle(linearGradientBrush, rect1); //繪制矩形到頁面 //繪制橢圓 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); //繪制多邊形 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); //繪制弧形 float startAngle = 0 ; float sweepAngle = 270 ; Rectangle2D.Float rect3= new Rectangle2D.Float( 0 , 110 , 50 , 50 ); page.getCanvas().drawArc(pen, rect3, startAngle, sweepAngle); //繪制扇形 Rectangle2D.Float rect4= new Rectangle2D.Float( 70 , 110 , 50 , 50 ); page.getCanvas().drawPie(pen, rect4, startAngle, sweepAngle); //繪制兩條垂直交叉的直線 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); //繪制貝塞爾曲線 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); //保存文檔 pdf.saveToFile( "DrawShapes.pdf" ,FileFormat.PDF); } } |
效果圖
以上就是Java 在PDF中繪制形狀的兩種方法的詳細內容,更多關于Java 在PDF中繪制形狀的資料請關注服務器之家其它相關文章!
原文鏈接:https://www.cnblogs.com/jazz-z/p/13278928.html