Skip to content

Commit d2b7480

Browse files
Replace-field-with-table
1 parent f9057d1 commit d2b7480

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-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.9.34622.214
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Replace-field-with-table", "Replace-field-with-table\Replace-field-with-table.csproj", "{AD2A243A-F1F9-410E-BE4E-592839D52919}"
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+
{AD2A243A-F1F9-410E-BE4E-592839D52919}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{AD2A243A-F1F9-410E-BE4E-592839D52919}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{AD2A243A-F1F9-410E-BE4E-592839D52919}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{AD2A243A-F1F9-410E-BE4E-592839D52919}.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 = {5A0B4C55-E8AA-4F27-BB36-8B562758FB60}
24+
EndGlobalSection
25+
EndGlobal
Binary file not shown.
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
using Syncfusion.DocIO.DLS;
2+
using Syncfusion.DocIO;
3+
4+
namespace Replace_field_with_table
5+
{
6+
internal class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
// Open the existing Word document using a FileStream.
11+
using (FileStream docStream = new FileStream(Path.GetFullPath(@"../../../Data/InputDocument.docx"), FileMode.Open, FileAccess.Read))
12+
{
13+
// Load the existing Word document.
14+
using (WordDocument document = new WordDocument(docStream, FormatType.Docx))
15+
{
16+
// Find the first sequence field (SEQ field) in the document.
17+
WSeqField seqField = document.FindItemByProperty(EntityType.SeqField, "", "") as WSeqField;
18+
19+
// Get the paragraph that contains the SEQ field.
20+
WParagraph paragraph = seqField.OwnerParagraph;
21+
22+
// Get the index of the paragraph within the text body.
23+
int paraindex = paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph);
24+
25+
// Get the index of the SEQ field within the paragraph.
26+
int seqfieldIndex = paragraph.ChildEntities.IndexOf(seqField);
27+
28+
// Clone the paragraph that contains the SEQ field.
29+
WParagraph clonedParagraph = seqField.OwnerParagraph.Clone() as WParagraph;
30+
31+
// Remove all entities before the SEQ field index in the cloned paragraph.
32+
for (int i = seqfieldIndex; i >= 0; i--)
33+
{
34+
clonedParagraph.ChildEntities.RemoveAt(i);
35+
}
36+
37+
// Remove all entities from the SEQ field index onward in the original paragraph.
38+
for (int j = paragraph.ChildEntities.Count - 1; j >= seqfieldIndex; j--)
39+
{
40+
paragraph.ChildEntities.RemoveAt(j);
41+
}
42+
43+
// Generate a sample table.
44+
IWTable table = GetTable();
45+
46+
// Clone the generated table.
47+
IWTable table1 = table.Clone() as IWTable;
48+
49+
// Insert the cloned table after the paragraph containing the SEQ field.
50+
paragraph.OwnerTextBody.ChildEntities.Insert(paraindex + 1, table1);
51+
52+
// Insert the modified cloned paragraph after the inserted table.
53+
paragraph.OwnerTextBody.ChildEntities.Insert(paraindex + 2, clonedParagraph);
54+
55+
// Save the modified document to a new file.
56+
using (FileStream docStream1 = new FileStream(Path.GetFullPath(@"../../../Data/ResultDocument.docx"), FileMode.Create, FileAccess.Write))
57+
{
58+
document.Save(docStream1, FormatType.Docx);
59+
}
60+
}
61+
}
62+
}
63+
64+
// Method to generate a sample table.
65+
static IWTable GetTable()
66+
{
67+
// Creates a new Word document.
68+
WordDocument document = new WordDocument();
69+
70+
// Adds a section into the Word document.
71+
IWSection section = document.AddSection();
72+
73+
// Adds a paragraph with the text "Price Details" in bold and Arial font.
74+
IWTextRange textRange = section.AddParagraph().AppendText("Price Details");
75+
textRange.CharacterFormat.FontName = "Arial";
76+
textRange.CharacterFormat.FontSize = 12;
77+
textRange.CharacterFormat.Bold = true;
78+
79+
// Adds an empty paragraph (for spacing).
80+
section.AddParagraph();
81+
82+
// Adds a new table with 3 rows and 2 columns.
83+
IWTable table = section.AddTable();
84+
table.ResetCells(3, 2);
85+
86+
// Adds the column headers to the first row.
87+
textRange = table[0, 0].AddParagraph().AppendText("Item");
88+
textRange.CharacterFormat.FontName = "Arial";
89+
textRange.CharacterFormat.FontSize = 12;
90+
textRange.CharacterFormat.Bold = true;
91+
92+
textRange = table[0, 1].AddParagraph().AppendText("Price($)");
93+
textRange.CharacterFormat.FontName = "Arial";
94+
textRange.CharacterFormat.FontSize = 12;
95+
textRange.CharacterFormat.Bold = true;
96+
97+
// Adds the first item and its price to the second row.
98+
textRange = table[1, 0].AddParagraph().AppendText("Cycle 1");
99+
textRange.CharacterFormat.FontName = "Arial";
100+
textRange.CharacterFormat.FontSize = 10;
101+
102+
textRange = table[1, 1].AddParagraph().AppendText("500");
103+
textRange.CharacterFormat.FontName = "Arial";
104+
textRange.CharacterFormat.FontSize = 10;
105+
106+
// Adds the second item and its price to the third row.
107+
textRange = table[2, 0].AddParagraph().AppendText("Cycle 2");
108+
textRange.CharacterFormat.FontName = "Arial";
109+
textRange.CharacterFormat.FontSize = 10;
110+
111+
textRange = table[2, 1].AddParagraph().AppendText("300");
112+
textRange.CharacterFormat.FontName = "Arial";
113+
textRange.CharacterFormat.FontSize = 10;
114+
115+
// Returns the generated table.
116+
return table;
117+
}
118+
}
119+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Replace_field_with_table</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="26.2.9" />
13+
</ItemGroup>
14+
15+
</Project>

0 commit comments

Comments
 (0)