How to duplicate a PDF page using JavaScript
Table of contents
Duplicate PDF pages using the duplicate PDF page JavaScript API. Create a free account, get API credentials, and implement page duplication using Node.js with axios and form-data. Combine with 30+ other API tools for watermarking, merging, and flattening.
This tutorial shows you how to duplicate PDF pages using our duplicate PDF page JavaScript API. The free plan includes 200 credits. Different operations consume different amounts of credits. Create a free account(opens in a new tab) to get your API key.
Why duplicate PDF pages?
Duplicating PDF pages is important for document workflows that require processing the same content differently. Common use cases include:
- Multi-recipient workflows — Apply different watermarks or annotations to the same page for different recipients, e.g. watermarked versions for clients and clean versions for internal use.
- Backup and versioning — Create page-level backups before performing destructive operations like flattening or redaction, preserving the original state.
- Template-based generation — Build complex documents by duplicating template pages and customizing each copy with recipient-specific data.
- Testing and validation — Duplicate pages to test different processing operations (compression, OCR, conversion) while keeping the original intact.
- Workflow automation — Process batches of pages programmatically as part of your backend infrastructure, duplicating pages at specific positions for assembly workflows.
The duplicate PDF page API automates this process in your workflow.
Nutrient DWS Processor API
Page duplication is one of 30+ PDF API tools. Combine it with other tools to build processing workflows:
- Convert MS Office files and images to PDF, then duplicate specific pages
- Merge multiple PDFs and duplicate selected pages
- Apply watermarks, or split or flatten PDFs with page duplication at any step
Your account gives you access to all PDF API tools.
Step 1 — Creating a free account on Nutrient
Go to our website(opens in a new tab) to create your free account.

After creating your account, you’ll see your plan overview.
You start with 200 credits and can access all PDF API tools.
Step 2 — Obtaining the API key
After verifying your email, get your API key from the dashboard. Click API keys in the left menu to see your keys.

Copy the Live API key for the duplicate PDF page JavaScript API.
Step 3 — Setting up folders and files
Create a folder called duplicate_pdf and open it in VS Code. Create two subfolders: input_documents and processed_documents.
In the root folder, create processor.js for your code. Put your PDF files in the input_documents folder.
Your folder structure:
duplicate_pdf├── input_documents├── processed_documents└── processor.jsStep 4 — Installing dependencies
Install these dependencies:
- axios(opens in a new tab) — For making REST API calls
- Form-Data(opens in a new tab) — For creating form data
Install both:
npm install axiosnpm install form-dataStep 5 — Writing the code
Open processor.js and add this code:
// This code requires Node.js. Do not run this code directly in a web browser.
const axios = require("axios");const FormData = require("form-data");const fs = require("fs");
const formData = new FormData();formData.append( "instructions", JSON.stringify({ parts: [ { file: "document", pages: { start: 0, end: 0, }, }, { file: "document", }, { file: "document", pages: { start: -1, end: -1, }, }, ], }),);formData.append( "document", fs.createReadStream("input_documents/document.pdf"),);(async () => { try { const response = await axios.post( "https://api.nutrient.io/build", formData, { headers: formData.getHeaders({ Authorization: "Bearer YOUR_API_KEY_HERE", }), responseType: "stream", }, );
response.data.pipe(fs.createWriteStream("processed_documents/result.pdf")); } catch (e) { const errorString = await streamToString(e.response.data); console.log(errorString); }})();
function streamToString(stream) { const chunks = []; return new Promise((resolve, reject) => { stream.on("data", (chunk) => chunks.push(Buffer.from(chunk))); stream.on("error", (err) => reject(err)); stream.on("end", () => resolve(Buffer.concat(chunks).toString("utf8"))); });}Make sure to replace YOUR_API_KEY_HERE with your API key.
Code explanation
The code creates a FormData object with instructions for the API. These instructions duplicate the last page of the PDF and add it to the end of the file.
The fs.createReadStream function reads the input document.
axios makes the API call and saves the response to the processed_documents folder.
Output
Run the code:
node processor.jsAfter successful execution, you’ll find result.pdf in the processed_documents folder.
Final folder structure:
duplicate_pdf├── input_documents| └── document.pdf├── processed_documents| └── result.pdf└── processor.jsConclusion
You’ve learned how to duplicate PDF pages in JavaScript using our duplicate PDF page API.
You can add this functionality to existing applications. The same API token works for other operations: merging documents, adding watermarks, and more. Sign up(opens in a new tab) for a free trial.
FAQ
The API uses zero-based indexing, where page 0 is the first page. You can use negative indexing: -1 refers to the last page, -2 to the second-to-last page. The code example duplicates the first page (index 0) and last page (index -1).
Yes. Upload multiple files with different names (like 'document1', 'document2') and reference them in the parts array. Each part can specify which source document and which pages to include, allowing complex multidocument assembly workflows.
Each duplication operation consumes 1 credit, regardless of the number of pages duplicated or file size. The free account includes 200 credits, enabling you to process up to 200 PDF duplication requests.
Yes. Nutrient DWS Processor API supports chaining multiple operations in a single request. Use the actions array to duplicate pages and then apply operations like watermarking, flattening, or OCR to the resulting document.
Use the pages object with start and end properties. For example, {"start": 0, "end": 2} duplicates pages 0, 1, and 2. Omit the pages object to include all pages from the source document.
The API accepts PDF files as input and outputs PDF files. You can convert other formats (MS Office, images) to PDF first using the same API, and then duplicate specific pages in a single workflow.