HTML JPG PDF XML DOCX
  Product Family
PDF

Add Attachments to PDF in Node.js SDK

API for adding attachments in PDF documents with Aspose.PDF Cloud Node.js SDK

Get Started

How to append attachments in PDF documents using Cloud Node.js SDK

For working with attachments in PDF documents, we’ll use Aspose.PDF Cloud Node.js SDK This Cloud SDK SDK assists Node.js programmers in developing cloud-based PDF creator, annotator, editor, and converter apps using Node.js programming language via Aspose.PDF REST API. Open NPM package manager, search for Aspose.PDF Cloud, and install. You may also use the following command from the Package Manager Console.

Package Manager Console Command


     
    npm install asposepdfcloud --save
     
     

Steps to add attachments using Node.js

Aspose.PDF Cloud Node.js developers can easily append attachments in PDF documents. Developers need just a few lines of code.

  1. Load your Application Secret and Key from the JSON file or set credentials in another way
  2. Create an object to connect to the Cloud API
  3. Upload your document file
  4. Perform the appending attachment actions for PDF document
  5. Download the result if need it
 

This sample code shows append file attachment annotation to PDF document using PDF Cloud Node.js SDK


    import credentials from "./credentials.json" with { type: "json" };
    import fs from 'node:fs/promises';
    import path from 'node:path';
    import { PdfApi } from "asposepdfcloud";
    import { AttachmentInfo } from "asposepdfcloud/src/models/attachmentInfo.js";

    async function addAttachment() {
        const LOCAL_FILE_NAME = "C:\\Samples\\Attachments\\sample_attachment.pdf";
        const STORAGE_FILE_NAME = "sample_attachment.pdf";
        const LOCAL_ATTACHMENT_FILE_NAME = "C:\\Samples\\Attachments\\file_example_MP3_700KB.mp3";
        const STORAGE_ATTACHMENT_FILE_NAME = "file_example_MP3_700KB.mp3";
        const RESULT_FILE_NAME = "C:\\Samples\\Attachments\\sample_attachment.pdf";
        try {
            const pdfApi = new PdfApi(credentials.id, credentials.key);
            let buffer = await fs.readFile(LOCAL_FILE_NAME);
            let uploadResult = await pdfApi.uploadFile(STORAGE_FILE_NAME, buffer);
            console.log(`Uploaded: ${uploadResult.body.errors.length === 0}`);
            buffer = await fs.readFile(LOCAL_ATTACHMENT_FILE_NAME);
            uploadResult = await pdfApi.uploadFile(STORAGE_ATTACHMENT_FILE_NAME, buffer);
            console.log(`Uploaded: ${uploadResult.body.errors.length === 0}`);

            const attachment = new AttachmentInfo();
            attachment.name = STORAGE_ATTACHMENT_FILE_NAME;
            attachment.path = STORAGE_ATTACHMENT_FILE_NAME;
            attachment.description = "An example of MP3 file";
            attachment.mimeType = "audio/mpeg";

            const appendResult = await pdfApi.postAddDocumentAttachment(STORAGE_FILE_NAME, attachment, null, null);

            if (appendResult.body.code == 200) {
                const downloadResult = await pdfApi.downloadFile(STORAGE_FILE_NAME);
                await fs.writeFile(RESULT_FILE_NAME, downloadResult.body);
            }
            else
                console.log("Unexpected error : can't download attachments");

        } catch (error) {
            console.error(error.message);
        }
    }
 

Work with the Attachments in PDF via Node.js SDK

Adding attachments to a PDF improves document organization, accessibility, and usability. It ensures all relevant files are stored in one place, streamlines workflows, enhances collaboration, and meets security and legal requirements, making it a valuable feature for business, legal, academic, and multimedia applications. Add the AcroForms into PDF documents with Aspose.PDF Cloud Node.js SDK.

With our Node.js library you can

  • Add PDF document’s header & footer in text or image format.
  • Add tables & stamps (text or image) to PDF documents.
  • Append multiple PDF documents to an existing file.
  • Work with PDF attachments, annotations, & form fields.
  • Apply encryption or decryption to PDF documents & set a password.
  • Delete all stamps & tables from a page or entire PDF document.
  • Delete a specific stamp or table from the PDF document by its ID.
  • Replace single or multiple instances of text on a PDF page or from the entire document.
  • Extensive support for converting PDF documents to various other file formats.
  • Extract various elements of PDF files & make PDF documents optimized.
  • You can try out our free App to add the AcroForms into PDF files online and test the functionality.