Skip to content
Get Doc Info Metadata

Get Doc Info Metadata

PdfReader.GetPdfInfo reads the trailer’s /Info dictionary and returns it as a model.PdfInfo. This is the flat, older metadata system, not XMP; a document can have one, the other, both, or neither, and the two do not have to agree. See the section overview for the difference.

Doing it

pdfReader, err := model.NewPdfReader(f)
if err != nil {
    return err
}

pdfInfo, err := pdfReader.GetPdfInfo()
if err != nil {
    return err
}

if pdfInfo.Title != nil {
    fmt.Println("Title:", pdfInfo.Title.Decoded())
}

Every standard field on PdfInfo is a pointer, so nil means the key was absent and you have to check before dereferencing. The string fields are *core.PdfObjectString: use Decoded() rather than String(), since it handles the UTF-16BE byte-order mark that PDF producers commonly write. CreationDate and ModifiedDate are *model.PdfDate and convert with ToGoTime(). Note the field is ModifiedDate in Go even though the dictionary key is /ModDate. Trapped is a *core.PdfObjectName holding True, False or Unknown.

Custom keys

Keys outside the standard set are not dropped. NewPdfInfoFromObject collects them into a separate dictionary, which you reach with CustomKeys() to list the names and GetCustomInfo(name) to fetch a value:

for _, key := range pdfInfo.CustomKeys() {
    fmt.Printf("%s: %s\n", key, pdfInfo.GetCustomInfo(key).Decoded())
}

GetCustomInfo returns a nil *core.PdfObjectString for a name that isn’t present, and Decoded() is nil-safe, so a missing key reads as an empty string rather than panicking.

Limitations

Only the nine standard keys are parsed into named fields. Anything else is a custom key, including keys some tools treat as conventional.

CustomKeys() currently returns a slice with as many empty leading entries as there are real keys, an artifact of how the slice is allocated. Guard against empty strings if the blank entries matter for your output.

A document with no /Info entry at all still returns a non-nil PdfInfo with every field nil, so there is no separate “absent” signal to check.

Run the example

printPdfDocInfo opens each file named on the command line, collects the info dictionary fields into a pdfDocInfo struct along with the page count, and prints the summary.

git clone https://github.com/unidoc/unipdf-examples.git
cd unipdf-examples/metadata
go run pdf_metadata_get_docinfo.go input1.pdf [input2.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
Last updated on