officefileapi-116865-word-processing-document-api-word-processing-document-document-properties.md
Word Processing Document API allows you to supply a document with additional information about its author, title, editor, etc. These details are represented by document properties - metadata stored with the document.
Standard ECMA-376 (Edition 1) divides document properties into three categories: Core, Extended, and Custom. Word Processing Document API recognizes Core properties as the document’s built-in properties and both Extended and Custom properties - as custom properties
Use the Document.DocumentProperties property to specify standard document properties, as illustrated below.
View Example: Word Processing Document API – How to Process Word Documents in Code
This code snippet demonstrates how to set standard document properties and show them in a document using specific fields. The FieldCollection.Update method updates all property fields in the document body.
Note
If the DOCPROPERTY fields are located in the text box, header or footer, they should be updated separately. Use the Section.BeginUpdateHeader - Section.EndUpdateHeader or Section.BeginUpdateFooter - Section.EndUpdateFooter paired methods to obtain the header or footer. The TextBox.Document property allows you to retrieve the text box content and update the corresponding fields.
using XtraRichEdit.API.Native;
//...
using (var wordProcessor = new RichEditDocumentServer()) {
wordProcessor.CreateNewDocument();
Document document = wordProcessor.Document;
document.BeginUpdate();
document.DocumentProperties.Creator = "John Doe";
document.DocumentProperties.Title = "Inserting Custom Properties";
document.DocumentProperties.Category = "TestDoc";
document.DocumentProperties.Description = "This code demonstrates API to modify and display standard document properties.";
document.Fields.Create(document.AppendText("\nAUTHOR: ").End, "AUTHOR");
document.Fields.Create(document.AppendText("\nTITLE: ").End, "TITLE");
document.Fields.Create(document.AppendText("\nCOMMENTS: ").End, "COMMENTS");
document.Fields.Create(document.AppendText("\nCREATEDATE: ").End, "CREATEDATE");
document.Fields.Create(document.AppendText("\nCategory: ").End, "DOCPROPERTY Category");
document.Fields.Update();
document.EndUpdate();
}
Imports Microsoft.VisualBasic
Imports XtraRichEdit.API.Native
'...
Using wordProcessor = New RichEditDocumentServer()
wordProcessor.CreateNewDocument()
Dim document As Document = wordProcessor.Document
document.BeginUpdate()
document.DocumentProperties.Creator = "John Doe"
document.DocumentProperties.Title = "Inserting Custom Properties"
document.DocumentProperties.Category = "TestDoc"
document.DocumentProperties.Description = "This code demonstrates API to modify and display standard document properties."
document.Fields.Create(document.AppendText(vbLf & "AUTHOR: ").End, "AUTHOR")
document.Fields.Create(document.AppendText(vbLf & "TITLE: ").End, "TITLE")
document.Fields.Create(document.AppendText(vbLf & "COMMENTS: ").End, "COMMENTS")
document.Fields.Create(document.AppendText(vbLf & "CREATEDATE: ").End, "CREATEDATE")
document.Fields.Create(document.AppendText(vbLf & "Category: ").End, "DOCPROPERTY Category")
document.Fields.Update()
document.EndUpdate()
End Using
The following table lists the built-in document properties supported in different formats:
| DOCX | DOTX | DOCM | DOTM | RTF | DOC | DOT | WordML | ODT | HTML | XML | |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Application | |||||||||||
| AppVersion | |||||||||||
| Category | |||||||||||
| ContentStatus | |||||||||||
| ContentType | |||||||||||
| Company | |||||||||||
| Created | |||||||||||
| Creator | |||||||||||
| Description | |||||||||||
| Identifier | |||||||||||
| Keywords | |||||||||||
| Language | |||||||||||
| LastModifiedBy | |||||||||||
| LastPrinted | |||||||||||
| Manager | |||||||||||
| Modified | |||||||||||
| Revision | |||||||||||
| Subject | |||||||||||
| Template | |||||||||||
| Title | |||||||||||
| Version |
To create a custom document property, use the Document.CustomProperties property to add a new item to the corresponding collection. This can be done in two ways:
server.CreateNewDocument();
Document document = server.Document;
document.BeginUpdate();
document.Fields.Create(document.AppendText("\nMyNumericProperty: ").End, "DOCVARIABLE CustomProperty MyNumericProperty");
document.Fields.Create(document.AppendText("\nMyStringProperty: ").End, "DOCVARIABLE CustomProperty MyStringProperty");
document.Fields.Create(document.AppendText("\nMyBooleanProperty: ").End, "DOCVARIABLE CustomProperty MyBooleanProperty");
document.EndUpdate();
document.CustomProperties["MyNumericProperty"]= 123.45;
document.CustomProperties["MyStringProperty"]="The Final Answer";
document.CustomProperties["MyBooleanProperty"]=true;
server.CalculateDocumentVariable += DocumentPropertyDisplayHelper.OnCalculateDocumentVariable;
document.Fields.Update();
server.CreateNewDocument()
Dim document As Document = server.Document
document.BeginUpdate()
document.Fields.Create(document.AppendText(vbLf & "MyNumericProperty: ").End, "DOCVARIABLE CustomProperty MyNumericProperty")
document.Fields.Create(document.AppendText(vbLf & "MyStringProperty: ").End, "DOCVARIABLE CustomProperty MyStringProperty")
document.Fields.Create(document.AppendText(vbLf & "MyBooleanProperty: ").End, "DOCVARIABLE CustomProperty MyBooleanProperty")
document.EndUpdate()
document.CustomProperties("MyNumericProperty")= 123.45
document.CustomProperties("MyStringProperty")="The Final Answer"
document.CustomProperties("MyBooleanProperty")=True
AddHandler server.CalculateDocumentVariable, AddressOf DocumentPropertyDisplayHelper.OnCalculateDocumentVariable
document.Fields.Update()
Using DocumentCustomProperties.Add method, as illustrated below.
private void SetCustomProperties()
{
Document document = server.Document;
document.CustomProperties.Add("MyNumericProperty", 123.45);
document.CustomProperties.Add("MyStringProperty", "The Final Answer");
document.CustomProperties.Add("MyBookmarkProperty", document.Bookmarks[0]);
document.CustomProperties.Add("MyBooleanProperty", true);
}
Private Sub SetCustomProperties()
Dim document As Document = server.Document
document.CustomProperties.Add("MyNumericProperty", 123.45)
document.CustomProperties.Add("MyStringProperty", "The Final Answer")
document.CustomProperties.Add("MyBookmarkProperty", document.Bookmarks(0))
document.CustomProperties.Add("MyBooleanProperty", True)
End Sub
Tip
You can load document properties without loading the document itself. Refer to the following topic for more information: How to: Load Document Properties