Fields

Fields in Word document are placeholders for data that might change on field update. Fields are represented by WField, WFieldMark instance in DocIO. A field in Word document contains field codes, field separator, field result, and field end.

To know various types of Microsoft Word supported fields and its syntax,refer MSDN article

From v16.1.0.24, the entire field code is included in Document Object Model(DOM). Hence the adding a field will automatically include below elements in DOM,

  1. WField – represents the starting of Field.

  2. ParagraphItem – represents the Field code.

  3. WFieldMark –represents the Field separator.

  4. ParagraphItem – represents the Field result.

  5. WFieldMark – represents the end of Field.

Find more information about migration changes from here.

Adding fields

You can add a field in a Word document by using AppendField method of WParagraph class.

The following code example illustrates how to add a field in Word document.

//Creates an instance of WordDocument class (Empty Word Document)

WordDocument document = new WordDocument();

//Adds a new section into the Word Document

IWSection section = document.AddSection();

//Adds a new paragraph into Word document and appends text into paragraph

IWParagraph paragraph = section.AddParagraph();

paragraph.AppendText("Today's Date: ");

//Adds the new Date field in Word document with field name and its type.

WField field = paragraph.AppendField("Date", FieldType.FieldDate) as WField;

//Field code used to describe how to display the date

field.FieldCode = @"DATE  \@" + "\"MMMM d, yyyy\""; 

//Saves the document in the given name and format

document.Save("Sample.docx", FormatType.Docx);

//Releases the resources occupied by WordDocument instance

document.Close();
'Creates an instance of WordDocument class (Empty Word Document)

Dim document As New WordDocument()

'Adds a new section into the Word Document

Dim section As IWSection = document.AddSection()

'Adds a new paragraph into Word document and appends text into paragraph

Dim paragraph As IWParagraph = section.AddParagraph()

paragraph.AppendText("Today's Date: ")

'Adds the new Date field in Word document with field name and its type.

Dim field As WField = TryCast(paragraph.AppendField("Date", FieldType.FieldDate), WField)

'Field code used to describe how to display the date

field.FieldCode = "DATE  \@" + """MMMM d, yyyy"""

'Saves the document in the given name and format

document.Save("Sample.docx", FormatType.Docx)

'Releases the resources occupied by WordDocument instance

document.Close()
//Creates an instance of WordDocument class (Empty Word Document)

WordDocument document = new WordDocument();

//Adds a new section into the Word Document

IWSection section = document.AddSection();

//Adds a new paragraph into Word document and appends text into paragraph

IWParagraph paragraph = section.AddParagraph();

paragraph.AppendText("Today's Date: ");

//Adds the new Date field in Word document with field name and its type.

WField field = paragraph.AppendField("Date", FieldType.FieldDate) as WField;

//Field code used to describe how to display the date

field.FieldCode = @"DATE  \@" + "\"MMMM d, yyyy\""; 

//Saves the Word file to MemoryStream
MemoryStream stream = new MemoryStream();
await document.SaveAsync(stream, FormatType.Docx);

//Saves the stream as Word file in local machine
Save(stream, "Sample.docx");

// Saves the Word document
async void Save(MemoryStream streams, string filename)
{
	streams.Position = 0;
	StorageFile stFile;
	if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")))
	{
		FileSavePicker savePicker = new FileSavePicker();
		savePicker.DefaultFileExtension = ".docx";
		savePicker.SuggestedFileName = filename;
		savePicker.FileTypeChoices.Add("Word Documents", new List<string>() { ".docx" });
		stFile = await savePicker.PickSaveFileAsync();
	}
	else
	{
		StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
		stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
	}
	if (stFile != null)
	{
		using (IRandomAccessStream zipStream = await stFile.OpenAsync(FileAccessMode.ReadWrite))
		{
			// Write compressed data from memory to file
			using (Stream outstream = zipStream.AsStreamForWrite())
			{
				byte[] buffer = streams.ToArray();
				outstream.Write(buffer, 0, buffer.Length);
				outstream.Flush();
			}
		}
	}
	// Launch the saved Word file
	await Windows.System.Launcher.LaunchFileAsync(stFile);
}
//Creates an instance of WordDocument class (Empty Word Document)

WordDocument document = new WordDocument();

//Adds a new section into the Word Document

IWSection section = document.AddSection();

//Adds a new paragraph into Word document and appends text into paragraph

IWParagraph paragraph = section.AddParagraph();

paragraph.AppendText("Today's Date: ");

//Adds the new Date field in Word document with field name and its type.

WField field = paragraph.AppendField("Date", FieldType.FieldDate) as WField;

//Field code used to describe how to display the date

field.FieldCode = @"DATE  \@" + "\"MMMM d, yyyy\""; 

//Saves the Word document to MemoryStream
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
stream.Position = 0;

//Download Word document in the browser
return File(stream, "application/msword", "Sample.docx");
//Creates an instance of WordDocument class (Empty Word Document)

WordDocument document = new WordDocument();

//Adds a new section into the Word Document

IWSection section = document.AddSection();

//Adds a new paragraph into Word document and appends text into paragraph

IWParagraph paragraph = section.AddParagraph();

paragraph.AppendText("Today's Date: ");

//Adds the new Date field in Word document with field name and its type.

WField field = paragraph.AppendField("Date", FieldType.FieldDate) as WField;

//Field code used to describe how to display the date

field.FieldCode = @"DATE  \@" + "\"MMMM d, yyyy\""; 

//Saves the Word document to  MemoryStream
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);

//Save the stream as a file in the device and invoke it for viewing
Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Sample.docx", "application/msword", stream);

Formatting Fields

You can format the field instances added to the Word document, by iterating items from field start to end.

The following code example illustrates how to format the field in Word document.

//Creates an instance of a WordDocument 

WordDocument document = new WordDocument();

//Adds one section and one paragraph to the document

document.EnsureMinimal();

//Adds the new Page field in Word document with field name and its type.

IWField field = document.LastParagraph.AppendField("Page", FieldType.FieldPage);

IEntity entity = field;

//Iterates to sibling items until Field End 

while (entity.NextSibling != null)

{

if (entity is WTextRange)

//Sets character format for text ranges. 

(entity as WTextRange).CharacterFormat.FontSize = 6;

else if ((entity is WFieldMark) && (entity as WFieldMark).Type == FieldMarkType.FieldEnd)

break;

//Gets next sibling item.

entity = entity.NextSibling;

}

//Saves and closes the WordDocument instance.

document.Save("Template.docx", FormatType.Docx);

document.Close();
//Creates an instance of a WordDocument 

WordDocument document = new WordDocument();

//Adds one section and one paragraph to the document

document.EnsureMinimal();

//Adds the new Page field in Word document with field name and its type.

IWField field = document.LastParagraph.AppendField("Page", FieldType.FieldPage);

IEntity entity = field;

//Iterates to sibling items until Field End 

while (entity.NextSibling != null)

{

if (entity is WTextRange)

//Sets character format for text ranges. 

(entity as WTextRange).CharacterFormat.FontSize = 6;

else if ((entity is WFieldMark) && (entity as WFieldMark).Type == FieldMarkType.FieldEnd)

break;

//Gets next sibling item.

entity = entity.NextSibling;

}

//Saves the Word file to MemoryStream
MemoryStream stream = new MemoryStream();
await document.SaveAsync(stream, FormatType.Docx);

//Saves the stream as Word file in local machine
Save(stream, "Template.docx");

// Saves the Word document
async void Save(MemoryStream streams, string filename)
{
	streams.Position = 0;
	StorageFile stFile;
	if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")))
	{
		FileSavePicker savePicker = new FileSavePicker();
		savePicker.DefaultFileExtension = ".docx";
		savePicker.SuggestedFileName = filename;
		savePicker.FileTypeChoices.Add("Word Documents", new List<string>() { ".docx" });
		stFile = await savePicker.PickSaveFileAsync();
	}
	else
	{
		StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
		stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
	}
	if (stFile != null)
	{
		using (IRandomAccessStream zipStream = await stFile.OpenAsync(FileAccessMode.ReadWrite))
		{
			// Write compressed data from memory to file
			using (Stream outstream = zipStream.AsStreamForWrite())
			{
				byte[] buffer = streams.ToArray();
				outstream.Write(buffer, 0, buffer.Length);
				outstream.Flush();
			}
		}
	}
	// Launch the saved Word file
	await Windows.System.Launcher.LaunchFileAsync(stFile);
}
//Creates an instance of a WordDocument 

WordDocument document = new WordDocument();

//Adds one section and one paragraph to the document

document.EnsureMinimal();

//Adds the new Page field in Word document with field name and its type.

IWField field = document.LastParagraph.AppendField("Page", FieldType.FieldPage);

IEntity entity = field;

//Iterates to sibling items until Field End 

while (entity.NextSibling != null)

{

if (entity is WTextRange)

//Sets character format for text ranges. 

(entity as WTextRange).CharacterFormat.FontSize = 6;

else if ((entity is WFieldMark) && (entity as WFieldMark).Type == FieldMarkType.FieldEnd)

break;

//Gets next sibling item.

entity = entity.NextSibling;

}

//Saves the Word document to MemoryStream
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
stream.Position = 0;

//Download Word document in the browser
return File(stream, "application/msword", "Template.docx");
//Creates an instance of a WordDocument 

WordDocument document = new WordDocument();

//Adds one section and one paragraph to the document

document.EnsureMinimal();

//Adds the new Page field in Word document with field name and its type.

IWField field = document.LastParagraph.AppendField("Page", FieldType.FieldPage);

IEntity entity = field;

//Iterates to sibling items until Field End 

while (entity.NextSibling != null)

{

if (entity is WTextRange)

//Sets character format for text ranges. 

(entity as WTextRange).CharacterFormat.FontSize = 6;

else if ((entity is WFieldMark) && (entity as WFieldMark).Type == FieldMarkType.FieldEnd)

break;

//Gets next sibling item.

entity = entity.NextSibling;

}

//Saves the Word document to  MemoryStream
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);

//Save the stream as a file in the device and invoke it for viewing
Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Template.docx", "application/msword", stream);

Updating fields

Field updating engine calculates the resultant value based on the field code information and updates the field result with the new value. You can update the following fields by using DocIO:

  • = (formula field)
  • DATE
  • TIME
  • DOCVARIABLE
  • DOCPROPERTY
  • COMPARE
  • IF
  • NEXTIF
  • MERGEREC
  • MERGESEQ
  • SECTION
  • NUMPAGES
  • TITLE
  • Cross-Reference

The following are the known limitations:

  • Updating of NUMPAGES field and Cross Reference field with Page number and Paragraph number options are not supported in Silverlight, WinRT, Universal, Windows Phone and Xamarin applications.
  • Currently group shapes, drawing canvas, and table auto resizing are not supported in Word to PDF lay outing, and this may lead to update incorrect page number and total number of pages.

The following code example illustrate how to update the fields present in Word document.

//Loads an existing Word document into DocIO instance 

WordDocument document = new WordDocument("Input.docx", FormatType.Docx);

//Updates the fields present in a document.

document.UpdateDocumentFields();

document.Save("Result.docx", FormatType.Docx);

document.Close();
'Loads an existing Word document into DocIO instance 

Dim document As New WordDocument("Input.docx", FormatType.Docx)

'Updates the fields present in a document.

document.UpdateDocumentFields()

document.Save("Result.docx", FormatType.Docx)

document.Close()
//Loads an existing Word document into DocIO instance 

Assembly assembly = typeof(App).GetTypeInfo().Assembly;
WordDocument document = new WordDocument(assembly.GetManifestResourceStream("Sample.Assets.Input.docx"), FormatType.Docx);

//Updates the fields present in a document.

document.UpdateDocumentFields();

//Saves the Word file to MemoryStream
MemoryStream stream = new MemoryStream();
await document.SaveAsync(stream, FormatType.Docx);

//Saves the stream as Word file in local machine
Save(stream, "Result.docx");

// Saves the Word document
async void Save(MemoryStream streams, string filename)
{
	streams.Position = 0;
	StorageFile stFile;
	if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")))
	{
		FileSavePicker savePicker = new FileSavePicker();
		savePicker.DefaultFileExtension = ".docx";
		savePicker.SuggestedFileName = filename;
		savePicker.FileTypeChoices.Add("Word Documents", new List<string>() { ".docx" });
		stFile = await savePicker.PickSaveFileAsync();
	}
	else
	{
		StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
		stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
	}
	if (stFile != null)
	{
		using (IRandomAccessStream zipStream = await stFile.OpenAsync(FileAccessMode.ReadWrite))
		{
			// Write compressed data from memory to file
			using (Stream outstream = zipStream.AsStreamForWrite())
			{
				byte[] buffer = streams.ToArray();
				outstream.Write(buffer, 0, buffer.Length);
				outstream.Flush();
			}
		}
	}
	// Launch the saved Word file
	await Windows.System.Launcher.LaunchFileAsync(stFile);
}
//Loads an existing Word document into DocIO instance 

FileStream fileStreamPath = new FileStream("Input.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx);

//Updates the fields present in a document.

document.UpdateDocumentFields();

//Saves the Word document to MemoryStream
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
stream.Position = 0;

//Download Word document in the browser
return File(stream, "application/msword", "Result.docx");
//Loads an existing Word document into DocIO instance 

Assembly assembly = typeof(App).GetTypeInfo().Assembly;
WordDocument document = new WordDocument(assembly.GetManifestResourceStream("GettingStarted.Data.Input.docx"), FormatType.Docx);

//Updates the fields present in a document.

document.UpdateDocumentFields();

//Saves the Word document to  MemoryStream
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);

//Save the stream as a file in the device and invoke it for viewing
Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Result.docx", "application/msword", stream);

IF Field

If field compares two values and updates the field result with true text, when comparison succeeds otherwise false text.

To know more about If field and its syntax in Microsoft Word, refer MSDN article

The following code example illustrates how to add an If field in Word document.

WordDocument document = new WordDocument();

IWSection section = document.AddSection();

IWParagraph paragraph = section.AddParagraph();

paragraph.AppendText("If field which uses string of characters in expression");

paragraph = section.AddParagraph();

//Creates the new instance of IF field

WIfField field = paragraph.AppendField("If", FieldType.FieldIf) as WIfField;

//Specifies the expression, true and false statement in field code

field.FieldCode = "IF \"True\" = \"True\" \"The given statement is Correct\" \"The given statement is Wrong\"";

paragraph = section.AddParagraph();

paragraph.AppendText("If field which uses numbers in expression");

paragraph = section.AddParagraph();

//Creates the new instance of IF field

field = paragraph.AppendField("If", FieldType.FieldIf) as WIfField;

//Specifies the expression, true and false statement in field code

field.FieldCode = "IF 100 >= 1000 \"The given statement is Correct\" \"The given statement is Wrong\"";

//Updates the document fields

document.UpdateDocumentFields();

document.Save("Sample.docx", FormatType.Docx);

document.Close();
Dim document As New WordDocument()

Dim section As IWSection = document.AddSection()

Dim paragraph As IWParagraph = section.AddParagraph()

paragraph.AppendText("If field which uses string of characters in expression")

paragraph = section.AddParagraph()

'Creates the new instance of IF field

Dim field As WIfField = TryCast(paragraph.AppendField("If", FieldType.FieldIf), WIfField)

'Specifies the expression, true and false statement in field code

field.FieldCode = "IF ""True"" = ""True"" ""The given statement is Correct"" ""The given statement is Wrong"""

paragraph = section.AddParagraph()

paragraph.AppendText("If field which uses numbers in expression")

paragraph = section.AddParagraph()

'Creates the new instance of IF field

field = TryCast(paragraph.AppendField("If", FieldType.FieldIf), WIfField)

'Specify the expression, true and false statement in field code

field.FieldCode = "IF 100 >= 1000 ""The given statement is Correct"" ""The given statement is Wrong"""

'Updates the document fields

document.UpdateDocumentFields()

document.Save("Sample.docx", FormatType.Docx)

document.Close()
//Creates an instance of a WordDocument

WordDocument document = new WordDocument();

//Adds a new section into the Word Document

IWSection section = document.AddSection();

//Adds a new paragraph into Word document and appends text into paragraph

IWParagraph paragraph = section.AddParagraph();

paragraph.AppendText("If field which uses string of characters in expression");

paragraph = section.AddParagraph();

//Creates the new instance of IF field

WIfField field = paragraph.AppendField("If", FieldType.FieldIf) as WIfField;

//Specifies the expression, true and false statement in field code

field.FieldCode = "IF \"True\" = \"True\" \"The given statement is Correct\" \"The given statement is Wrong\"";

paragraph = section.AddParagraph();

paragraph.AppendText("If field which uses numbers in expression");

paragraph = section.AddParagraph();

//Creates the new instance of IF field

field = paragraph.AppendField("If", FieldType.FieldIf) as WIfField;

//Specifies the expression, true and false statement in field code

field.FieldCode = "IF 100 >= 1000 \"The given statement is Correct\" \"The given statement is Wrong\"";

//Updates the document fields

document.UpdateDocumentFields();

//Saves the Word file to MemoryStream
MemoryStream stream = new MemoryStream();
await document.SaveAsync(stream, FormatType.Docx);

//Saves the stream as Word file in local machine
Save(stream, "Sample.docx");

// Saves the Word document
async void Save(MemoryStream streams, string filename)
{
	streams.Position = 0;
	StorageFile stFile;
	if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")))
	{
		FileSavePicker savePicker = new FileSavePicker();
		savePicker.DefaultFileExtension = ".docx";
		savePicker.SuggestedFileName = filename;
		savePicker.FileTypeChoices.Add("Word Documents", new List<string>() { ".docx" });
		stFile = await savePicker.PickSaveFileAsync();
	}
	else
	{
		StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
		stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
	}
	if (stFile != null)
	{
		using (IRandomAccessStream zipStream = await stFile.OpenAsync(FileAccessMode.ReadWrite))
		{
			// Write compressed data from memory to file
			using (Stream outstream = zipStream.AsStreamForWrite())
			{
				byte[] buffer = streams.ToArray();
				outstream.Write(buffer, 0, buffer.Length);
				outstream.Flush();
			}
		}
	}
	// Launch the saved Word file
	await Windows.System.Launcher.LaunchFileAsync(stFile);
}
//Creates an instance of a WordDocument

WordDocument document = new WordDocument();

//Adds a new section into the Word Document

IWSection section = document.AddSection();

//Adds a new paragraph into Word document and appends text into paragraph

IWParagraph paragraph = section.AddParagraph();

paragraph.AppendText("If field which uses string of characters in expression");

paragraph = section.AddParagraph();

//Creates the new instance of IF field

WIfField field = paragraph.AppendField("If", FieldType.FieldIf) as WIfField;

//Specifies the expression, true and false statement in field code

field.FieldCode = "IF \"True\" = \"True\" \"The given statement is Correct\" \"The given statement is Wrong\"";

paragraph = section.AddParagraph();

paragraph.AppendText("If field which uses numbers in expression");

paragraph = section.AddParagraph();

//Creates the new instance of IF field

field = paragraph.AppendField("If", FieldType.FieldIf) as WIfField;

//Specifies the expression, true and false statement in field code

field.FieldCode = "IF 100 >= 1000 \"The given statement is Correct\" \"The given statement is Wrong\"";

//Updates the document fields

document.UpdateDocumentFields();

//Saves the Word document to MemoryStream
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
stream.Position = 0;

//Download Word document in the browser
return File(stream, "application/msword", "Sample.docx");
//Creates an instance of a WordDocument

WordDocument document = new WordDocument();

//Adds a new section into the Word Document

IWSection section = document.AddSection();

//Adds a new paragraph into Word document and appends text into paragraph

IWParagraph paragraph = section.AddParagraph();

paragraph.AppendText("If field which uses string of characters in expression");

paragraph = section.AddParagraph();

//Creates the new instance of IF field

WIfField field = paragraph.AppendField("If", FieldType.FieldIf) as WIfField;

//Specifies the expression, true and false statement in field code

field.FieldCode = "IF \"True\" = \"True\" \"The given statement is Correct\" \"The given statement is Wrong\"";

paragraph = section.AddParagraph();

paragraph.AppendText("If field which uses numbers in expression");

paragraph = section.AddParagraph();

//Creates the new instance of IF field

field = paragraph.AppendField("If", FieldType.FieldIf) as WIfField;

//Specifies the expression, true and false statement in field code

field.FieldCode = "IF 100 >= 1000 \"The given statement is Correct\" \"The given statement is Wrong\"";

//Updates the document fields

document.UpdateDocumentFields();

//Saves the Word document to  MemoryStream
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);

//Save the stream as a file in the device and invoke it for viewing
Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Sample.docx", "application/msword", stream);

Document Variables

The DocVariable field displays the value of a specified document variable in the Word document. The document variables can be added or modified using Variables property of WordDocument class.

The following code example illustrate how to add a DocVariable field in Word document.

WordDocument document = new WordDocument();

IWSection section = document.AddSection();

IWParagraph paragraph = section.AddParagraph();

paragraph.AppendText("First Name of the customer: ");

//Adds the DocVariable field with Variable name and its type

paragraph.AppendField("FirstName", FieldType.FieldDocVariable);

paragraph = section.AddParagraph();

paragraph.AppendText("Last Name of the customer: ");

//Adds the DocVariable field with Variable name and its type

paragraph.AppendField("LastName", FieldType.FieldDocVariable);

//Adds the value for variable in WordDocument.Variable collection

document.Variables.Add("FirstName", "Jeff");

document.Variables.Add("LastName", "Smith");

//Updates the document fields

document.UpdateDocumentFields();

document.Save("Sample.docx", FormatType.Docx);

document.Close();
Dim document As New WordDocument()

Dim section As IWSection = document.AddSection()

Dim paragraph As IWParagraph = section.AddParagraph()

paragraph.AppendText("First Name of the customer: ")

'Adds the DocVariable field with Variable name and its type

paragraph.AppendField("FirstName", FieldType.FieldDocVariable)

paragraph = section.AddParagraph()

paragraph.AppendText("Last Name of the customer: ")

'Adds the DocVariable field with Variable name and its type

paragraph.AppendField("LastName", FieldType.FieldDocVariable)

'Adds the value for variable in WordDocument.Variable collection

document.Variables.Add("FirstName", "Jeff")

document.Variables.Add("LastName", "Smith")

'Updates the document fields

document.UpdateDocumentFields()

document.Save("Sample.docx", FormatType.Docx)

document.Close()
//Creates an instance of a WordDocument

WordDocument document = new WordDocument();

//Adds a new section into the Word Document

IWSection section = document.AddSection();

//Adds a new paragraph into Word document and appends text into paragraph

IWParagraph paragraph = section.AddParagraph();

paragraph.AppendText("First Name of the customer: ");

//Adds the DocVariable field with Variable name and its type

paragraph.AppendField("FirstName", FieldType.FieldDocVariable);

paragraph = section.AddParagraph();

paragraph.AppendText("Last Name of the customer: ");

//Adds the DocVariable field with Variable name and its type

paragraph.AppendField("LastName", FieldType.FieldDocVariable);

//Adds the value for variable in WordDocument.Variable collection

document.Variables.Add("FirstName", "Jeff");

document.Variables.Add("LastName", "Smith");

//Updates the document fields

document.UpdateDocumentFields();

//Saves the Word file to MemoryStream
MemoryStream stream = new MemoryStream();
await document.SaveAsync(stream, FormatType.Docx);

//Saves the stream as Word file in local machine
Save(stream, "Sample.docx");

// Saves the Word document
async void Save(MemoryStream streams, string filename)
{
	streams.Position = 0;
	StorageFile stFile;
	if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")))
	{
		FileSavePicker savePicker = new FileSavePicker();
		savePicker.DefaultFileExtension = ".docx";
		savePicker.SuggestedFileName = filename;
		savePicker.FileTypeChoices.Add("Word Documents", new List<string>() { ".docx" });
		stFile = await savePicker.PickSaveFileAsync();
	}
	else
	{
		StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
		stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
	}
	if (stFile != null)
	{
		using (IRandomAccessStream zipStream = await stFile.OpenAsync(FileAccessMode.ReadWrite))
		{
			// Write compressed data from memory to file
			using (Stream outstream = zipStream.AsStreamForWrite())
			{
				byte[] buffer = streams.ToArray();
				outstream.Write(buffer, 0, buffer.Length);
				outstream.Flush();
			}
		}
	}
	// Launch the saved Word file
	await Windows.System.Launcher.LaunchFileAsync(stFile);
}
//Creates an instance of a WordDocument

WordDocument document = new WordDocument();

//Adds a new section into the Word Document

IWSection section = document.AddSection();

//Adds a new paragraph into Word document and appends text into paragraph

IWParagraph paragraph = section.AddParagraph();

paragraph.AppendText("First Name of the customer: ");

//Adds the DocVariable field with Variable name and its type

paragraph.AppendField("FirstName", FieldType.FieldDocVariable);

paragraph = section.AddParagraph();

paragraph.AppendText("Last Name of the customer: ");

//Adds the DocVariable field with Variable name and its type

paragraph.AppendField("LastName", FieldType.FieldDocVariable);

//Adds the value for variable in WordDocument.Variable collection

document.Variables.Add("FirstName", "Jeff");

document.Variables.Add("LastName", "Smith");

//Updates the document fields

document.UpdateDocumentFields();

//Saves the Word document to MemoryStream
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
stream.Position = 0;

//Download Word document in the browser
return File(stream, "application/msword", "Sample.docx");
//Creates an instance of a WordDocument

WordDocument document = new WordDocument();

//Adds a new section into the Word Document

IWSection section = document.AddSection();

//Adds a new paragraph into Word document and appends text into paragraph

IWParagraph paragraph = section.AddParagraph();

paragraph.AppendText("First Name of the customer: ");

//Adds the DocVariable field with Variable name and its type

paragraph.AppendField("FirstName", FieldType.FieldDocVariable);

paragraph = section.AddParagraph();

paragraph.AppendText("Last Name of the customer: ");

//Adds the DocVariable field with Variable name and its type

paragraph.AppendField("LastName", FieldType.FieldDocVariable);

//Adds the value for variable in WordDocument.Variable collection

document.Variables.Add("FirstName", "Jeff");

document.Variables.Add("LastName", "Smith");

//Updates the document fields

document.UpdateDocumentFields();

//Saves the Word document to  MemoryStream
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);

//Save the stream as a file in the device and invoke it for viewing
Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Sample.docx", "application/msword", stream);

Cross Reference

A cross-reference refers to an item that appears in another location in a document. You can create cross-reference to bookmarks in a document by using AppendCrossReference method of WParagraph class.

NOTE

Essential DocIO supports to create and update cross-reference fields only for bookmarks in a document

The following code example illustrate how to append cross reference for bookmark in Word document.

WordDocument document = new WordDocument();

IWSection section = document.AddSection();

IWParagraph paragraph = section.AddParagraph();

//Adds text, bookmark start and end in the paragraph

paragraph.AppendBookmarkStart("Title");

paragraph.AppendText("Northwind Database");

paragraph.AppendBookmarkEnd("Title");

paragraph = section.AddParagraph();

paragraph.AppendText("The Northwind sample database (Northwind.mdb) is included with all versions of Access. It provides data you can experiment with and database objects that demonstrate features you might want to implement in your own databases.");

section = document.AddSection();

section.AddParagraph();

paragraph = section.AddParagraph() as WParagraph;

//Gets the collection of bookmark start in the word document

List<Entity> items = document.GetCrossReferenceItems(ReferenceType.Bookmark);

paragraph.AppendText("Bookmark Cross Reference starts here ");

//Appends the cross reference for bookmark “Title” with ContentText as reference kind

paragraph.AppendCrossReference(ReferenceType.Bookmark, ReferenceKind.ContentText, items[0], true, false, false, string.Empty);

//Updates the document Fields

document.UpdateDocumentFields();

document.Save("Sample.docx", FormatType.Docx);

document.Close();
Dim document As New WordDocument()

Dim section As IWSection = document.AddSection()

Dim paragraph As IWParagraph = section.AddParagraph()

'Adds text, bookmark start and end in the paragraph

paragraph.AppendBookmarkStart("Title")

paragraph.AppendText("Northwind Database")

paragraph.AppendBookmarkEnd("Title")

paragraph = section.AddParagraph()

paragraph.AppendText("The Northwind sample database (Northwind.mdb) is included with all versions of Access. It provides data you can experiment with and database objects that demonstrate features you might want to implement in your own databases.")

section = document.AddSection()

section.AddParagraph()

paragraph = TryCast(section.AddParagraph(), WParagraph)

'Gets the collection of bookmark start in the word document

Dim items As List(Of Entity) = document.GetCrossReferenceItems(ReferenceType.Bookmark)

paragraph.AppendText("Bookmark Cross Reference starts here ")

'Appends the cross reference for bookmark “Title” with ContentText as reference kind

paragraph.AppendCrossReference(ReferenceType.Bookmark, ReferenceKind.ContentText, items(0), True, False, False, String.Empty)

'Updates the document Fields

document.UpdateDocumentFields()

document.Save("Sample.docx", FormatType.Docx)

document.Close()
//Creates an instance of a WordDocument

WordDocument document = new WordDocument();

//Adds a new section into the Word Document

IWSection section = document.AddSection();

//Adds a new paragraph into Word document

IWParagraph paragraph = section.AddParagraph();

//Adds text, bookmark start and end in the paragraph

paragraph.AppendBookmarkStart("Title");

paragraph.AppendText("Northwind Database");

paragraph.AppendBookmarkEnd("Title");

paragraph = section.AddParagraph();

paragraph.AppendText("The Northwind sample database (Northwind.mdb) is included with all versions of Access. It provides data you can experiment with and database objects that demonstrate features you might want to implement in your own databases.");

section = document.AddSection();

section.AddParagraph();

paragraph = section.AddParagraph() as WParagraph;

//Gets the collection of bookmark start in the word document

List<Entity> items = document.GetCrossReferenceItems(ReferenceType.Bookmark);

paragraph.AppendText("Bookmark Cross Reference starts here ");

//Appends the cross reference for bookmark “Title” with ContentText as reference kind

paragraph.AppendCrossReference(ReferenceType.Bookmark, ReferenceKind.ContentText, items[0], true, false, false, string.Empty);

//Updates the document Fields

document.UpdateDocumentFields();

//Saves the Word file to MemoryStream
MemoryStream stream = new MemoryStream();
await document.SaveAsync(stream, FormatType.Docx);

//Saves the stream as Word file in local machine
Save(stream, "Sample.docx");

// Saves the Word document
async void Save(MemoryStream streams, string filename)
{
	streams.Position = 0;
	StorageFile stFile;
	if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")))
	{
		FileSavePicker savePicker = new FileSavePicker();
		savePicker.DefaultFileExtension = ".docx";
		savePicker.SuggestedFileName = filename;
		savePicker.FileTypeChoices.Add("Word Documents", new List<string>() { ".docx" });
		stFile = await savePicker.PickSaveFileAsync();
	}
	else
	{
		StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
		stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
	}
	if (stFile != null)
	{
		using (IRandomAccessStream zipStream = await stFile.OpenAsync(FileAccessMode.ReadWrite))
		{
			// Write compressed data from memory to file
			using (Stream outstream = zipStream.AsStreamForWrite())
			{
				byte[] buffer = streams.ToArray();
				outstream.Write(buffer, 0, buffer.Length);
				outstream.Flush();
			}
		}
	}
	// Launch the saved Word file
	await Windows.System.Launcher.LaunchFileAsync(stFile);
}
//Creates an instance of a WordDocument

WordDocument document = new WordDocument();

//Adds a new section into the Word Document

IWSection section = document.AddSection();

//Adds a new paragraph into Word document

IWParagraph paragraph = section.AddParagraph();

//Adds text, bookmark start and end in the paragraph

paragraph.AppendBookmarkStart("Title");

paragraph.AppendText("Northwind Database");

paragraph.AppendBookmarkEnd("Title");

paragraph = section.AddParagraph();

paragraph.AppendText("The Northwind sample database (Northwind.mdb) is included with all versions of Access. It provides data you can experiment with and database objects that demonstrate features you might want to implement in your own databases.");

section = document.AddSection();

section.AddParagraph();

paragraph = section.AddParagraph() as WParagraph;

//Gets the collection of bookmark start in the word document

List<Entity> items = document.GetCrossReferenceItems(ReferenceType.Bookmark);

paragraph.AppendText("Bookmark Cross Reference starts here ");

//Appends the cross reference for bookmark “Title” with ContentText as reference kind

paragraph.AppendCrossReference(ReferenceType.Bookmark, ReferenceKind.ContentText, items[0], true, false, false, string.Empty);

//Updates the document Fields

document.UpdateDocumentFields();

//Saves the Word document to MemoryStream
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);
stream.Position = 0;

//Download Word document in the browser
return File(stream, "application/msword", "Sample.docx");
//Creates an instance of a WordDocument

WordDocument document = new WordDocument();

//Adds a new section into the Word Document

IWSection section = document.AddSection();

//Adds a new paragraph into Word document

IWParagraph paragraph = section.AddParagraph();

//Adds text, bookmark start and end in the paragraph

paragraph.AppendBookmarkStart("Title");

paragraph.AppendText("Northwind Database");

paragraph.AppendBookmarkEnd("Title");

paragraph = section.AddParagraph();

paragraph.AppendText("The Northwind sample database (Northwind.mdb) is included with all versions of Access. It provides data you can experiment with and database objects that demonstrate features you might want to implement in your own databases.");

section = document.AddSection();

section.AddParagraph();

paragraph = section.AddParagraph() as WParagraph;

//Gets the collection of bookmark start in the word document

List<Entity> items = document.GetCrossReferenceItems(ReferenceType.Bookmark);

paragraph.AppendText("Bookmark Cross Reference starts here ");

//Appends the cross reference for bookmark “Title” with ContentText as reference kind

paragraph.AppendCrossReference(ReferenceType.Bookmark, ReferenceKind.ContentText, items[0], true, false, false, string.Empty);

//Updates the document Fields

document.UpdateDocumentFields();

//Saves the Word document to  MemoryStream
MemoryStream stream = new MemoryStream();
document.Save(stream, FormatType.Docx);

//Save the stream as a file in the device and invoke it for viewing
Xamarin.Forms.DependencyService.Get<ISave>().SaveAndView("Sample.docx", "application/msword", stream);