TX Text Control .NET for Windows Forms Documentation

Importing Fields from Microsoft Word

TX Text Control supports field formats of other applications such as Microsoft Word. The ApplicationField class provides an interface to retrieve and change the data and parameters of such fields. Currently supported formats are the Microsoft Word Field Format, the format of Microsoft Word Form Fields and the field format of the Heiler HighEdit component. Because these formats are not compatible, the user must specify the format when a document is loaded. The following code imports all MergeField and PrintDate fields contained in a RTF document that has been created with Microsoft Word:

[C#] LoadSettings ls = new LoadSettings(); ls.ApplicationFieldFormat = ApplicationFieldFormat.MSWord; ls.ApplicationFieldTypeNames = new string[] { "MERGEFIELD", "PRINTDATE" }; textControl1.Load("c:\\test.rtf", TXTextControl.StreamType.RichTextFormat, ls);
[Visual Basic] Dim ls As New LoadSettings ls.ApplicationFieldFormat = ApplicationFieldFormat.MSWord ls.ApplicationFieldTypeNames = New String() {"MERGEFIELD", "PRINTDATE"} TextControl1.Load("c:\test.rtf", TXTextControl.StreamType.RichTextFormat, ls)

After the document has been loaded all MergeField and PrintDate fields are available through the TextControl.ApplicationFields collection. The collection contains objects of the type ApplicationField. The ApplicationField class has a TypeName property specifying the field's type and a Parameters property specifying the field's parameters as an array of strings. The following is the format of a MergeField defined through Microsoft Word:

MERGEFIELD FieldName [Switches]

For example: MERGEFIELD Address \* Upper \v

For further information about the switches and it's meaning, please refer to Microsoft's Field Reference:

http://www.textcontrol.com/in/msofficefieldreference.htm

In this example the FieldName is Address and there are two switches specifying some formatting options. The ApplicationField.Parameters array then contains three elements, the first of which is the FieldName and the others are the switches.

The following code iterates through all fields imported with the example above and sets the visible text of the address field:

[C#] foreach (ApplicationField appField in textControl1.ApplicationFields) { if (appField.TypeName == "MERGEFIELD" && appField.Parameters[0] == "Address") { appField.Text = "Bakerstreet, London"; } }
[Visual Basic] For Each appField As ApplicationField In TextControl1.ApplicationFields If appField.TypeName = "MERGEFIELD" And appField.Parameters(0) = "Address" Then appField.Text = "Bakerstreet, London" End If Next

To add to or remove elements from the Parameters string array a new array must be constructed because .NET arrays have a fixed size. The following code looks for a \v switch and removes it from the parameters array. First a new array is created and the remaining strings that can be in front or behind the searched string are copied:

[C#] foreach (ApplicationField appField in textControl1.ApplicationFields) { string[] params = appField.Parameters; int index = Array.IndexOf(params, "\\v"); if (index >= 0) { string[] newParams = new string[params.Length-1]; if (index > 0) { Array.Copy(params, newParams, index); } if (params.Length-index-1 > 0) { Array.Copy(params, index+1, newParams, index, params.Length-index-1); } appField.Parameters = newParams; } }
[Visual Basic] For Each appField As TXTextControl.ApplicationField In TextControl1.ApplicationFields Dim params() As String = appField.Parameters Dim index As Integer = Array.IndexOf(params, "\v") If index >= 0 Then Dim newParams(params.Length - 2) As String If index > 0 Then Array.Copy(params, newParams, index) End If If params.Length - index - 1 > 0 Then Array.Copy(params, index + 1, newParams, index, params.Length - index - 1) End If appField.Parameters = newParams End If Next

TX Text Control also supports importing and exporting of Microsoft Word form fields. These fields can be imported specifying FORMCHECKBOX, FORMDROPDOWN or FORMTEXTBOX with the LoadSettings.ApplicationFieldTypeNames property. Microsoft Word form fields also have parameters, that can be edited in Microsoft Word with the Form Field Options dialog box. TX Text Control provides these parameters through the ApplicationField.Parameters property using the XML syntax of the Office Open XML Format.

To learn more about the Office Open XML Format, please refer to the official ECMA standard:

http://www.textcontrol.com/in/ecmaofficeopenxmlfileformats.htm

 
 
 

Products

Support

Downloads

Corporate

Buy Now