Page Operations
This section covers core page-level operations including extracting pages, removing pages, and reordering page sequences.
1. Extract Pages
Compile specific pages into a new PDF document.
Signature
function extractPages(
file: File | Blob | ArrayBuffer | Uint8Array,
pagesToExtract: number[]
): Promise<Uint8Array>
Example
import { extractPages } from "@manojcoder/pdf-utils";
async function runExtraction(file: File) {
const pages = [0, 1, 3]; // Extracts pages 1, 2, and 4
const bytes = await extractPages(file, pages);
const blob = new Blob([bytes], { type: "application/pdf" });
// Process blob...
}
2. Remove Pages
Delete specific page indices from the document.
Signature
function removePages(
file: File | Blob | ArrayBuffer | Uint8Array,
pagesToRemove: number[]
): Promise<Uint8Array>
Example
import { removePages } from "@manojcoder/pdf-utils";
async function runRemoval(file: File) {
const pages = [2]; // Deletes the third page (index 2)
const bytes = await removePages(file, pages);
const blob = new Blob([bytes], { type: "application/pdf" });
// Process blob...
}
3. Reorder Pages
Rearrange page layouts by passing the new sequence array.
Signature
function reorderPages(
file: File | Blob | ArrayBuffer | Uint8Array,
newOrder: number[]
): Promise<Uint8Array>
Example
import { reorderPages } from "@manojcoder/pdf-utils";
async function runReorder(file: File) {
// Reorders a 3-page PDF to sequence: page 3, page 1, page 2
const sequence = [2, 0, 1];
const bytes = await reorderPages(file, sequence);
const blob = new Blob([bytes], { type: "application/pdf" });
// Process blob...
}
Notice: All indices are 0-based. An out-of-bounds index will throw a validation error.