Copy Pages
ComPDF React Native SDK provides a page copy API for duplicating a page in the current PDF document and inserting the copied page at a target position. This is useful for page reordering, template reuse, and quickly creating similar pages.
API Overview
The page copy API is available on CPDFDocument:
copyPage(pageIndex: number, insertIndex: number): Promise<boolean>;Parameter description:
| Parameter | Description |
|---|---|
pageIndex | The zero-based index of the source page to copy |
insertIndex | The zero-based insertion index for the copied page; use -1 to append to the end of the document |
Return value:
true: the page was copied and inserted successfully.false: the page index is invalid, the insertion index is invalid, or the native platform failed to copy the page.
Copy a Specific Page
Use copyPage(pageIndex, insertIndex) to duplicate a page in the current document and insert the copy at the desired position.
const result = await pdfReaderRef.current?._pdfDocument.copyPage(0, 1);
console.log("Copy page result:", result);This example copies the first page and inserts the copy before the second page.
If insertIndex is set to -1, the copied page is appended to the end of the document:
const result = await pdfReaderRef.current?._pdfDocument.copyPage(0, -1);Use With CPDFReaderView
In a reader page, call the same API through the _pdfDocument property of a CPDFReaderView instance. After a successful copy, refresh the reader view so the new page count is reflected.
import { CPDFReaderView } from "@compdfkit_pdf_sdk/react_native";
async function copyCurrentPage(reader: CPDFReaderView, pageIndex: number) {
const result = await reader._pdfDocument.copyPage(pageIndex, -1);
if (result) {
await reader.reloadPagesPreservingPosition();
}
}This is a good fit for custom toolbars, page menus, or batch page operations.
Complete Example
The following example duplicates the first page in a loaded reader and appends the copy to the end of the document.
import { useRef } from "react";
import { Button } from "react-native";
import { CPDFReaderView } from "@compdfkit_pdf_sdk/react_native";
export default function CopyPageExample() {
const pdfReaderRef = useRef<CPDFReaderView>(null);
const duplicateFirstPage = async () => {
const reader = pdfReaderRef.current;
if (!reader) {
return;
}
const result = await reader._pdfDocument.copyPage(0, -1);
if (!result) {
throw new Error("Copy page failed.");
}
await reader.reloadPagesPreservingPosition();
console.log("Page copied successfully.");
};
return (
<>
<CPDFReaderView
ref={pdfReaderRef}
document="file:///path/to/sample.pdf"
style={{ flex: 1 }}
/>
<Button title="Copy First Page" onPress={duplicateFirstPage} />
</>
);
}Notes
pageIndexmust point to an existing page in the current document, otherwise the API returnsfalse.insertIndexsupports0..pageCount;-1means append to the end of the document.- Page copy changes the document page count, so refresh the reader state if you use this API in a reader view.
- If the document is empty, the page index is invalid, or the native SDK rejects the current document state, the API returns
false. - If you need to duplicate multiple pages, call
copyPagemultiple times in the order required by your business logic.