PDF 生成模板编辑器
开源可视化 PDF 生成引擎,支持自定义模板,并提供面向开发者的 API,灵活构建文档生成流程。
ComPDF for Web 支持各种标准注释,每一个都以类似的方式添加到项目中。在创建注释之前,你需要在你的项目中初始化一个 PDF 文档。
注: 当添加注释时,页面空间是一个以当前页面左上角为原点的坐标系统来定位注释在页面的位置。
// 导入 ComPDFKit Web Demo 的JS文件。
import ComPDFKitViewer from "@compdfkit_pdf_sdk/webviewer";
const viewer = document.getElementById('webviewer');
ComPDFKitViewer({
path: '/',
initialDoc: 'Your PDF Url',
licenseKey: 'Input your license here'
}, viewer)
.then((instance) => {
const { documentViewer, annotationManager, Annotations, Actions } = instance.Core;
documentViewer.addEventListener('documentLoaded', () => {
console.log('ComPDFKit Web Demo loaded');
})
});每个注释都是 Annotations 中某个类的实例。用 PageNumber(从 1 开始)指定页面,用 X、Y、Width、Height 指定矩形,再调用 annotationManager.addAnnotation() 添加。
使用以下方法将便签注释(文本便签注释)添加到 PDF 文档页面。
const text = new Annotations.StickyAnnotation({
PageNumber: 1,
X: 100,
Y: 30,
Width: 24,
Height: 24,
StrokeColor: new Annotations.Color(255, 0, 0),
Opacity: 1
});
text.setContents('test');
annotationManager.addAnnotation(text);使用以下方法将超链接或页面链接添加到 PDF 文档中。
// 添加超链接。
const link = new Annotations.Link({
PageNumber: 1,
X: 92,
Y: 200,
Width: 75,
Height: 30
});
link.addAction('U', new Actions.URI({ uri: 'https://example.com' }));
annotationManager.addAnnotation(link);
// 添加页面链接。
const pageLink = new Annotations.Link({
PageNumber: 1,
X: 92,
Y: 200,
Width: 75,
Height: 30
});
pageLink.addAction('U', new Actions.GoTo({ dest: { page: 2 } }));
annotationManager.addAnnotation(pageLink);使用以下方法将文本注释添加到 PDF 文档页面。
const freeText = new Annotations.FreeTextAnnotation({
PageNumber: 1,
X: 100,
Y: 200,
Width: 60,
Height: 40,
TextColor: new Annotations.Color(0, 0, 0),
FontSize: '16pt',
Font: 'Helvetica',
Opacity: 1,
TextAlign: 'left'
});
freeText.setContents('test');
annotationManager.addAnnotation(freeText);使用以下方法将图形注释,如矩形,圆形,线条和箭头等添加到 PDF 文档页面。
const red = new Annotations.Color(255, 0, 0);
// 矩形。
const square = new Annotations.RectangleAnnotation({
PageNumber: 1,
X: 50,
Y: 50,
Width: 50,
Height: 50,
StrokeThickness: 2,
Opacity: 1,
FillColor: null, // 无填充。
StrokeColor: red,
Style: 'solid'
});
annotationManager.addAnnotation(square);
// 圆形。
const circle = new Annotations.EllipseAnnotation({
PageNumber: 1,
X: 50,
Y: 50,
Width: 50,
Height: 50,
StrokeThickness: 2,
Opacity: 1,
FillColor: null, // 无填充。
StrokeColor: red,
Style: 'solid'
});
annotationManager.addAnnotation(circle);
// 线条。
const line = new Annotations.LineAnnotation({
PageNumber: 1,
StrokeThickness: 2,
Opacity: 1,
StrokeColor: red,
Style: 'solid'
});
line.setStartPoint(50, 50);
line.setEndPoint(100, 100);
annotationManager.addAnnotation(line);
// 多边形。
const polygon = new Annotations.PolygonAnnotation({
PageNumber: 1,
StrokeThickness: 2,
Opacity: 1,
FillColor: null, // 无填充。
StrokeColor: red,
Style: 'solid'
});
polygon.setPath([
new Annotations.Point(156, 328),
new Annotations.Point(230, 302),
new Annotations.Point(300, 362),
new Annotations.Point(310, 436),
new Annotations.Point(180, 447),
new Annotations.Point(114, 395)
]);
annotationManager.addAnnotation(polygon);
// 折线。
const polyline = new Annotations.PolylineAnnotation({
PageNumber: 1,
StrokeThickness: 2,
Opacity: 1,
StrokeColor: red,
Style: 'solid'
});
polyline.setPath([
new Annotations.Point(130, 156),
new Annotations.Point(202, 120),
new Annotations.Point(263, 223),
new Annotations.Point(157, 232)
]);
annotationManager.addAnnotation(polyline);
// 圆弧。
const arc = new Annotations.ArcAnnotation({
PageNumber: 1,
StrokeThickness: 2,
Opacity: 1,
StrokeColor: red
});
arc.setPath([
new Annotations.Point(92, 225),
new Annotations.Point(233, 169),
new Annotations.Point(143, 98)
]);
annotationManager.addAnnotation(arc);使用以下方法将高亮注释添加到 PDF 文档页面。其他标记类注释的添加方法类似下列代码所示方式。
注: 每个 Quad 的点依次为 (left,bottom), (right,bottom), (right,top), (left,top)。当前页面左上角为坐标原点。
// 标记注释的类包括:TextHighlightAnnotation, TextUnderlineAnnotation, TextStrikeoutAnnotation, TextSquigglyAnnotation。
const highlight = new Annotations.TextHighlightAnnotation({
PageNumber: 1,
Opacity: 0.8,
StrokeColor: new Annotations.Color(255, 0, 0)
});
highlight.setQuads([
new Annotations.Quad(116, 360, 360, 360, 360, 300, 116, 300)
]);
highlight.setContents('test');
annotationManager.addAnnotation(highlight);波浪线注释的波形由静态属性 Annotations.TextSquigglyAnnotation.waveLength(默认 10)和 waveAmplitude(默认 2)决定,单位为页面单位。它们作用于查看器中显示的所有波浪线注释,不影响保存到文件中的外观。修改后需重新绘制注释,例如调用 annotationManager.drawAnnotationsFromList(annotations)。
手绘是用各种颜色的笔在 PDF 文档上进行自由标记的注释类型。按照以下方法添加手绘注释到 PDF 文档页面。
const ink = new Annotations.FreeHandAnnotation({
PageNumber: 1,
StrokeThickness: 2,
Opacity: 0.8,
StrokeColor: new Annotations.Color(255, 0, 0)
});
ink.setPath([
new Annotations.Point(9.20, 34),
new Annotations.Point(9.20, 34),
new Annotations.Point(9.20, 33),
new Annotations.Point(9.20, 31),
new Annotations.Point(10.20, 31),
new Annotations.Point(11.20, 30),
new Annotations.Point(12.20, 28),
new Annotations.Point(13.2, 27),
new Annotations.Point(13.2, 26),
new Annotations.Point(15.2, 24),
new Annotations.Point(17.2, 23),
new Annotations.Point(22.2, 19)
]);
annotationManager.addAnnotation(ink);使用以下方法向 PDF 文档页面添加标准和文本图章。
// 标准图章。
const standardStamp = new Annotations.StampAnnotation({
PageNumber: 1,
X: 205,
Y: 379,
Width: 230,
Height: 52
});
standardStamp.Icon = 'NotApproved';
annotationManager.addAnnotation(standardStamp);
// 文本图章。
const textStamp = new Annotations.StampAnnotation({
PageNumber: 1,
X: 220,
Y: 367,
Width: 100,
Height: 45
});
textStamp.setStampText('REVISED', {
date: '28/07/2023 07:28:28',
color: 'red', // 'white' | 'red' | 'green' | 'blue'
shape: 'rectangle' // 'rectangle' | 'leftTriangle' | 'rightTriangle' | 'none'
});
annotationManager.addAnnotation(textStamp);
// 图片图章
const res = await fetch('https://example.com/image.png');
const imageBlob = await res.blob();
const reader = new FileReader();
reader.onloadend = async () => {
const imageBase64 = reader.result;
const imageStamp = new Annotations.StampAnnotation({
PageNumber: 1,
X: 220,
Y: 367,
Width: 100,
Height: 45
});
await imageStamp.setImageData(imageBase64);
annotationManager.addAnnotation(imageStamp);
};
reader.readAsDataURL(imageBlob);