HTML JPG PDF XML DOCX
  Product Family
PDF

Crop PDF in Node.js Cloud API

API for working with cropping PDF documents using Cloud Node.js SDK

Get Started

How to Crop PDF via Node.js SDK

To crop PDF, we’ll use Aspose.PDF Cloud Node.js SDK. This Cloud 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. Once you have the App SID & key, you are ready to give the Aspose.PDF Cloud Node.js SDK.

Package Manager Console Command


     
    npm install asposepdfcloud --save
     
     

Steps to crop PDF via Cloud Node.js

Aspose.PDF Cloud developers can easily load & crop PDF in just a few lines of code.

  1. Initialize the Aspose PDF API
  2. Upload original document
  3. Get page size info
  4. Extract page as image (full original page)
  5. Create a new PDF with the specified cropped size
  6. Insert cropped image portion into new PDF
  7. Download resulting file if success
 

Crop PDF using Node.js


    import { PdfChangeLayoutHelper, pdfApi } from "./changeLayoutHelper.js";
    export { PdfCropPage };

    const PdfCropPage = {
        async cropPage(document, pageNumber, llx, lly, width, height, outputDocument, localFolder, tempFolder) {
            if ( pdfApi ) {
                await PdfChangeLayoutHelper.uploadDocument(document, localFolder, tempFolder);  

                var pageSie = await PdfChangeLayoutHelper.getPageInfo(document, pageNumber, tempFolder);

                const imageFile = await PdfChangeLayoutHelper.extractPdfPage(document, pageNumber, pageSie.width, pageSie.height, localFolder, tempFolder);
                const newPdf = await PdfChangeLayoutHelper.createPdfDocument(outputDocument, width, height, tempFolder);
                if (newPdf.body.code != 200) {
                    console.error("cropPage(): Failed to create new PDF document!");
                    return;
                }

                const response = await PdfChangeLayoutHelper.insertPageAsImage(outputDocument, imageFile, llx, lly, tempFolder);

                if (response.body.code == 200) {
                    console.log("cropPage(): Page successfully cropped.");
                    await PdfChangeLayoutHelper.downloadResult(outputDocument, localFolder, tempFolder, "cropped_")
                }
                else
                    console.error("cropPage(): Can't crop pdf document page!")
            }
        }
    }
 

With our Node.js library you can

Crop PDF documents with Aspose.PDF Cloud Node.js SDK.

  • 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 test the functionality.