|
| 1 | +using Syncfusion.DocIO.DLS; |
| 2 | +using Syncfusion.DocIO; |
| 3 | + |
| 4 | +// 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(); |
| 23 | + |
| 24 | +/// <summary> |
| 25 | +/// Inserts the field code with a nested field. |
| 26 | +/// </summary> |
| 27 | +static void InsertBarcodeField(IWParagraph paragraph, WField? field) |
| 28 | +{ |
| 29 | + // Get the index of the field in the paragraph to insert the field code. |
| 30 | + int fieldIndex = paragraph.Items.IndexOf(field) + 1; |
| 31 | + |
| 32 | + // Add the field code for the barcode. |
| 33 | + field.FieldCode = "DISPLAYBARCODE "; |
| 34 | + |
| 35 | + // Increment field index for further insertion. |
| 36 | + fieldIndex++; |
| 37 | + |
| 38 | + // Insert document variables into the field code at the specified index. |
| 39 | + InsertDocVariables("Barcode", ref fieldIndex, paragraph); |
| 40 | + |
| 41 | + // Insert the barcode type (e.g., CODE128) into the paragraph. |
| 42 | + InsertText(" CODE128", ref fieldIndex, paragraph as WParagraph); |
| 43 | +} |
| 44 | + |
| 45 | +/// <summary> |
| 46 | +/// Inserts text such as operators or identifiers into the paragraph. |
| 47 | +/// </summary> |
| 48 | +static void InsertText(string text, ref int fieldIndex, WParagraph paragraph) |
| 49 | +{ |
| 50 | + // Create a new text range with the specified text. |
| 51 | + WTextRange textRange = new WTextRange(paragraph.Document); |
| 52 | + textRange.Text = text; |
| 53 | + |
| 54 | + // Insert the text range as a field code item at the specified index. |
| 55 | + paragraph.Items.Insert(fieldIndex, textRange); |
| 56 | + |
| 57 | + // Increment the field index for subsequent insertions. |
| 58 | + fieldIndex++; |
| 59 | +} |
| 60 | + |
| 61 | +/// <summary> |
| 62 | +/// Inserts document variables at the given index in the specified paragraph. |
| 63 | +/// </summary> |
| 64 | +static void InsertDocVariables(string fieldName, ref int fieldIndex, IWParagraph paragraph) |
| 65 | +{ |
| 66 | + // Create a new paragraph to hold the document variable field. |
| 67 | + WParagraph para = new WParagraph(paragraph.Document); |
| 68 | + para.AppendField(fieldName, FieldType.FieldDocVariable); |
| 69 | + |
| 70 | + // Get the count of child entities in the paragraph (should be 1 for the field). |
| 71 | + int count = para.ChildEntities.Count; |
| 72 | + |
| 73 | + // Insert the field into the original paragraph, ensuring the complete field structure is added. |
| 74 | + paragraph.ChildEntities.Insert(fieldIndex, para.ChildEntities[0]); |
| 75 | + |
| 76 | + // Increment the field index based on the count of inserted entities. |
| 77 | + fieldIndex += count; |
| 78 | +} |
0 commit comments