Document structure
Read and set metadata, bookmarks and page labels, show and hide layers, and embed or extract file attachments with NexPDF for C++.
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 examples parse JSON results with nlohmann/json, as an
example only, and use the read_file and write_file helpers from
Getting started.
Metadata
metadata_json() returns the document information dictionary (title, author, subject, keywords and
the other entries the file has). set_metadata(title, author, subject, keywords) changes it;
std::nullopt (the default for each argument) keeps the current value.
#include <nexpdf.hpp>
#include <nlohmann/json.hpp>
namespace nexpdf = nexdevit::nexpdf;
auto doc = nexpdf::Document::open("report.pdf");
auto info = nlohmann::json::parse(doc.metadata_json());
std::cout << info.value("title", "") << " " << info.value("author", "") << "\n";
doc.set_metadata("Q3 report", "Finance"); // subject and keywords are kept
doc.save("report-titled.pdf");Bookmarks
Bookmarks (the outline) are a JSON tree of title, 0-based page and optional children.
set_outlines_json replaces the whole tree.
void walk(const nlohmann::json& items, int depth = 0) {
for (const auto& item : items) {
std::cout << std::string(depth * 2, ' ') << item["title"].get<std::string>()
<< " (page " << item["page"].get<int>() + 1 << ")\n";
if (item.contains("children")) walk(item["children"], depth + 1);
}
}
walk(nlohmann::json::parse(doc.outlines_json()));
doc.set_outlines_json(R"([{"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. page_labels_json() returns the label of every page and the ranges that produce them.
auto labels = nlohmann::json::parse(doc.page_labels_json());
std::cout << labels["labels"].dump() << "\n"; // e.g. ["i","ii","1","2"]
doc.set_page_labels_json(R"([{"start":0,"style":"roman"},{"start":2,"style":"decimal","first":1}])");
doc.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.
for (const auto& layer : nlohmann::json::parse(doc.layers_json())) {
std::cout << layer["name"].get<std::string>() << (layer["visible"].get<bool>() ? " visible" : " hidden") << "\n";
if (layer["name"] == "Notes") {
doc.set_layer_visible(layer["num"].get<uint32_t>(), layer["gen"].get<uint16_t>(), 0); // hide; 1 shows it
}
}
write_file("page1.png", doc.render_page_png(0, 144)); // rendered without the layerAttachments
PDFs can carry files: at document level (an attachment list) or on a page (a paperclip annotation).
attachments_json() lists both; page is null for document-level files.
for (const auto& a : nlohmann::json::parse(doc.attachments_json())) {
std::cout << a["name"] << " " << a["mime"] << " " << a["size"] << " " << a["page"] << "\n";
}
write_file("invoice.xml", doc.attachment_data("invoice.xml"));
doc.add_attachment("data.csv", read_file("data.csv"), "text/csv", "Source data");
doc.remove_attachment("old.csv");
doc.save("with-data.pdf");To attach a file to a page with a visible icon, use add_file_attachment_annot; see
Annotations.
Related
- Editing: pages, text and images
- Opening and saving