通过添加标注可对特定内容进行解释、说明或其他附加内容等,本文介绍使用spire.pdf for java在pdf文档中添加多种不同类型的标注的方法,包括箭头、云朵、椭圆、矩形和连接线条等形状类型。
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();
}
}
标注添加结果如下,鼠标移动至相应标注内容上可弹出标注提示内容: