Rasterex APINPM Package/docs/components/pdf/page-replace

Page Replace

Replace pages in the current PDF with pages from another PDF using page-replace with the SDK.

Overview

The page-replace action swaps pages in the current PDF with pages from another PDF file. Like insert, the parent provides a File object and the source pages to import.

This action is used when the target pages in the current document should be replaced, not just supplemented.

  • Required fields: pageRange, file, and selectedPages.
  • Use pageRange to identify the page or pages being replaced in the current file.
  • Use selectedPages to identify the source pages being inserted in their place.

Example

Replace page index 4 in the current PDF with page index 0 from a source PDF.

typescript
const sourceFile = fileInput.files?.[0];

if (sourceFile) {
  await viewer.documents.manipulatePages({
    action: "page-replace",
    pageRange: [[4]],
    file: sourceFile,
    selectedPages: [[0]]
  });
}
Example

Vanilla TS Integration

Example showing how to replace pages programmatically:

typescript
import { createViewer } from "@rasterex/viewer";

const viewer = createViewer({
  container: "#viewer"
});

await viewer.mount();
await viewer.ready();

async function handleFileReplace(sourceFile: File, targetIndex: number): Promise<void> {
  try {
    await viewer.documents.manipulatePages({
      action: "page-replace",
      pageRange: [[targetIndex]],
      file: sourceFile,
      selectedPages: [[0]]
    });
    console.log("Pages replaced successfully");
  } catch (error) {
    console.error("Replacement failed:", error);
  }
}
Vanilla TS Integration

Live Preview

Open the focused demo to test page replacement from another PDF file.

PDF Page Replace Demo

Focused live demo for page-replace.

Preview opens in a large modal for zoom-friendly review.