Skip to content
Inspect PDF Objects

Inspect PDF Objects

pdfReader.Inspect() walks every entry in the cross reference table and returns a histogram of object types, keyed by the Type or Subtype name found in each dictionary. It is the cheapest way to characterize an unknown file: whether it has fonts, how many XObjects it carries, whether it contains anything executable.

Doing it

objTypes, err := pdfReader.Inspect()
if err != nil {
    return err
}

for name, count := range objTypes {
    fmt.Printf("- %s: %d instances\n", name, count)
}

if count, has := objTypes["JavaScript"]; has {
    fmt.Printf("! has %d JavaScript objects\n", count)
}

The map is unordered, so sort the keys before printing if you want stable output.

Keys are PDF type names, not Go type names. A dictionary contributes its Type value if it has one, otherwise its Subtype value, so you see Catalog, Page, Pages, Font, XObject, Annot and the like. JavaScript is the one key that does not follow that rule: it is reported for any dictionary whose S entry is the name JavaScript, which is how an action dictionary declares itself. Flash and Video, which the example also checks, are rich media subtypes and arrive through the normal Subtype path.

Limitations

Objects with neither a Type nor a Subtype entry are not counted at all, so the totals do not add up to the number of objects in the file. Arrays, numbers and strings that happen to be numbered objects fall into this group.

Objects that fail to parse are skipped silently. Inspect only reports an error when the cross reference table itself is empty, in which case you get “invalid document (xref table missing)”.

The counts come from the current revision’s cross reference table. In an incrementally updated file, objects that were superseded by a later revision are not counted twice, but objects present only in an earlier revision are not counted at all.

Finding no JavaScript, Flash or Video keys is not a clean bill of health. It says those three object kinds are absent, nothing more; embedded files, launch actions and external URI actions are not covered.

Sample input

Sample PDF file

Run the example

inspectPdf prints the page count, then the object type histogram sorted by name, then a one line verdict based on the presence of JavaScript, Flash and Video keys.

git clone https://github.com/unidoc/unipdf-examples.git
cd unipdf-examples/analysis
go run pdf_inspect.go input.pdf

If this is your first time using UniPDF, follow the getting started guide to create an API key and set up your development environment.

View the full source

Sample output

Input file: templates/boarding-pass/unipdf-boarding-pass.pdf
PDF Num Pages: 1
Object types:
- Catalog: 1 instances
- Font: 64 instances
- Outlines: 1 instances
- Page: 1 instances
- Pages: 1 instances
- XObject: 14 instances
Most likely harmless - No javascript or rich media objects.
Last updated on