The following code adds a document information section to an
existing PDF document. First it adds an empty dictionary and
references it from the document trailer. Then it adds an Author,
Title and Subject before saving.
There are multiple places that metadata can be put into a PDF.
The most commonly used are the Info entry of the Trailer and the
Metadata entry of the Catalog. The Info entry is the older and most
widely recognized location. The Metadata entry is a more recent XML
based store. It is important that information within these stores is
consistent. If the information is inconsistent then you'll find that
the metadata reported by different applications is different. To
ensure that the data is consistent we're going to delete any XML
Metadata entry that may be present. That way we force applications
to report the Info store. However it wouldn't be a difficult matter
to load up any XML in the Metadata entry and modify that as well as
the Info entry.
[C#]
Doc theDoc = new
Doc(); theDoc.Read(Server.MapPath("../mypics/sample.pdf")); if
(theDoc.GetInfo(-1, "/Info") ==
"") theDoc.SetInfo(-1, "/Info:Ref",
theDoc.AddObject("<<
>>").ToString()); theDoc.SetInfo(-1, "/Info*/Author:Text",
"Arthur Dent"); theDoc.SetInfo(-1, "/Info*/Title:Text", "Musings
on Life"); theDoc.SetInfo(-1, "/Info*/Subject:Text",
"Philosophy"); theDoc.SetInfo(theDoc.Root, "/Metadata:Del",
""); theDoc.Save(Server.MapPath("docaddobject.pdf")); theDoc.Clear();
[Visual Basic]
Dim theDoc As Doc =
New Doc()
theDoc.Read(Server.MapPath("../mypics/sample.pdf")) If
theDoc.GetInfo(- 1, "/Info") = ""
Then theDoc.SetInfo(- 1, "/Info:Ref",
theDoc.AddObject("<< >>").ToString()) End
If theDoc.SetInfo(- 1, "/Info*/Author:Text", "Arthur
Dent") theDoc.SetInfo(- 1, "/Info*/Title:Text", "Musings on
Life") theDoc.SetInfo(- 1, "/Info*/Subject:Text",
"Philosophy") theDoc.SetInfo(theDoc.Root, "/Metadata:Del",
"") theDoc.Save(Server.MapPath("docaddobject.pdf")) theDoc.Clear()
|
|
|