在 Node.js 中为 PDF 添加背景
Contents
[
Hide
]
以下代码片段展示了如何使用 AsposePdfAddBackgroundImage 函数在 Node.js 中为 PDF 页面添加背景图像。
请检查以下代码片段,以在 Node.js 环境中添加背景图像。
CommonJS:
- 调用
require并导入asposepdfnodejs模块 作为AsposePdf变量。 - 指定将添加背景图像的 PDF 文件名称。
- 调用
AsposePdf作为 Promise 并执行添加背景图像的操作。如果成功,则接收对象。 - 调用函数 AsposePdfAddBackgroundImage.
- 将背景图像添加到 PDF 文件中。如果 ‘json.errorCode’ 为 0,则操作结果保存为 “ResultAddBackgroundImage.pdf”。如果 json.errorCode 参数不为 0,且相应地在您的文件中出现错误,错误信息将包含在 ‘json.errorText’ 中。
const AsposePdf = require('asposepdfnodejs');
const pdf_file = 'Aspose.pdf';
const background_file = 'Aspose.jpg';
AsposePdf().then(AsposePdfModule => {
/*Add background image to a PDF-file and save the "ResultAddBackgroundImage.pdf"*/
const json = AsposePdfModule.AsposePdfAddBackgroundImage(pdf_file, background_file, "ResultAddBackgroundImage.pdf");
console.log("AsposePdfAddBackgroundImage => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
});
ECMAScript/ES6:
- 导入
asposepdfnodejs模块。 - 指定将添加背景图像的 PDF 文件名称。
- 初始化 AsposePdf 模块。如果成功,则接收对象。
- 调用函数 AsposePdfAddBackgroundImage.
- 将背景图像添加到 PDF 文件中。如果 ‘json.errorCode’ 为 0,则操作结果保存为 “ResultAddBackgroundImage.pdf”。如果 json.errorCode 参数不为 0,且相应地在您的文件中出现错误,错误信息将包含在 ‘json.errorText’ 中。
import AsposePdf from 'asposepdfnodejs';
const AsposePdfModule = await AsposePdf();
const pdf_file = 'Aspose.pdf';
const background_file = 'Aspose.jpg';
/*Add background image to a PDF-file and save the "ResultAddBackgroundImage.pdf"*/
const json = AsposePdfModule.AsposePdfAddBackgroundImage(pdf_file, background_file, "ResultAddBackgroundImage.pdf");
console.log("AsposePdfAddBackgroundImage => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);