Extract Pages from PDF in Node.js SDK
Extract Pages from PDF Document using Cloud Node.js SDK
Get StartedHow to Extract PDF Pages via Node.js SDK
In order to Extract Pages from 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. Simply create an account at Aspose for Cloud and get your application information. 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 Extract Pages via Cloud Node.js
Aspose.PDF Cloud developers can easily Extract Pages from PDF in just a few lines of code.
- Create an object to connect to the Pdf.Cloud API
- Upload your document file
- Downloads the modified PDF document
- Retrieves information about all pages in the PDF
- Retrieves information about a specific page
- Displays information about the pages in the console
This sample code shows Extracting Pages using Node.js
import credentials from "./credentials.json" with { type: "json" };
import fs from 'node:fs/promises';
import path from 'node:path';
import { PdfApi } from "asposepdfcloud";
const configParams = {
LOCAL_FOLDER: "C:\\Samples\\",
PDF_DOCUMENT_NAME: "sample.pdf",
LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
PAGE_NUMBER: 2, // Your document page number...
};
const pdfApi = new PdfApi(credentials.id, credentials.key);
const pdfPages = {
async uploadDocument () {
const fileNamePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
const pdfFileData = await fs.readFile(fileNamePath);
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
},
async downloadResult () {
const changedPdfData = await pdfApi.downloadFile(configParams.PDF_DOCUMENT_NAME);
const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
await fs.writeFile(filePath, changedPdfData.body);
console.log("Downloaded: " + filePath);
},
async getPagesInfo () {
const resultPages = await pdfApi.getPages(configParams.PDF_DOCUMENT_NAME);
if (resultPages.body.code == 200 && resultPages.body.pages.list) {
this.showPages(resultPages.body.pages.list, "pages");
return resultPages.body.pages.list;
}
else
console.error("Unexpected error : can't get pages!!!");
},
async getPageInfo (pageNumber) {
const resultPages = await pdfApi.getPage(configParams.PDF_DOCUMENT_NAME, pageNumber);
if (resultPages.body.code == 200 && resultPages.body.page) {
this.showPages( [ resultPages.body.page ], "page");
return resultPages.body.page;
}
else
console.error("Unexpected error : can't get pages!!!");
},
showPages (pages, prefix) {
if (Array.isArray(pages) && pages.length > 0)
{
pages.forEach(function(page) {
console.log(prefix +" => id: '" + page.id + "', lLx: '" + page.rectangle.lLX + "', lLY: '" + page.rectangle.lLY + "', uRX: '" + page.rectangle.uRX + "', uRY: '" + page.rectangle.uRY + "'");
});
}
else
console.error("showPages() error: array of pages is empty!")
},
}
async function main() {
try {
await pdfPages.uploadDocument();
await pdfPages.getPagesInfo();
await pdfPages.getPageInfo(configParams.PAGE_NUMBER);
} catch (error) {
console.error("Error:", error.message);
}
}
Work with the Pages in PDF via Node.js SDK
Extracting pages from a PDF is a common task that serves various purposes across different contexts. This process involves selecting specific pages from a larger document to create a new, separate PDF file. Understanding the reasons behind this practice can help in effectively managing and utilizing PDF documents. Large PDF files can be cumbersome to share or store. By extracting only the necessary pages, users can create smaller, more manageable files. This is particularly useful when only a portion of the document is relevant for a specific purpose. For instance, removing unnecessary pages can significantly decrease the file size, making it easier to handle and distribute. Extracting pages allows users to repurpose content for different applications. For example, one might extract pages from a comprehensive report to create a standalone summary or to isolate specific data for analysis. This enables the reuse of existing content without the need to recreate information from scratch. Extract the Pages 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 Apps to test the functionality online.
- Learning Resources
- Documentation
- Source Code
- API References
- Product Support
- Free Support
- Paid Support
- Blog
- Why Aspose.PDF Cloud for Node.js?
- Customers List
- Security