Editing pages and documents
Let users edit pages and content in the native reader, or merge, split, insert, delete, rotate, reorder and crop pages, set page backgrounds and edit text and images from Dart through the NexPDF C API, with NexPDF for Flutter.
License module
Page and content editing needs the editor module (Startup tier and above). Inserting pictures as
pages needs no module.
In the reader
The native reader has a page editor (reorder, rotate, delete, insert) and a
content mode that edits the text and images of a page. Offer it with
'modeConfig': {'availableModes': ['viewer', 'content']}.
From code
The editing functions have no Dart method. Call them
through dart:ffi on doc.handle, then save with
doc.save(). Page indices start at 0.
import 'dart:ffi';
import 'nexpdf_ffi.dart';
typedef _PageOpNative = Int32 Function(Pointer<Void>, Size, Int32);
typedef _PageOp = int Function(Pointer<Void>, int, int);
// int32_t nexpdf_rotate_page(NexPdfDocument *doc, size_t page_index, int32_t degrees);
final rotatePage = nexpdfLib.lookupFunction<_PageOpNative, _PageOp>('nexpdf_rotate_page');
// int32_t nexpdf_delete_page(NexPdfDocument *doc, size_t page_index);
final deletePage = nexpdfLib.lookupFunction<Int32 Function(Pointer<Void>, Size),
int Function(Pointer<Void>, int)>('nexpdf_delete_page');
// int32_t nexpdf_move_page(NexPdfDocument *doc, size_t from, size_t to);
final movePage = nexpdfLib.lookupFunction<Int32 Function(Pointer<Void>, Size, Size),
int Function(Pointer<Void>, int, int)>('nexpdf_move_page');
// int32_t nexpdf_append_document(NexPdfDocument *doc, const NexPdfDocument *other);
final appendDocument = nexpdfLib.lookupFunction<Int32 Function(Pointer<Void>, Pointer<Void>),
int Function(Pointer<Void>, Pointer<Void>)>('nexpdf_append_document');
nexpdfCall(() => rotatePage(doc.handle, 0, 90)); // multiples of 90 degrees
nexpdfCall(() => movePage(doc.handle, 3, 0)); // moves page 3 to the front
nexpdfCall(() => deletePage(doc.handle, 5));
final other = sdk.open(otherBytes);
nexpdfCall(() => appendDocument(doc.handle, other.handle)); // doc now has its pages, then other's
other.dispose();
final edited = doc.save();Split
nexpdf_extract_pages returns a new native document that the Dart NexPdfDocument cannot wrap.
Save it with nexpdf_save_bytes and free it with nexpdf_free:
import 'package:ffi/ffi.dart';
// NexPdfDocument *nexpdf_extract_pages(const NexPdfDocument *doc, const uint32_t *indices, size_t count);
final extractPages = nexpdfLib.lookupFunction<Pointer<Void> Function(Pointer<Void>, Pointer<Uint32>, Size),
Pointer<Void> Function(Pointer<Void>, Pointer<Uint32>, int)>('nexpdf_extract_pages');
final saveBytes = nexpdfLib.lookupFunction<NexPdfBytes Function(Pointer<Void>),
NexPdfBytes Function(Pointer<Void>)>('nexpdf_save_bytes');
final free = nexpdfLib.lookupFunction<Void Function(Pointer<Void>), void Function(Pointer<Void>)>('nexpdf_free');
final firstThree = using((a) {
final indices = a<Uint32>(3)..asTypedList(3).setAll(0, [0, 1, 2]);
final part = nexpdfCall(() => extractPages(doc.handle, indices, 3));
try {
return takeBytes(saveBytes(part));
} finally {
free(part);
}
});Other editing functions
| C function | Does |
|---|---|
nexpdf_add_blank_page, nexpdf_duplicate_page, nexpdf_reverse_pages | Add, copy and reverse pages |
nexpdf_insert_image_pages(doc, index, data, len) | One page per JPEG, PNG or TIFF picture, before index |
nexpdf_crop_page(doc, page, llx, lly, urx, ury) | A new crop box in PDF points |
nexpdf_page_content_json, nexpdf_edit_text, nexpdf_add_page_text, nexpdf_transform_image, nexpdf_replace_image, nexpdf_delete_content | Edit the text and images of a page |
nexpdf_set_page_background, nexpdf_remove_page_background | Page backgrounds |
Related
- Document structure: metadata, bookmarks, page labels and attachments
- Compare and optimise: compare versions and make files smaller