Skip to content

Commit 834911f

Browse files
Added sample
1 parent 9d7a76d commit 834911f

5 files changed

Lines changed: 112 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Solution>
2+
<Project Path="Replace-DocVariable-to-table/Replace-DocVariable-to-table.csproj" />
3+
</Solution>
Binary file not shown.

‎Fields/Replace-DocVariable-to-table/.NET/Replace-DocVariable-to-table/Output/.gitkeep‎

Whitespace-only changes.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
namespace Replace_DocVariable_to_table
4+
{
5+
class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
//Opens an existing document
10+
using (WordDocument document = new WordDocument(Path.GetFullPath(@"../../../Data/Template.docx"), FormatType.Docx))
11+
{
12+
WTable table = new WTable(document);
13+
table.ResetCells(1, 3);
14+
//Header FIRST ROW
15+
table[0, 0].Width = 13f;
16+
table[0, 1].Width = 128f;
17+
table[0, 1].AddParagraph().AppendText("Syncfusion");
18+
table[0, 2].Width = 80f;
19+
table[0, 2].AddParagraph().AppendText("DocIO");
20+
21+
string docVariableToBereplaced = "InsertTable";
22+
//Finds the DocVariable field and replace it with a Table
23+
ReplaceDocVariableWithTable(document, docVariableToBereplaced, table);
24+
//Saves and closes the document instance
25+
document.Save("../../../Output/output.docx", FormatType.Docx);
26+
}
27+
}
28+
/// <summary>
29+
/// Finds the DocVariable field and replace it with a Table
30+
/// </summary>
31+
/// <param name="document">Word document in which you should replace the field with</param>
32+
/// <param name="docVariableToBeReplaced">The name of the Doc variable that you should replace</param>
33+
/// <param name="table">The table which will replace the Doc Variable</param>
34+
private static void ReplaceDocVariableWithTable(WordDocument document, string docVariableToBeReplaced, WTable table)
35+
{
36+
List<Entity> docFields = document.FindAllItemsByProperty(EntityType.Field, "FieldType", FieldType.FieldDocVariable.ToString());
37+
if(docFields != null)
38+
{
39+
foreach (Entity ent in docFields)
40+
{
41+
WField docField = ent as WField;
42+
if (docField.FieldValue == docVariableToBeReplaced)
43+
{
44+
WParagraph ownerPara = docField.OwnerParagraph;
45+
int fieldIndex = ownerPara.ChildEntities.IndexOf(docField);
46+
int textBodyIndex = ownerPara.OwnerTextBody.ChildEntities.IndexOf(ownerPara);
47+
ownerPara.ChildEntities.Remove(docField);
48+
//Split the paragraph and insert the table in the between
49+
SplitAndInsertTable(ownerPara, table, fieldIndex, textBodyIndex);
50+
}
51+
}
52+
}
53+
}
54+
/// <summary>
55+
/// Split the paragraph and insert the table in the between
56+
/// </summary>
57+
/// <param name="paragraph">Owner paragraph of the field</param>
58+
/// <param name="table">The table which will replace the Doc Variable</param>
59+
/// <param name="index">Index of the paragraph item</param>
60+
/// <param name="textBodyIndex">Index of the text body</param>
61+
private static void SplitAndInsertTable(WParagraph paragraph, WTable table, int index, int textBodyIndex)
62+
{
63+
WTextBody ownerTextBody = paragraph.OwnerTextBody;
64+
//Clone the paragraph to split the paragraph into two
65+
WParagraph clonedParagraph = (WParagraph)paragraph.Clone();
66+
//Removes the cloned para items before the field to split the para
67+
for (int i = 0; i < index; i++)
68+
{
69+
clonedParagraph.ChildEntities.Remove(clonedParagraph.Items[i]);
70+
}
71+
//Removes the para items after the field
72+
for (int i = index; i < paragraph.Items.Count; i++)
73+
{
74+
paragraph.ChildEntities.Remove(paragraph.Items[i]);
75+
}
76+
if (paragraph.ChildEntities.Count == 0)
77+
{
78+
paragraph.OwnerTextBody.ChildEntities.Remove(paragraph);
79+
}
80+
int indexOftheItemToBeInserted = textBodyIndex + 1, indexOftheSplittedPara = textBodyIndex + 2;
81+
//Insert the table to be inserted in the position of Doc Variable field
82+
if (paragraph.ChildEntities.Count == 0)
83+
{
84+
ownerTextBody.ChildEntities.Insert(textBodyIndex, table);
85+
indexOftheSplittedPara = indexOftheSplittedPara - 1;
86+
}
87+
else
88+
ownerTextBody.ChildEntities.Insert(indexOftheItemToBeInserted, table);
89+
//If there is a splitted paragraph insert it after the inserted table
90+
if (!(clonedParagraph.ChildEntities.Count == 0))
91+
ownerTextBody.ChildEntities.Insert(indexOftheSplittedPara, clonedParagraph);
92+
}
93+
}
94+
}
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_DocVariable_to_table</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
</Project>

0 commit comments

Comments
 (0)