Morgan Rose
Morgan Rose

Reputation: 29

Document properties in Word and PDF files

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

Answers (1)

Mario Z
Mario Z

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

Related Questions