HTML JPG PDF XML DOCX
  Product Family
PDF

Extract Attachments from PDF in Node.js SDK

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

Get Started

How to extract attachments from 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 extract attachments using Node.js

Aspose.PDF Cloud Node.js developers can easily extract, append & replace 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 extracting attachment actions for PDF document
  5. Download the result if need it
 

This sample code shows extracting attachments of 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 getAllAttachments() {
        const LOCAL_PATH = "C:\\Samples\\";
        const LOCAL_FILE_NAME = "C:\\Samples\\Attachments\\sample_attachment.pdf";
        const STORAGE_FILE_NAME = "sample_attachment.pdf";
        try {
            const pdfApi = new PdfApi(credentials.id, credentials.key);
            const pdfData = await fs.readFile(LOCAL_FILE_NAME);
            await pdfApi.uploadFile(STORAGE_FILE_NAME, pdfData);

            const result = await pdfApi.getDocumentAttachments(STORAGE_FILE_NAME);

            if (result.body.code === 200 && result.body.attachments) {
                const attachmentList = result.body.attachments.list || [];
                if (!attachmentList.length) {
                    console.error("No attachments found.");
                    return;
                }

                const downloadTasks = attachmentList.map(async (attachment) => {
                    try {
                        const attachmentUrl = attachment.links[0].href;
                        const info = await pdfApi.getDocumentAttachmentByIndex(STORAGE_FILE_NAME, attachmentUrl);
                        const download = await pdfApi.getDownloadDocumentAttachmentByIndex(STORAGE_FILE_NAME, attachmentUrl);
                        await fs.writeFile(path.join(LOCAL_PATH, info.body.attachment.name), download.body);
                    } catch (error) {
                        console.error("Failed to download attachment:", error);
                    }
                });

                await Promise.all(downloadTasks);
            } else {
                console.error("Failed to retrieve attachments. Status:", result.statusCode);
            }
        } catch (error) {
            console.error("Error processing PDF attachments:", error);
        }
    }
 

Work with the Attachments in PDF via Node.js SDK

Getting attachments from a PDF is essential for accessing supplementary content, improving workflow efficiency, ensuring compliance, and enhancing security. It helps users retrieve critical files, automate document processing, and optimize document management in business, legal, and technical applications. Get the Attachments from 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.