Skip to content

Commit 13cf80b

Browse files
Add doc variable in display bar code
1 parent 6ec39a7 commit 13cf80b

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35309.182
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Add-DocVariable-in-DisplayBarCode", "Add-DocVariable-in-DisplayBarCode\Add-DocVariable-in-DisplayBarCode.csproj", "{193797D1-5655-4C15-BB0E-5B73BD9D142B}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{193797D1-5655-4C15-BB0E-5B73BD9D142B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{193797D1-5655-4C15-BB0E-5B73BD9D142B}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{193797D1-5655-4C15-BB0E-5B73BD9D142B}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{193797D1-5655-4C15-BB0E-5B73BD9D142B}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {AC73F271-E4F6-4A2C-86B8-C1297A31B1EE}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Add_DocVariable_in_DisplayBarCode</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Output\.gitkeep">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
</ItemGroup>
20+
21+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)