Editing pages and documents
Merge, split, insert, delete, rotate, reorder and crop pages, set page backgrounds, and edit text and images on the device with NexPDF for React Native.
License module
Page and content editing needs the editor module (Startup tier and above). Inserting pictures as
pages needs no module.
Page indices start at 0. Get the result as base64 with saveBytes(), or saveLinearized() for a
"Fast Web View" file.
Merge and split
const a = await NexPdf.open(aBase64);
const b = await NexPdf.open(bBase64);
await a.appendDocument(b); // a now has the pages of a, then b
const merged = await a.saveBytes();
const firstThree = await a.extractPages([0, 1, 2]);
const part1 = await firstThree.saveBytes();
await firstThree.close();Insert, duplicate, delete and reorder
await doc.addBlankPage(); // appends a blank page
await doc.duplicatePage(0); // inserts a copy of page 0 after it
await doc.movePage(3, 0); // moves page 3 to the front
await doc.deletePage(5);
await doc.reversePages();
await doc.insertImagePages(2, jpegBase64); // one page per picture, before page 2Rotate and crop
await doc.rotatePage(0, 90); // multiples of 90 degrees
await doc.cropPage(1, 36, 36, 576, 756); // new crop box in PDF pointsPage backgrounds
await doc.setBackground({ color: '#fbf4e2', pages: [] }); // every page
await doc.removePageBackground(null);Pass a picture as RGB pixels (width x height x 3 bytes, base64),
setBackground(options, { rgbBase64, width, height }), and fit (cover, stretch or center),
scale and opacity in the options.
Edit page text and images
pageContentJson(page) lists the text lines and images of a page with an id for each:
const items = JSON.parse(await doc.pageContentJson(0));
const line = items.find((i: any) => i.kind === 'text' && i.text.includes('Draft'));
await doc.editText(0, line.id, JSON.stringify({ text: 'Final', color: '#111827' }));
await doc.addPageText(0, JSON.stringify({ text: 'Approved', x: 72, y: 60, size: 14, color: '#16A34A', bold: true }));Ids stay valid until the next edit of that page, so read the content again after each change. For
images use transformImage(page, id, llx, lly, urx, ury) to move or resize,
replaceImage(page, id, rgb, width, height) to swap the picture and deleteContent(page, id) to
remove it.
Related
- Document structure: metadata, bookmarks, page labels and attachments
- Compare and optimise: compare versions and make files smaller