Node.jsでPDFに背景を追加
Contents
[
Hide
]
以下のコードスニペットは、PDFページに背景画像を追加する方法を示しています AsposePdfAddBackgroundImage Node.jsの関数です。
Node.js 環境で背景画像を追加するために、以下のコードスニペットを確認してください。
CommonJS:
- 呼び出す
requireおよびインポートasposepdfnodejsモジュールとしてAsposePdf変数。 - 背景画像を追加する PDF ファイルの名前を指定します。
- 呼び出す
AsposePdfPromiseとして、背景画像を追加する操作を実行します。成功した場合はオブジェクトを受け取ります。 - 関数を呼び出します 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 "ResultBackgroundImage.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 "ResultBackgroundImage.pdf"*/
const json = AsposePdfModule.AsposePdfAddBackgroundImage(pdf_file, background_file, "ResultAddBackgroundImage.pdf");
console.log("AsposePdfAddBackgroundImage => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);