Can I change metadata of a PDF file?
Yes. Read the document, edit the info dictionary, and write it back through
PdfWriter.SetDocInfo.
info, err := pdfReader.GetPdfInfo()
if err != nil {
return err
}
info.Title = core.MakeString("Quarterly report")
info.Author = core.MakeString("Accounts")
pdfWriter, err := pdfReader.ToWriter(&model.ReaderToWriterOpts{SkipInfo: true})
if err != nil {
return err
}
pdfWriter.SetDocInfo(info)
return pdfWriter.WriteToFile("output.pdf")SetDocInfo replaces the whole dictionary rather than merging into it, so
anything left nil on the PdfInfo is absent from the output. Reading the
existing one first, as above, is what makes this an edit. SkipInfo: true stops
ToWriter copying the source /Info over the top afterwards.
The string fields are *core.PdfObjectString, so they go through
core.MakeString. CreationDate and ModifiedDate are *model.PdfDate from
model.NewPdfDateFromTime, and Trapped is a *core.PdfObjectName. Custom
keys go in with AddCustomInfo, which errors if the name collides with one of
the nine standard ones.
A PDF has a second, independent metadata system: an XMP packet on the document
catalog. Writing /Info does not touch it, so a document carrying both will now
have two versions of the truth, and that is a PDF/A conformance failure. Push
the same values into the XMP with xmputil.Document.SetPdfInfo.
When you are appending an incremental update rather than rewriting the file,
PdfAppender.SetDocInfo is the equivalent call.
See Set doc info metadata for the process-wide setters, and the metadata guides for XMP.