Working with Pages
Add, remove, reorder, copy, merge, and split PDF pages.
Working with Pages
This guide covers all operations you can perform on PDF pages.
Get Pages
All Pages
const pages = await pdf.getPages();
console.log(`Document has ${pages.length} pages`);Single Page (Zero-Indexed)
const firstPage = await pdf.getPage(0);
const lastPage = await pdf.getPage(pdf.getPageCount() - 1);Page Properties
const page = await pdf.getPage(0);
// Dimensions in points (1 inch = 72 points)
const { width, height } = page;
// Rotation (0, 90, 180, or 270)
const rotation = page.rotation;
// Page index
console.log(`Page ${page.index + 1} of ${pdf.getPageCount()}`);Add Pages
Empty Page
// Add to end with default size (Letter)
const page = pdf.addPage();
// Add with specific size
const a4Page = pdf.addPage({ size: "a4" });
// Add at specific position (0-indexed)
const insertedPage = pdf.addPage({ insertAt: 0 }); // First pageCustom Size
// Size in points
const customPage = pdf.addPage({
width: 400,
height: 600,
});
// Landscape A4
const landscape = pdf.addPage({
size: "a4",
orientation: "landscape",
});Remove Pages
// Remove by index
pdf.removePage(0); // Remove first page
// Remove multiple pages (indices shift after each removal)
// To remove pages 0, 1, 2 - remove from end first:
pdf.removePage(2);
pdf.removePage(1);
pdf.removePage(0);Reorder Pages
// Move page from index 0 to index 2
pdf.movePage(0, 2);
// Reverse all pages
const pageCount = pdf.getPageCount();
for (let i = 0; i < Math.floor(pageCount / 2); i++) {
pdf.movePage(i, pageCount - 1 - i);
}Copy Pages Between Documents
When you copy pages, all resources (fonts, images, etc.) are automatically copied with them.
import { PDF } from "@libpdf/core";
const sourceBytes = await readFile("source.pdf");
const sourcePdf = await PDF.load(sourceBytes);
const destPdf = PDF.create();
// Copy specific pages (copies and inserts automatically)
await destPdf.copyPagesFrom(sourcePdf, [0, 2, 4]);
await writeFile("dest.pdf", await destPdf.save());Copy All Pages
const allIndices = Array.from(
{ length: sourcePdf.getPageCount() },
(_, i) => i
);
await destPdf.copyPagesFrom(sourcePdf, allIndices);Merge PDFs
Combine multiple PDFs into one:
import { PDF } from "@libpdf/core";
const pdf1Bytes = await readFile("doc1.pdf");
const pdf2Bytes = await readFile("doc2.pdf");
const pdf3Bytes = await readFile("doc3.pdf");
const merged = await PDF.merge([pdf1Bytes, pdf2Bytes, pdf3Bytes]);
await writeFile("merged.pdf", await merged.save());Merge with Options
const merged = await PDF.merge([pdf1Bytes, pdf2Bytes], {
// Include annotations from source documents
includeAnnotations: true,
});Split / Extract Pages
Extract Page Range
const extracted = await pdf.extractPages([0, 1, 2]); // First 3 pages
await writeFile("first-three.pdf", await extracted.save());Split into Single-Page PDFs
for (let i = 0; i < pdf.getPageCount(); i++) {
const singlePage = await pdf.extractPages([i]);
await writeFile(`page-${i + 1}.pdf`, await singlePage.save());
}Split by Page Count
async function splitEvery(pdf: PDF, pagesPerChunk: number): Promise<PDF[]> {
const chunks: PDF[] = [];
for (let i = 0; i < pdf.getPageCount(); i += pagesPerChunk) {
const indices = [];
for (let j = i; j < Math.min(i + pagesPerChunk, pdf.getPageCount()); j++) {
indices.push(j);
}
chunks.push(await pdf.extractPages(indices));
}
return chunks;
}
const chunks = await splitEvery(pdf, 10); // 10 pages per fileEmbed Pages as XObjects
You can embed a page from another PDF as a Form XObject, useful for watermarks, backgrounds, or page overlays.
const backgroundPdf = await PDF.load(backgroundBytes);
// Embed the page as an XObject (pass PDF and page index)
const embeddedPage = await pdf.embedPage(backgroundPdf, 0);
// Draw it on each page
for (const page of await pdf.getPages()) {
page.drawPage(embeddedPage, {
x: 0,
y: 0,
// Scale to fit
width: page.width,
height: page.height,
});
}Common Issues
| Issue | Solution |
|---|---|
| Pages appear blank after copy | Use copyPagesFrom() which copies resources automatically |
| Wrong page order after merge | Check source document page indices |
| Memory issues with large PDFs | Process pages in batches, save incrementally |
| Fonts look different after copy | Ensure source PDF has embedded fonts |
