PDF 生成模板编辑器
开源可视化 PDF 生成引擎,支持自定义模板,并提供面向开发者的 API,灵活构建文档生成流程。
本示例展示如何导出和导入注释,注释导出的文件格式为 XFDF。
// 导入 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 } = instance.Core;
documentViewer.addEventListener('documentLoaded', async () => {
console.log('ComPDFKit Web Demo loaded');
// 示例 1:导出注释
if (await ExportAnnotation(instance.Core)) {
console.log('Export annotaitons done.');
} else {
console.log('Export annotaitons failed.');
}
// 示例 2:导入注释
ImportAnnotaiton(instance.Core);
})
})
// 将文档中的注释导出为 XFDF。
async function ExportAnnotation({ annotationManager }) {
/**
* 将文档中的注释导出为 XFDF 字符串。
*
* @returns {Promise<string>} XFDF 字符串。
*/
const xfdfString = await annotationManager.exportAnnotations();
if (!xfdfString) {
return false;
}
return true;
}
// 将 XFDF 导入文档。
async function ImportAnnotaiton({ annotationManager }) {
const file = await fetch('test.xfdf');
await annotationManager.importAnnotations(await file.blob());
}