Reputation: 29
I'm trying to read a specific property from a Word file with GemBox.Document, but in some cases, it might not be in the document, so I get an error.
var document = DocumentModel.Load("ovl-last.docx");
var documentProperties = document.DocumentProperties;
var ovldoctype = documentProperties.Custom["DocType"];
In the Document Properties example, I noticed that we can iterate through the properties. Is this the only "safe" way to get a property and prevent an exception, or is there another way to check its existence?
Also, when I save the DocumentModel
to PDF I noticed that I get a hard-coded "GemBox.Document XYZ" value for the "PDF Producer" property. I can see it in: Adobe Reader -> File -> Properties... -> Advanced -> PDF Producer
Can I change this to something else?
Upvotes: 1
Views: 273
Reputation: 4381
Note that both DocumentProperties.BuiltIn
and DocumentProperties.Custom
are dictionaries, so you can either use TryGetValue
to try retrieving the "DocType" value or use ContainsKey
method to check if the "DocType" key exists.
For instance, something like this:
var document = DocumentModel.Load("ovl-last.docx");
var documentProperties = document.DocumentProperties;
if (document.DocumentProperties.Custom.TryGetValue("DocType", out object ovldoctype))
{
// ...
}
Or this:
var document = DocumentModel.Load("ovl-last.docx");
var documentProperties = document.DocumentProperties;
if (document.DocumentProperties.Custom.ContainsKey("DocType"))
{
var ovldoctype = documentProperties.Custom["DocType"];
// ...
}
Regarding the "PDF Producer", try adding the "Producer" property:
documentProperties.Custom.Add("Producer", "My value for PDF Producer");
document.Save("ovl-last.pdf");
Upvotes: 1