PDF Generation Template Editor
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
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.
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.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);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.
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} />
</>
);
}pageIndex must point to an existing page in the current document, otherwise the API returns false.insertIndex supports 0..pageCount; -1 means append to the end of the document.false.copyPage multiple times in the order required by your business logic.