HTML JPG PDF XML DOCX
  Product Family
PDF

在 Node.js 云 API 中裁剪 PDF

用于使用云端 Node.js SDK 处理 PDF 文档裁剪的 API

Get Started

如何通过 Node.js SDK 裁剪 PDF

要裁剪 PDF,我们将使用 Aspose.PDF Cloud Node.js SDK。这个云 SDK 帮助 Node.js 程序员使用 Node.js 编程语言通过 Aspose.PDF REST API 开发基于云的 PDF 创建、注释、编辑和转换应用程序。一旦您拥有 App SID 和密钥,您就可以使用 Aspose.PDF Cloud Node.js SDK。

包管理器控制台命令


     
    npm install asposepdfcloud --save
     
     

通过云端 Node.js 裁剪 PDF 的步骤

Aspose.PDF Cloud 开发人员可以轻松地在几行代码中加载和裁剪 PDF。

  1. 初始化 Aspose PDF API
  2. 上传原始文档
  3. 获取页面大小信息
  4. 将页面提取为图像(完整原始页面)
  5. 创建具有指定裁剪尺寸的新 PDF
  6. 将裁剪的图像部分插入新 PDF
  7. 如果成功,下载结果文件
 

使用 Node.js 裁剪 PDF


    import { PdfChangeLayoutHelper, pdfApi } from "./changeLayoutHelper.js";
    export { PdfCropPage };

    const PdfCropPage = {
        async cropPage(document, pageNumber, llx, lly, width, height, outputDocument, localFolder, tempFolder) {
            if ( pdfApi ) {
                await PdfChangeLayoutHelper.uploadDocument(document, localFolder, tempFolder);  

                var pageSie = await PdfChangeLayoutHelper.getPageInfo(document, pageNumber, tempFolder);

                const imageFile = await PdfChangeLayoutHelper.extractPdfPage(document, pageNumber, pageSie.width, pageSie.height, localFolder, tempFolder);
                const newPdf = await PdfChangeLayoutHelper.createPdfDocument(outputDocument, width, height, tempFolder);
                if (newPdf.body.code != 200) {
                    console.error("cropPage(): Failed to create new PDF document!");
                    return;
                }

                const response = await PdfChangeLayoutHelper.insertPageAsImage(outputDocument, imageFile, llx, lly, tempFolder);

                if (response.body.code == 200) {
                    console.log("cropPage(): Page successfully cropped.");
                    await PdfChangeLayoutHelper.downloadResult(outputDocument, localFolder, tempFolder, "cropped_")
                }
                else
                    console.error("cropPage(): Can't crop pdf document page!")
            }
        }
    }
 

通过我们的 Node.js 库,您可以

使用 Aspose.PDF Cloud Node.js SDK 裁剪 PDF 文档。

  • 以文本或图像格式添加 PDF 文档的页眉和页脚。
  • 向 PDF 文档添加表格和印章(文本或图像)。
  • 将多个 PDF 文档附加到现有文件。
  • 处理 PDF 附件、注释和表单字段。
  • 对 PDF 文档应用加密或解密并设置密码。
  • 从页面或整个 PDF 文档中删除所有印章和表格。
  • 通过其 ID 从 PDF 文档中删除特定印章或表格。
  • 替换 PDF 页面或整个文档中的单个或多个文本实例。
  • 全面支持将 PDF 文档转换为各种其他文件格式。
  • 提取 PDF 文件的各种元素并优化 PDF 文档。
  • 您可以尝试我们的免费应用程序来测试功能。