Skip to content

Commit 52cab57

Browse files
Merge pull request #212 from SyncfusionExamples/Replace-field-with-table
Replace-field-with-table
2 parents a1a7a3a + 76be3fa commit 52cab57

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
// Get the paragraph that contains the SEQ field.
19+
WParagraph paragraph = seqField.OwnerParagraph;
20+
// Get the index of the paragraph within the text body.
21+
int paraindex = paragraph.OwnerTextBody.ChildEntities.IndexOf(paragraph);
22+
// Get the index of the SEQ field within the paragraph.
23+
int seqfieldIndex = paragraph.ChildEntities.IndexOf(seqField);
24+
// Clone the paragraph that contains the SEQ field.
25+
WParagraph clonedParagraph = seqField.OwnerParagraph.Clone() as WParagraph;
26+
// Remove all entities before the SEQ field index in the cloned paragraph.
27+
for (int i = seqfieldIndex; i >= 0; i--)
28+
{
29+
clonedParagraph.ChildEntities.RemoveAt(i);
30+
}
31+
// Remove all entities from the SEQ field index onward in the original paragraph.
32+
for (int j = paragraph.ChildEntities.Count - 1; j >= seqfieldIndex; j--)
33+
{
34+
paragraph.ChildEntities.RemoveAt(j);
35+
}
36+
37+
//Create a new table
38+
IWTable table = new WTable(document);
39+
table.ResetCells(3, 2);
40+
table.Rows[0].Cells[0].AddParagraph().AppendText("Sno");
41+
table.Rows[0].Cells[1].AddParagraph().AppendText("Product");
42+
table.Rows[0].IsHeader = true;
43+
table.Rows[1].Cells[0].AddParagraph().AppendText("1.");
44+
table.Rows[1].Cells[1].AddParagraph().AppendText("Essential DocIO");
45+
table.Rows[2].Cells[0].AddParagraph().AppendText("2.");
46+
table.Rows[2].Cells[1].AddParagraph().AppendText("Essential Pdf");
47+
48+
// Clone the generated table.
49+
IWTable table1 = table.Clone() as IWTable;
50+
// Insert the cloned table after the paragraph containing the SEQ field.
51+
paragraph.OwnerTextBody.ChildEntities.Insert(paraindex + 1, table1);
52+
// Insert the modified cloned paragraph after the inserted table.
53+
paragraph.OwnerTextBody.ChildEntities.Insert(paraindex + 2, clonedParagraph);
54+
// Save the modified document to a new file.
55+
using (FileStream docStream1 = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.Write))
56+
{
57+
document.Save(docStream1, FormatType.Docx);
58+
}
59+
}
60+
}
61+
}
62+
}
63+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\InputDocument.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\.gitkeep">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
24+
</Project>

0 commit comments

Comments
 (0)