Skip to Content
NexPDF

Compare and optimise

Compare two PDF versions page by page, make files smaller by pruning objects and subsetting fonts, save for Fast Web View and control memory with NexPDF for C++.

English

License module

Comparing documents, linearized saving and the memory calls work without a key. optimize and subset_fonts need the editor module (Startup tier and above).

The examples parse JSON results with nlohmann/json, as an example only, and use the write_file helper from Getting started.

Compare two versions

compare_json(other, dpi) compares two documents page by page: whether the text changed and how much of the rendered page differs. compare_overlay_png(other, page_index, dpi) draws the differences of one page as a PNG.

#include <nexpdf.hpp>
#include <nlohmann/json.hpp>

namespace nexpdf = nexdevit::nexpdf;

auto a = nexpdf::Document::open("v1.pdf");
auto b = nexpdf::Document::open("v2.pdf");

auto diff = nlohmann::json::parse(a.compare_json(b, 72));
std::cout << diff["pages_a"] << " pages before, " << diff["pages_b"] << " after\n";

for (const auto& page : diff["pages"]) {
    int index = page["page"].get<int>();
    double changed = page["visual_diff"].get<double>();
    if (page["text_changed"].get<bool>() || changed > 0.001) {
        std::printf("page %d: %.2f%% of the pixels changed\n", index + 1, changed * 100);
        write_file("diff-" + std::to_string(index + 1) + ".png", a.compare_overlay_png(b, index, 110));
    }
}

To see which words changed (inserted, deleted, replaced and moved text, and an annotated PDF of the changes), use Word-level comparison.

visual_diff is the share of differing pixels, from 0 to 1. A low resolution (72 dpi) is enough to find changed pages; the overlay resolution is clamped to 18-300 dpi.

Make files smaller

optimize() compresses uncompressed streams and removes objects nothing refers to. subset_fonts() replaces embedded TrueType, OpenType and CFF fonts with subsets of the glyphs the document uses. Both return a JSON report; save afterwards.

auto doc = nexpdf::Document::open("large.pdf");

auto r = nlohmann::json::parse(doc.optimize());
std::cout << r["streams_compressed"] << " streams compressed, " << r["objects_pruned"]
          << " objects pruned, " << r["bytes_saved"] << " bytes saved\n";

auto f = nlohmann::json::parse(doc.subset_fonts());
std::cout << f["fonts_subset"] << " fonts subset: " << f["bytes_before"] << " -> "
          << f["bytes_after"] << " bytes\n";

doc.save("small.pdf");

Fonts that may still be needed for new text are left whole and counted in fonts_skipped.

Fast Web View

Linearized files let a browser show the first page before the rest downloads:

doc.save("web.pdf", true);

Memory

memory_usage_bytes() estimates the memory a document uses. trim_memory() compacts its internal buffers and drops objects nothing refers to any more, and returns the bytes reclaimed. Call it in long-running processes after large edits or batch work:

std::cout << doc.memory_usage_bytes() / 1024 << " KiB\n";
size_t freed = doc.trim_memory();

The document stays fully usable. Destroying it, or calling reset(), frees everything. In C the same calls are nexpdf_memory_usage_bytes(doc) and nexpdf_trim_memory(doc). See Platform notes for threading and memory.

On this page