UPDATE 02-01-2020:
The current latest bugfix version for GemBox.Document introduced API support for Field.Update
, so from now own the updating of both DOCPROPERTY and DOCVARIABLE fields can be simplified like the following:
For Each field As Field In document.GetChildElements(True, ElementType.Field)
field.Update()
Next
UPDATE 11-10-2017:
The current latest bugfix version for GemBox.Document introduced API support for DocumentModel.Variables
, so from now own the DOCVARIABLE fields can be updated, for example with something like the following:
Dim variables As VariablesDictionary = document.Variables
For Each field As Field In document.GetChildElements(True, ElementType.Field).Cast(Of Field)().Where(Function(f) f.FieldType = FieldType.DocVariable)
Dim instruction As String = field.GetInstructionText()
Dim variableName As String = If(instruction.IndexOf(" "c) < 0, instruction, instruction.Remove(instruction.IndexOf(" "c)))
Dim value As String = Nothing
If variables.TryGetValue(variableName, value) Then
field.ResultInlines.Clear()
field.ResultInlines.Add(New Run(document, value) With {.CharacterFormat = field.CharacterFormat.Clone()})
End If
Next
ORIGINAL:
GemBox.Document does not automatically update DOCPROPERTY fields when saving to DOCX file. However, they are updated when saving to PDF, XPS or image format and when printing.
Nevertheless, you can update them with something like the following:
Dim properties As DocumentProperties = document.DocumentProperties
For Each field As Field In document.GetChildElements(True, ElementType.Field).Cast(Of Field)().Where(Function(f) f.FieldType = FieldType.DocProperty)
Dim instruction As String = field.GetInstructionText()
Dim propertyName As String = If(instruction.IndexOf(" "c) < 0, instruction, instruction.Remove(instruction.IndexOf(" "c)))
Dim value As String = Nothing
Dim customValue As Object = Nothing
Dim buildInProperty As BuiltInDocumentProperty
If properties.Custom.TryGetValue(propertyName, customValue) Then
value = customValue.ToString()
ElseIf [Enum].TryParse(propertyName, buildInProperty) Then
properties.BuiltIn.TryGetValue(buildInProperty, value)
End If
If Not String.IsNullOrEmpty(value) Then
field.ResultInlines.Clear()
field.ResultInlines.Add(New Run(document, value) With {.CharacterFormat = field.CharacterFormat.Clone()})
End If
Next
Also regarding the DOCVARIABLE fields, these ones cannot currently be updated with GemBox.Document.