Add background to PDF in Node.js
Contents
[
Hide
]
The following code snippets shows how to add a background image to PDF pages using the AsposePdfAddBackgroundImage function in Node.js.
Please check the following code snippet in order to add a background image in Node.js environment.
CommonJS:
- Call
require
and importasposepdfnodejs
module asAsposePdf
variable. - Specify the name of the PDF file in which the background image will be added.
- Call
AsposePdf
as Promise and perform the operation for adding background image. Receive the object if successful. - Call the function AsposePdfAddBackgroundImage.
- Add background image to a PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultAddBackgroundImage.pdf”. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘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:
- Import the
asposepdfnodejs
module. - Specify the name of the PDF file in which the background image will be added.
- Initialize the AsposePdf module. Receive the object if successful.
- Call the function AsposePdfAddBackgroundImage.
- Add background image to a PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultAddBackgroundImage.pdf”. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘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);