Add Images to PDF document via JavaScript via C++

Insert images to PDF document programmatically using Aspose.PDF for JavaScript via C++ Library

Add Image to PDF Document Using JavaScript via C++ Library

Adding images to a PDF can enhance the visual appeal of the document. Images can be used to illustrate concepts, provide examples, or showcase products, making the PDF more engaging and informative. In order to add an image into PDF file, we’ll use Aspose.PDF for JavaScript via C++ is a easy and secure toolkit used to work with PDF directly in the web browser. To install and use Aspose.PDF for JavaScript via C++ extract files from the ZIP archive.

Add Image to PDF using JavaScript via C++


You need Aspose.PDF for JavaScript via C++ to try the code in your environment.

  1. Create a Document object and open the input PDF document.
  2. Get the page you want to add an image.
  3. Add the image into the page’s Resources collection.
  4. Use the GSave operator to save the current graphical state.
  5. Use ConcatenateMatrix operator to specify where the image is to be placed.
  6. Use the Do operator to draw the image on the page.
  7. Use GRestore operator to save the updated graphical state.
  8. Save the PDF file.

Add Image to PDF document - JavaScript via C++

This sample code shows how to add Images into PDF page - JavaScript via C++


    var ffileImage = function (e) {
        const file_reader = new FileReader();
        /*set the image filename*/
        fileImage = e.target.files[0].name;
        file_reader.onload = (event) => {
        /*prepare(save) the image file from BLOB*/
        AsposePdfPrepare(event.target.result, fileImage);
        };
        file_reader.readAsArrayBuffer(e.target.files[0]);
    };

    var ffileAddImage = function (e) {
        const file_reader = new FileReader();
        file_reader.onload = (event) => {
        /*add the image file to end a PDF-file and save the "ResultImage.pdf"*/
        const json = AsposePdfAddImage(event.target.result, e.target.files[0].name, fileImage, "ResultImage.pdf");
        if (json.errorCode == 0) document.getElementById('output').textContent = json.fileNameResult;
        else document.getElementById('output').textContent = json.errorText;
        /*make a link to download the result file*/
        DownloadFile(json.fileNameResult, "application/pdf");
        };
        file_reader.readAsArrayBuffer(e.target.files[0]);
    };