22using Syncfusion . DocIO ;
33
44// Creates a new instance of WordDocument (empty Word document).
5- WordDocument wordDocument = new WordDocument ( ) ;
6- // Adds a section to the document.
7- IWSection section = wordDocument . AddSection ( ) ;
8- // Adds a paragraph to the section.
9- IWParagraph paragraph = section . AddParagraph ( ) ;
10- // Adds a document variable named "Barcode" with a value of "123456".
11- wordDocument . Variables . Add ( "Barcode" , "123456" ) ;
12- // Appends a field to the paragraph. The field type is set as FieldUnknown initially.
13- WField field = paragraph . AppendField ( "DISPLAYBARCODE " , Syncfusion . DocIO . FieldType . FieldUnknown ) as WField ;
14- // Specifies the field code for the barcode.
15- InsertBarcodeField ( paragraph , field ) ;
16- // Updates all document fields to reflect changes.
17- wordDocument . UpdateDocumentFields ( ) ;
18- // Saves the output Word document to a specified file stream in DOCX format.
19- FileStream stream = new FileStream ( Path . GetFullPath ( @"Output/Result.docx" ) , FileMode . OpenOrCreate ) ;
20- wordDocument . Save ( stream , FormatType . Docx ) ;
21- // Closes the file stream to release the resource.
22- stream . Close ( ) ;
5+ using ( WordDocument wordDocument = new WordDocument ( ) )
6+ {
7+ // Adds a section to the document.
8+ IWSection section = wordDocument . AddSection ( ) ;
9+ // Adds a paragraph to the section.
10+ IWParagraph paragraph = section . AddParagraph ( ) ;
11+ // Adds a document variable named "Barcode" with a value of "123456".
12+ wordDocument . Variables . Add ( "Barcode" , "123456" ) ;
13+ // Appends a field to the paragraph. The field type is set as FieldUnknown initially.
14+ WField field = paragraph . AppendField ( "DISPLAYBARCODE " , Syncfusion . DocIO . FieldType . FieldUnknown ) as WField ;
15+ // Specifies the field code for the barcode.
16+ InsertBarcodeField ( paragraph , field ) ;
17+ // Updates all document fields to reflect changes.
18+ wordDocument . UpdateDocumentFields ( ) ;
19+ // Saves the output Word document to a specified file stream in DOCX format.
20+ using ( FileStream stream = new FileStream ( Path . GetFullPath ( @"Output/Result.docx" ) , FileMode . OpenOrCreate ) )
21+ {
22+ wordDocument . Save ( stream , FormatType . Docx ) ;
23+ }
24+ }
2325
2426/// <summary>
2527/// Inserts the field code with a nested field.
@@ -28,16 +30,12 @@ static void InsertBarcodeField(IWParagraph paragraph, WField? field)
2830{
2931 // Get the index of the field in the paragraph to insert the field code.
3032 int fieldIndex = paragraph . Items . IndexOf ( field ) + 1 ;
31-
3233 // Add the field code for the barcode.
3334 field . FieldCode = "DISPLAYBARCODE " ;
34-
3535 // Increment field index for further insertion.
3636 fieldIndex ++ ;
37-
3837 // Insert document variables into the field code at the specified index.
3938 InsertDocVariables ( "Barcode" , ref fieldIndex , paragraph ) ;
40-
4139 // Insert the barcode type (e.g., CODE128) into the paragraph.
4240 InsertText ( " CODE128" , ref fieldIndex , paragraph as WParagraph ) ;
4341}
@@ -50,10 +48,8 @@ static void InsertText(string text, ref int fieldIndex, WParagraph paragraph)
5048 // Create a new text range with the specified text.
5149 WTextRange textRange = new WTextRange ( paragraph . Document ) ;
5250 textRange . Text = text ;
53-
5451 // Insert the text range as a field code item at the specified index.
5552 paragraph . Items . Insert ( fieldIndex , textRange ) ;
56-
5753 // Increment the field index for subsequent insertions.
5854 fieldIndex ++ ;
5955}
@@ -66,13 +62,10 @@ static void InsertDocVariables(string fieldName, ref int fieldIndex, IWParagraph
6662 // Create a new paragraph to hold the document variable field.
6763 WParagraph para = new WParagraph ( paragraph . Document ) ;
6864 para . AppendField ( fieldName , FieldType . FieldDocVariable ) ;
69-
7065 // Get the count of child entities in the paragraph (should be 1 for the field).
7166 int count = para . ChildEntities . Count ;
72-
7367 // Insert the field into the original paragraph, ensuring the complete field structure is added.
7468 paragraph . ChildEntities . Insert ( fieldIndex , para . ChildEntities [ 0 ] ) ;
75-
7669 // Increment the field index based on the count of inserted entities.
7770 fieldIndex += count ;
7871}
0 commit comments