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
| Options | A bitwise combination of resource deduplication option constants |
|---|---|
| Stats | Receives scan, hashing, comparison, reference, payload, category, and orphan-object counters |
Resource type options
| PDF_RESOURCE_DEDUP_IMAGES | Includes image XObjects, explicit image masks, soft-mask images, alternate images, and thumbnails |
|---|---|
| PDF_RESOURCE_DEDUP_FORMS | Includes Form XObjects such as reusable page graphics, annotation appearances, and transparency groups |
| PDF_RESOURCE_DEDUP_PATTERNS | Includes stream-based tiling patterns |
| PDF_RESOURCE_DEDUP_SHADINGS | Includes stream-based mesh shadings |
| PDF_RESOURCE_DEDUP_FUNCTIONS | Includes sampled function streams |
| PDF_RESOURCE_DEDUP_ICC_PROFILES | Includes ICC profile streams referenced by ICCBased colour spaces or output intents |
| PDF_RESOURCE_DEDUP_FONT_PROGRAMS | Includes streams referenced through FontFile, FontFile2, or FontFile3 |
| PDF_RESOURCE_DEDUP_CMAPS | Includes CMap and ToUnicode streams |
| PDF_RESOURCE_DEDUP_TYPE3_CHARPROCS | Includes Type 3 glyph procedure streams |
| PDF_RESOURCE_DEDUP_EMBEDDED_FILES | Includes embedded-file streams |
| PDF_RESOURCE_DEDUP_METADATA | Includes metadata streams |
| PDF_RESOURCE_DEDUP_POSTSCRIPT | Includes PostScript XObjects |
| PDF_RESOURCE_DEDUP_ALL_TYPES | Combines all twelve resource type flags |
Operation options
| PDF_RESOURCE_DEDUP_DRY_RUN | Reports duplicate streams and references without changing the object graph or collecting objects |
|---|---|
| PDF_RESOURCE_DEDUP_GARBAGE_COLLECT | Runs mark-and-sweep collection after references are redirected |
| PDF_RESOURCE_DEDUP_DEFAULT | Combines all resource types with garbage collection |
Return values
| Positive value | The number of duplicate indirect streams consolidated or reported by a dry run |
|---|---|
| 0 | No 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