本教程将展示如何使用spire.pdf for java在pdf文档中绘制形状,以及设置形状边线颜色和填充色。
import java.awt.color;
import java.awt.geom.rectangle2d;
import com.spire.ms.system.drawing.pointf;
import com.spire.pdf.graphics.*;
public class drawshapes {
public static void main(string[] args) {
//创建pdfdocument对象
pdfdocument doc = new pdfdocument();
//添加一页
pdfpagebase page = doc.getpages().add();
//绘制矩形
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.white),new pdfrgbcolor(color.blue),pdflineargradientmode.horizontal);
page.getcanvas().drawrectangle(pen, lineargradientbrush, rect1);
//绘制椭圆
pointf centerstart= new pointf(205,45);
pointf centerend= new pointf(205,45);
pdfradialgradientbrush radialgradientbrush = new pdfradialgradientbrush(centerstart,0,centerend,25,new pdfrgbcolor(color.pink),new pdfrgbcolor(color.red));
rectangle2d.float rect2= new rectangle2d.float(180, 20, 50, 50);
page.getcanvas().drawellipse(pen,radialgradientbrush,rect2);
//绘制多边形
pointf p1=new pointf(290,70);
pointf p2=new pointf(310,45);
pointf p3=new pointf(325,60);
pointf p4=new pointf(340,20);
pointf p5=new pointf(370,70);
pointf[] points = {p1, p2, p3, p4, p5};
pdfbrush brush= pdfbrushes.getgreenyellow();
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);
//绘制直线
pointf pstart=new pointf(205,110);
pointf pend=new pointf(205,160);
page.getcanvas().drawline(pen, pstart, pend);
//绘制贝塞尔曲线
pointf startpoint = new pointf(290, 135);
pointf firstcontrolpoint = new pointf(330, 70);
pointf secondcontrolpoint = new pointf(330, 200);
pointf endpoint = new pointf(370, 135);
page.getcanvas().drawbezier(pen, startpoint, firstcontrolpoint, secondcontrolpoint, endpoint);
//保存文档
doc.savetofile("drawshapes.pdf", fileformat.pdf);
}
}