标签:tps 需要 diff ram ica tcap setname cap geo
本文介绍通过Java程序在PDF文档中创建多种不同类型的标记,包括箭头、云朵、椭圆、矩形和连接线条等形状的标记。需要使用PDF类库工具(Spire.PDF for Java),下载解压Jar包后,将lib文件夹下的Spire.Pdf.jar文件导入Java程序;或者也可以通过Maven仓库下载导入。导入结果如下:import com.spire.pdf.*;
import com.spire.pdf.annotations.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.Date;
public class AddAnnotations {
public static void main(String[] args) throws Exception{
//创建PDF文档,添加一页
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.getPages().add();
//添加箭头标记
String text1 = "this is Arrow annotation";//指定文本字符串内容
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 20);//创建字体
PdfSolidBrush brush1 = new PdfSolidBrush(new PdfRGBColor(Color.blue));//创建画刷
PdfStringFormat Alignment = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);//设置文本对齐方式
page.getCanvas().drawString(text1, font, brush1, 50, 70, Alignment);//绘制文本到页面
Dimension2D dimension = font.measureString(text1);
Rectangle2D.Float bounds = new Rectangle2D.Float(50, 50, (float) dimension.getWidth(), (float) dimension.getHeight());
int[] linePoints = new int[]{92, (int) (page.getSize().getHeight() - bounds.getY() - 90),
(int) (92 + bounds.getWidth()), (int) (page.getSize().getHeight() - bounds.getY() - 90)};
PdfLineAnnotation annotation1 = new PdfLineAnnotation(linePoints, "LineArrow annotation test");
annotation1.setBeginLineStyle(PdfLineEndingStyle.OpenArrow);//创建单向箭头标记
annotation1.setEndLineStyle(PdfLineEndingStyle.OpenArrow);
annotation1.setBackColor(new PdfRGBColor(Color.red));
annotation1.setCaptionType(PdfLineCaptionType.Inline);
annotation1.setLineCaption(true);
((PdfNewPage) page).getAnnotations().add(annotation1);//添加标记
//添加云朵标记
String text2 = "this is Cloud annotation";
PdfBrush brush2 = PdfBrushes.getBlue();
page.getCanvas().drawString(text2, font, brush2, 50, 200);
Point2D point2D[] = new Point2D[]{
new Point2D.Float(30, 200),
new Point2D.Float(300, 180),
new Point2D.Float(300, 250),
new Point2D.Float(30, 220),
new Point2D.Float(30, 200)
};
PdfPolygonAnnotation annotation2 = new PdfPolygonAnnotation(page, point2D);
annotation2.setText("PolygonCloud annotation test");
annotation2.setAuthor("E-iceblue");
annotation2.setSubject("test");
annotation2.setModifiedDate(new Date());
annotation2.setBorderEffect(PdfBorderEffect.Big_Cloud);//创建边框为云朵状的标记
annotation2.setLocation(new Point2D.Float(190, 230));
annotation2.setColor(new PdfRGBColor(new Color(34,139,34)));
((PdfNewPage) page).getAnnotations().add(annotation2);//添加标记
//添加椭圆标记
String text3 = "this is Circle annotation";
PdfBrush brush3 = PdfBrushes.getBlue();
Dimension2D dimension2D = font.measureString(text3);
dimension2D.setSize(dimension2D.getWidth() + 35, dimension2D.getHeight() + 20);
page.getCanvas().drawString(text3, font, brush3, 50, 300);
Rectangle2D.Float annotationBounds1 = new Rectangle2D.Float();
annotationBounds1.setFrame(new Point2D.Float(36, (float) 290), dimension2D);
PdfSquareAndCircleAnnotation annotation3 = new PdfSquareAndCircleAnnotation(annotationBounds1);
annotation3.setSubType(PdfSquareAndCircleAnnotationType.Circle);//创建椭圆标记
float[] f1 = {0.5f, 0.5f, 0.5f, 0.5f};
annotation3.setRectangularDifferenceArray(f1);
annotation3.setText("Circle annotation test");
annotation3.setColor(new PdfRGBColor(new Color(255,0,0)));
annotation3.setModifiedDate(new Date());
annotation3.setName("*****");
LineBorder border1 = new LineBorder();
border1.setBorderWidth(2);
annotation3.setLineBorder(border1);
((PdfNewPage) page).getAnnotations().add(annotation3);//添加标记
//添加矩形标记
String text4 = "this is Square annotation";
PdfBrush brush4 = PdfBrushes.getBlue();
Dimension2D dimension4 = font.measureString(text4);
dimension2D.setSize(dimension2D.getWidth() + 80, dimension2D.getHeight() + 20);
page.getCanvas().drawString(text4, font, brush4, 50, 400);
Rectangle2D.Float annotationBounds2 = new Rectangle2D.Float();
annotationBounds2.setFrame(new Point2D.Float(50, (float) 400), dimension4);//矩形位置
PdfSquareAndCircleAnnotation annotation4 = new PdfSquareAndCircleAnnotation(annotationBounds2);
annotation4.setSubType(PdfSquareAndCircleAnnotationType.Square);//创建矩形标记
float[] f2 = {0.5f, 0.5f, 0.5f, 0.5f};
annotation4.setRectangularDifferenceArray(f2);
annotation4.setText("Square annotation test");
annotation4.setColor(new PdfRGBColor(new Color(255,0,255)));
annotation4.setModifiedDate(new Date());
LineBorder border2 = new LineBorder();
border2.setBorderWidth(1);
annotation4.setLineBorder(border2);
((PdfNewPage) page).getAnnotations().add(annotation4);
//添加连接线标记
String text5 = "this is Connected lines annotation";
PdfBrush brush5 = PdfBrushes.getBlue();
page.getCanvas().drawString(text5, font, brush5, 50, 465);
Point2D pointzd[] = new Point2D[]{
new Point2D.Float(30, 470),
new Point2D.Float(350, 450),
new Point2D.Float(350, 520),
new Point2D.Float(30, 490),
new Point2D.Float(30, 470)
};
PdfPolygonAnnotation annotation5 = new PdfPolygonAnnotation(page, pointzd);
annotation5.setText("Connected Lines annotation test");
annotation5.setAuthor("冰蓝");
annotation5.setSubject("test");
annotation5.setModifiedDate(new Date());
annotation5.setBorderEffect(PdfBorderEffect.None);
annotation5.setLocation(new Point2D.Float(190, 230));
annotation5.setColor(new PdfRGBColor(new Color(255,215,0)));
((PdfNewPage) page).getAnnotations().add(annotation5);
//保存文档
pdf.saveToFile("AddAnnotations.pdf");
pdf.dispose();
}
}
创建结果:
(完)
标签:tps 需要 diff ram ica tcap setname cap geo
原文地址:https://blog.51cto.com/eiceblue/2497353