Skip to content

PDF 生成模板编辑器

开源可视化 PDF 生成引擎,支持自定义模板,并提供面向开发者的 API,灵活构建文档生成流程。

查看 GitHub

文档编辑 ​

本示例展示如何操作 PDF 文档的多个页面,包括插入、替换、提取、旋转等。

javascript
// 导入 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, createDocument, PageRotation } = instance.Core;
  documentViewer.addEventListener('documentLoaded', async () => {
    console.log('ComPDFKit Web Demo loaded');

    const doc = documentViewer.getDocument();

    // 示例 1:插入空白页
    await doc.insertBlankPages([1], 595, 842);

    // 示例 2:插入 PDF 页面
    const insertDocument = await createDocument(file, { password: '' }); // File 对象。
    await doc.insertPages(insertDocument, undefined, 1); // 插入该 PDF 的所有页面。

    // 示例 3:删除页面
    await doc.removePages([1]);

    // 示例 4:旋转页面
    await doc.rotatePages([1], PageRotation.E_90);

    // 示例 5:替换页面
    await doc.removePages([1]);
    const replaceDocument = await createDocument(file, { password: '' }); // File 对象。
    await doc.insertPages(replaceDocument, undefined, 1); // 插入该 PDF 的所有页面。

    // 示例 6:提取页面
    const extractedData = await doc.extractPages('1,3-4'); // 将这些页面提取为一个新的 PDF。
    const extractedFiles = await doc.extractPages('1,3-4', undefined, { separateFile: true }); // 每页单独一个文件。

    // 示例 7:移动页面
    await doc.movePages([1], 3); // 将第 1 页移动到第 3 页之前。

    // 示例 8:复制页面
    await doc.copyPages([1]);
  })
})