本文将介绍如何使用spire.pdf 在java应用程序中为pdf文档添加弹出式附注和文本框注释。
新建pdf文档添加弹出式附注:
import com.spire.pdf.*;
import com.spire.pdf.annotations.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.*;
public class pdfannotation {
public static void main(string[] args) {
//初始化pdfdocument实例
pdfdocument doc = new pdfdocument();
//设置边距
pdfunitconvertor unitcvtr = new pdfunitconvertor();
pdfmargins margin = new pdfmargins();
margin.settop(unitcvtr.convertunits(2.54f, pdfgraphicsunit.centimeter, pdfgraphicsunit.point));
margin.setbottom(margin.gettop());
margin.setleft(unitcvtr.convertunits(3f, pdfgraphicsunit.centimeter, pdfgraphicsunit.point));
margin.setright(margin.getleft());
//添加新页
pdfpagebase page = doc.getpages().add(pdfpagesize.a4, margin);
//插入文本
pdfbrush brush1 = pdfbrushes.getblack();
pdftruetypefont font1 = new pdftruetypefont(new font("arial unicode ms", font.bold font.italic,13), true);
pdfstringformat format1 = new pdfstringformat(pdftextalignment.left);
float y = 50;
string s = "spire.pdf for java 用于创建编辑pdf文档";
page.getcanvas().drawstring(s, font1, brush1, 0, y - 5, format1);
y = y (float)font1.measurestring(s, format1).getheight();
//指定附注的文本、图标及图标颜色
pdftruetypefont font = new pdftruetypefont(new font("arial unicode ms",0, 10));
pdfstringformat format = new pdfstringformat();
format.setmeasuretrailingspaces(true);
string prompt = "弹出式注释";
dimension2d size = font.measurestring(prompt, format);
page.getcanvas().drawstring(prompt, font, pdfbrushes.getdodgerblue(), 0, y);
float x = (float)size.getwidth();
string label = "spire.pdf for java添加弹出式批注";
page.getcanvas().drawstring(label, font, pdfbrushes.getorangered(), x, y);
x = x (float)font.measurestring(label, format).getwidth();
string markuptext = "什么是spire.pdf for java";
rectangle2d rectangle2d = new rectangle.float();
rectangle2d.setframe(new point2d.double(x,y),new dimension());
pdfpopupannotation annotation = new pdfpopupannotation(rectangle2d, markuptext);
annotation.seticon(pdfpopupicon.paragraph);
annotation.setopen(true);
annotation.setcolor(new pdfrgbcolor(color.yellow));
((pdfnewpage) page).getannotations().add(annotation);
//保存文档
doc.savetofile("output/annotation.pdf");
doc.close();
}
}
效果图:
为已有pdf文档添加文本框注释:
import com.spire.pdf.*;
import com.spire.pdf.annotations.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.*;
import com.spire.pdf.general.find.pdftextfind;
public class pdfannotation {
public static void main(string[] args) {
//加载示例文档
pdfdocument doc = new pdfdocument();
doc.loadfromfile("sample.pdf");
//获取第一页
pdfpagebase page = doc.getpages().get(0);
//获取文本
pdftextfind[] find = page.findtext("主要功能").getfinds();
//指定文本框注释的位置
float x = (float) (find[0].getposition().getx() - doc.getpagesettings().getmargins().getleft() find[0].getsize().getwidth() 20);
float y = (float) (find[0].getposition().gety() - doc.getpagesettings().getmargins().gettop() 20);
//创建文本框注释
rectangle2d.float rect = new rectangle2d.float(x, y, 120, 20);
pdffreetextannotation textannotation = new pdffreetextannotation(rect);
// 设置注释文字及字体,注释框边界颜色及背景色
textannotation.setmarkuptext("spire.pdf 文本框注释");
pdftruetypefont font = new pdftruetypefont(new font("arial unicode ms", 0, 10));
;
textannotation.setfont(font);
pdfannotationborder border = new pdfannotationborder(0.5f);
textannotation.setborder(border);
textannotation.setbordercolor(new pdfrgbcolor(color.pink));
textannotation.setcolor(new pdfrgbcolor(color.yellow));
textannotation.setopacity(0.75f);
textannotation.settextmarkupcolor(new pdfrgbcolor(color.black));
//添加注释到pdf
page.getannotationswidget().add(textannotation);
//保存文档
doc.savetofile("output/freetextannotation.pdf");
doc.close();
}
}
效果图: