DeduplicateResources

Document manipulation, Optimisation

Description

Finds byte-identical indirect resource streams throughout the loaded document, redirects every reference to a deterministic canonical stream, and optionally removes the duplicate indirect objects

Equality requires both an order-independent signature of the complete stream dictionary and an exact match of the encoded stream payload, so image masks, colour spaces, decode arrays, optional-content associations, structure identifiers, metadata, filters, and every semantic dictionary entry remain part of the decision

Syntax

Delphi

Function TPDFlib.DeduplicateResources(
  Options: Integer= PDF_RESOURCE_DEDUP_DEFAULT): Integer; Overload;
Function TPDFlib.DeduplicateResources(Options: Integer;
  Out Stats: TPDFlibResourceDedupStats): Integer; Overload;

Parameters

OptionsA bitwise combination of resource deduplication option constants
StatsReceives scan, hashing, comparison, reference, payload, category, and orphan-object counters

Resource type options

PDF_RESOURCE_DEDUP_IMAGESIncludes image XObjects, explicit image masks, soft-mask images, alternate images, and thumbnails
PDF_RESOURCE_DEDUP_FORMSIncludes Form XObjects such as reusable page graphics, annotation appearances, and transparency groups
PDF_RESOURCE_DEDUP_PATTERNSIncludes stream-based tiling patterns
PDF_RESOURCE_DEDUP_SHADINGSIncludes stream-based mesh shadings
PDF_RESOURCE_DEDUP_FUNCTIONSIncludes sampled function streams
PDF_RESOURCE_DEDUP_ICC_PROFILESIncludes ICC profile streams referenced by ICCBased colour spaces or output intents
PDF_RESOURCE_DEDUP_FONT_PROGRAMSIncludes streams referenced through FontFile, FontFile2, or FontFile3
PDF_RESOURCE_DEDUP_CMAPSIncludes CMap and ToUnicode streams
PDF_RESOURCE_DEDUP_TYPE3_CHARPROCSIncludes Type 3 glyph procedure streams
PDF_RESOURCE_DEDUP_EMBEDDED_FILESIncludes embedded-file streams
PDF_RESOURCE_DEDUP_METADATAIncludes metadata streams
PDF_RESOURCE_DEDUP_POSTSCRIPTIncludes PostScript XObjects
PDF_RESOURCE_DEDUP_ALL_TYPESCombines all twelve resource type flags

Operation options

PDF_RESOURCE_DEDUP_DRY_RUNReports duplicate streams and references without changing the object graph or collecting objects
PDF_RESOURCE_DEDUP_GARBAGE_COLLECTRuns mark-and-sweep collection after references are redirected
PDF_RESOURCE_DEDUP_DEFAULTCombines all resource types with garbage collection

Return values

Positive valueThe number of duplicate indirect streams consolidated or reported by a dry run
0No eligible duplicate stream was found or no document is loaded

Remarks

The lowest object number and generation in each exact-equivalence group becomes the canonical stream, making repeated runs deterministic

Dictionary key order does not affect equality, but every semantic key and value does, including indirect reference identity; a numeric top-level Length entry is compared by its resolved value so equivalent direct and indirect length forms can share a resource

Resources with equal decoded content but different filters or encoded bytes remain separate, as do resources whose dictionaries refer to different nested objects

All candidate payloads are hashed once with SHA-256, and a byte-for-byte confirmation runs only inside a matching hash and dictionary-signature bucket

The method operates on indirect streams because ISO 32000 requires streams to be indirect objects and shared resources are represented by indirect references

Use the default after document editing and before a full save to reclaim duplicate object bytes immediately

For an incremental save, omit PDF_RESOURCE_DEDUP_GARBAGE_COLLECT when old revisions must remain physically intact; redirected references still reduce duplication in the new revision but cannot remove bytes from earlier revisions

Example

var
  Stats: TPDFlibResourceDedupStats;
  Merged: Integer;
begin
  Merged:= PDF.DeduplicateResources(
    PDF_RESOURCE_DEDUP_DEFAULT, Stats);
  PDF.SaveToFile('optimised.pdf');
end;

See also

TPDFlibResourceDedupStats, PurgeUnusedResources, GarbageCollectObjects