Document structure
Read and set metadata, bookmarks and page labels, show and hide layers, and embed or extract file attachments through the NexPDF C API with NexPDF for Flutter.
License module
Reading metadata, bookmarks, page labels, layers and attachments, and switching layers on or off,
work without a key. Changing metadata, bookmarks, page labels and attachments needs the editor
module (Startup tier and above).
The structure functions have no Dart method. Call them
through dart:ffi on doc.handle, then save with
doc.save(). The examples use these lookups:
import 'dart:convert';
import 'dart:ffi';
import 'dart:typed_data';
import 'package:ffi/ffi.dart';
import 'package:nexpdf_flutter/nexpdf_flutter.dart';
import 'nexpdf_ffi.dart';
typedef _DocJsonNative = Pointer<Utf8> Function(Pointer<Void>);
typedef _DocSetJsonNative = Int32 Function(Pointer<Void>, Pointer<Utf8>);
typedef _DocSetJson = int Function(Pointer<Void>, Pointer<Utf8>);
/// Calls a `char *f(const NexPdfDocument *)` function and decodes its JSON.
Object? docJson(NexPdfDocument doc, String name) {
final f = nexpdfLib.lookupFunction<_DocJsonNative, _DocJsonNative>(name);
return jsonDecode(nexpdfCall(() => takeString(f(doc.handle))) ?? 'null');
}
/// Calls an `int32_t f(NexPdfDocument *, const char *json)` function.
void setDocJson(NexPdfDocument doc, String name, Object value) {
final f = nexpdfLib.lookupFunction<_DocSetJsonNative, _DocSetJson>(name);
using((a) => nexpdfCall(() => f(doc.handle, jsonEncode(value).toNativeUtf8(allocator: a))));
}Metadata
nexpdf_metadata_json returns the document information dictionary (title, author, subject,
keywords and the other entries the file has). nexpdf_set_metadata(doc, title, author, subject, keywords) changes it; a null pointer (nullptr) keeps the current value.
final info = docJson(doc, 'nexpdf_metadata_json') as Map<String, Object?>;
print('${info['title']} ${info['author']}');
// int32_t nexpdf_set_metadata(NexPdfDocument *doc, const char *title, const char *author,
// const char *subject, const char *keywords);
final setMetadata = nexpdfLib.lookupFunction<
Int32 Function(Pointer<Void>, Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>),
int Function(Pointer<Void>, Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>)>('nexpdf_set_metadata');
using((a) => nexpdfCall(() => setMetadata(doc.handle,
'Q3 report'.toNativeUtf8(allocator: a), 'Finance'.toNativeUtf8(allocator: a), nullptr, nullptr)));
final titled = doc.save();Bookmarks
Bookmarks (the outline) are a JSON tree of title, 0-based page and optional children.
nexpdf_set_outlines_json replaces the whole tree.
void walk(List<Object?> items, [int depth = 0]) {
for (final item in items.cast<Map<String, Object?>>()) {
print('${' ' * depth}${item['title']} (page ${(item['page'] as int) + 1})');
walk((item['children'] as List?) ?? const [], depth + 1);
}
}
walk(docJson(doc, 'nexpdf_outlines_json') as List);
setDocJson(doc, 'nexpdf_set_outlines_json', [
{'title': 'Summary', 'page': 0},
{'title': 'Details', 'page': 2, 'children': [{'title': 'Costs', 'page': 3}]},
]);Page labels
Page labels are the page numbers a viewer shows, such as i, ii for a preface and 1, 2
after it. nexpdf_page_labels_json returns the label of every page and the ranges that produce
them.
final labels = docJson(doc, 'nexpdf_page_labels_json') as Map<String, Object?>;
print((labels['labels'] as List).take(4)); // e.g. (i, ii, 1, 2)
setDocJson(doc, 'nexpdf_set_page_labels_json', [
{'start': 0, 'style': 'roman'},
{'start': 2, 'style': 'decimal', 'first': 1},
]);
setDocJson(doc, 'nexpdf_set_page_labels_json', []); // removes the labelsStyles are decimal, roman, ROMAN, letters, LETTERS and none. A range can also have a
prefix, for example "A-".
Layers
Layers (optional content groups) are parts of a page that can be shown or hidden, common in CAD drawings and maps. Each layer is identified by its object number and generation.
final layers = (docJson(doc, 'nexpdf_layers_json') as List).cast<Map<String, Object?>>();
for (final layer in layers) {
print('${layer['name']} ${layer['visible'] == true ? 'visible' : 'hidden'}');
}
// int32_t nexpdf_set_layer_visible(NexPdfDocument *doc, uint32_t num, uint16_t generation, int32_t visible);
final setLayerVisible = nexpdfLib.lookupFunction<Int32 Function(Pointer<Void>, Uint32, Uint16, Int32),
int Function(Pointer<Void>, int, int, int)>('nexpdf_set_layer_visible');
final notes = layers.firstWhere((l) => l['name'] == 'Notes');
nexpdfCall(() => setLayerVisible(doc.handle, notes['num'] as int, notes['gen'] as int, 0)); // 1 shows it
final png = doc.renderPagePng(0, dpi: 144); // rendered without the layerAttachments
PDFs can carry files: at document level (an attachment list) or on a page (a paperclip annotation).
nexpdf_attachments_json lists both; page is null for document-level files.
for (final a in (docJson(doc, 'nexpdf_attachments_json') as List).cast<Map<String, Object?>>()) {
print('${a['name']} ${a['mime']} ${a['size']} ${a['page']}');
}
// NexPdfBytes nexpdf_attachment_data(const NexPdfDocument *doc, const char *name);
final attachmentData = nexpdfLib.lookupFunction<NexPdfBytes Function(Pointer<Void>, Pointer<Utf8>),
NexPdfBytes Function(Pointer<Void>, Pointer<Utf8>)>('nexpdf_attachment_data');
// int32_t nexpdf_add_attachment(NexPdfDocument *doc, const char *name, const uint8_t *data,
// size_t len, const char *mime, const char *description);
final addAttachment = nexpdfLib.lookupFunction<
Int32 Function(Pointer<Void>, Pointer<Utf8>, Pointer<Uint8>, Size, Pointer<Utf8>, Pointer<Utf8>),
int Function(Pointer<Void>, Pointer<Utf8>, Pointer<Uint8>, int, Pointer<Utf8>, Pointer<Utf8>)>('nexpdf_add_attachment');
// int32_t nexpdf_remove_attachment(NexPdfDocument *doc, const char *name);
final removeAttachment = nexpdfLib.lookupFunction<_DocSetJsonNative, _DocSetJson>('nexpdf_remove_attachment');
final Uint8List xml = using((a) =>
nexpdfCall(() => takeBytes(attachmentData(doc.handle, 'invoice.xml'.toNativeUtf8(allocator: a)))));
using((a) => nexpdfCall(() => addAttachment(doc.handle, 'data.csv'.toNativeUtf8(allocator: a),
toNative(csvBytes, a), csvBytes.length, 'text/csv'.toNativeUtf8(allocator: a),
'Source data'.toNativeUtf8(allocator: a))));
using((a) => nexpdfCall(() => removeAttachment(doc.handle, 'old.csv'.toNativeUtf8(allocator: a))));
final withData = doc.save();To attach a file to a page with a visible icon, use nexpdf_add_file_attachment_annot; see
Annotations.
In the reader
The native reader has bookmarks and attachments built in, so users can browse them without any code.
Related
- Editing: pages, text and images
- Opening and saving