亲宝软件园·资讯

展开

Java 在PDF中绘制形状 Java 在PDF中绘制形状的两种方法

Jazzz 人气:0

在我们编辑PDF文档的过程中,有时候需要在文档中添加一些如多边形、矩形、椭圆形之类的图形,而Free Spire PDF for Java 则正好可以帮助我们在Java程序中通过代码在PDF文档中绘制形状,以及设置形状边线颜色和填充色。

Jar包导入

方法一:下载Free Spire.PDF for Java包并解压缩,然后将lib文件夹下的Spire.Pdf.jar包作为依赖项导入到Java应用程序中

方法二:直接通过Maven仓库安装JAR包,配置pom.xml文件的代码如下:

<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代码

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

效果图

加载全部内容

相关教程
猜你喜欢
用户评论