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 through the NexPDF C API with NexPDF for Flutter.

English

License module

Comparing documents, linearized saving and the memory functions work without a key. nexpdf_optimize and nexpdf_subset_fonts need the editor module (Startup tier and above).

These functions have no Dart method. Call them through dart:ffi on doc.handle.

Compare two versions

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

import 'dart:convert';
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:nexpdf_flutter/nexpdf_flutter.dart';
import 'nexpdf_ffi.dart';

// char *nexpdf_compare_json(const NexPdfDocument *a, const NexPdfDocument *b, double dpi);
final compareJson = nexpdfLib.lookupFunction<Pointer<Utf8> Function(Pointer<Void>, Pointer<Void>, Double),
    Pointer<Utf8> Function(Pointer<Void>, Pointer<Void>, double)>('nexpdf_compare_json');
// NexPdfBytes nexpdf_compare_overlay_png(const NexPdfDocument *a, const NexPdfDocument *b,
//                                        size_t page_index, double dpi);
final compareOverlay = nexpdfLib.lookupFunction<NexPdfBytes Function(Pointer<Void>, Pointer<Void>, Size, Double),
    NexPdfBytes Function(Pointer<Void>, Pointer<Void>, int, double)>('nexpdf_compare_overlay_png');

final a = sdk.open(v1Bytes), b = sdk.open(v2Bytes);
try {
  final diff = jsonDecode(nexpdfCall(() => takeString(compareJson(a.handle, b.handle, 72)))!) as Map<String, Object?>;
  print('${diff['pages_a']} pages before, ${diff['pages_b']} after');

  for (final page in (diff['pages'] as List).cast<Map<String, Object?>>()) {
    final share = (page['visual_diff'] as num).toDouble();
    if (page['text_changed'] == true || share > 0.001) {
      final index = page['page'] as int;
      print('page ${index + 1}: ${(share * 100).toStringAsFixed(2)}% of the pixels changed');
      final png = nexpdfCall(() => takeBytes(compareOverlay(a.handle, b.handle, index, 110)));
    }
  }
} finally {
  a.dispose();
  b.dispose();
}

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

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. Show the overlay with Image.memory(png).

Make files smaller

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

// char *nexpdf_optimize(NexPdfDocument *doc);   char *nexpdf_subset_fonts(NexPdfDocument *doc);
typedef _DocJson = Pointer<Utf8> Function(Pointer<Void>);
final optimize = nexpdfLib.lookupFunction<_DocJson, _DocJson>('nexpdf_optimize');
final subsetFonts = nexpdfLib.lookupFunction<_DocJson, _DocJson>('nexpdf_subset_fonts');

final r = jsonDecode(nexpdfCall(() => takeString(optimize(doc.handle)))!) as Map<String, Object?>;
print('${r['streams_compressed']} streams compressed, ${r['objects_pruned']} objects pruned, '
    '${r['bytes_saved']} bytes saved');

final f = jsonDecode(nexpdfCall(() => takeString(subsetFonts(doc.handle)))!) as Map<String, Object?>;
print('${f['fonts_subset']} fonts subset: ${f['bytes_before']} -> ${f['bytes_after']} bytes');

final small = doc.save();

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. Use nexpdf_save_linearized; see Opening and saving.

Memory

nexpdf_memory_usage_bytes estimates the memory a document uses. nexpdf_trim_memory compacts its internal buffers and drops objects nothing refers to any more, and returns the bytes reclaimed. Call it on long-lived documents after large edits or batch work:

// size_t nexpdf_memory_usage_bytes(const NexPdfDocument *doc);   size_t nexpdf_trim_memory(NexPdfDocument *doc);
final memoryUsage = nexpdfLib.lookupFunction<Size Function(Pointer<Void>), int Function(Pointer<Void>)>('nexpdf_memory_usage_bytes');
final trimMemory = nexpdfLib.lookupFunction<Size Function(Pointer<Void>), int Function(Pointer<Void>)>('nexpdf_trim_memory');

print('${memoryUsage(doc.handle) ~/ 1024} KiB');
final freed = trimMemory(doc.handle);

The document stays fully usable. dispose() frees everything. See Platform notes for isolates and memory.

On this page