How to Add DocVariable Inside DISPLAYBARCODE Field in Core Word?
Syncfusion Essential DocIO is a .NET Word library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can add DocVariable field inside DISPLAYBARCODE field in Word document using C#.
Steps to add DocVariable field inside DISPLAYBARCODE field in Word document:
- Create a new .NET Core console application project.
- Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your project from NuGet.org.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion license key in your application to use the components without trail message.
- Include the following namespaces in Program.cs file
C#
using Syncfusion.DocIO;
- Use the following code example to add DocVariable field inside DISPLAYBARCODE field in Word document.
C#
// Creates a new instance of WordDocument (empty Word document).
using (WordDocument wordDocument = new WordDocument())
{
// Adds a section to the document.
IWSection section = wordDocument.AddSection();
// Adds a paragraph to the section.
IWParagraph paragraph = section.AddParagraph();
// Adds a document variable named "Barcode" with a value of "123456".
wordDocument.Variables.Add("Barcode", "123456");
// Appends a field to the paragraph. The field type is set as FieldUnknown initially.
WField field = paragraph.AppendField("DISPLAYBARCODE ", Syncfusion.DocIO.FieldType.FieldUnknown) as WField;
// Specifies the field code for the barcode.
InsertBarcodeField(paragraph, field);
// Updates all document fields to reflect changes.
wordDocument.UpdateDocumentFields();
// Saves the output Word document to a specified file stream in DOCX format.
using (FileStream stream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.OpenOrCreate))
{
wordDocument.Save(stream, FormatType.Docx);
}
}
- Use the following helper method to insert the field code with nested field.
C#
/// <summary>
/// Inserts the field code with a nested field.
/// </summary>
static void InsertBarcodeField(IWParagraph paragraph, WField? field)
{
// Get the index of the field in the paragraph to insert the field code.
int fieldIndex = paragraph.Items.IndexOf(field) + 1;
// Add the field code for the barcode.
field.FieldCode = "DISPLAYBARCODE ";
// Increment field index for further insertion.
fieldIndex++;
// Insert document variables into the field code at the specified index.
InsertDocVariables("Barcode", ref fieldIndex, paragraph);
// Insert the barcode type (e.g., CODE128) into the paragraph.
InsertText(" CODE128", ref fieldIndex, paragraph as WParagraph);
}
- Use the following helper method to insert text such as operators or identifiers into the paragraph.
C#
/// <summary>
/// Inserts text such as operators or identifiers into the paragraph.
/// </summary>
static void InsertText(string text, ref int fieldIndex, WParagraph paragraph)
{
// Create a new text range with the specified text.
WTextRange textRange = new WTextRange(paragraph.Document);
textRange.Text = text;
// Insert the text range as a field code item at the specified index.
paragraph.Items.Insert(fieldIndex, textRange);
// Increment the field index for subsequent insertions.
fieldIndex++;
}
- Use the following helper method to insert document variables at the given index in the specified paragraph.
C#
/// <summary>
/// Inserts document variables at the given index in the specified paragraph.
/// </summary>
static void InsertDocVariables(string fieldName, ref int fieldIndex, IWParagraph paragraph)
{
// Create a new paragraph to hold the document variable field.
WParagraph para = new WParagraph(paragraph.Document);
para.AppendField(fieldName, FieldType.FieldDocVariable);
// Get the count of child entities in the paragraph (should be 1 for the field).
int count = para.ChildEntities.Count;
// Insert the field into the original paragraph, ensuring the complete field structure is added.
paragraph.ChildEntities.Insert(fieldIndex, para.ChildEntities[0]);
// Increment the field index based on the count of inserted entities.
fieldIndex += count;
}
You can download a complete working sample to add DocVariable field inside DISPLAYBARCODE field in Word document from the GitHub.
By executing the program, you will get the Word document as follows.
Take a moment to peruse the documentation where you can find basic Word document processing options along with the features like mail merge, merge, split, and compare Word documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.
Conclusion
I hope you enjoyed learning how to add DocVariable field inside DISPLAYBARCODE field in Core Word.
You can refer to our ASP.NET Core DocIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core DocIO example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!