Skip to content

Commit 5461332

Browse files
Added sample
1 parent 122d5fe commit 5461332

6 files changed

Lines changed: 165 additions & 0 deletions

File tree

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.14.37216.2 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Find_a_Checkbox_in_a_Word_Document", "Find_a_Checkbox_in_a_Word_Document\Find_a_Checkbox_in_a_Word_Document.csproj", "{E634BF44-BF95-2A97-0C1A-21DA88BC1470}"
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+
{E634BF44-BF95-2A97-0C1A-21DA88BC1470}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{E634BF44-BF95-2A97-0C1A-21DA88BC1470}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{E634BF44-BF95-2A97-0C1A-21DA88BC1470}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{E634BF44-BF95-2A97-0C1A-21DA88BC1470}.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 = {C2B1D189-08D9-44F0-B768-9510ED36BD36}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
<ItemGroup>
15+
<None Update="Data\Template.docx">
16+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
17+
</None>
18+
<None Update="Output\.gitkeep">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</None>
21+
</ItemGroup>
22+
23+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
namespace Extract_Selected_CheckBox
5+
{
6+
class Program
7+
{
8+
9+
static void Main(string[] args)
10+
{
11+
// Load the word document
12+
using (FileStream fileStream = new FileStream("../../../Data/Template.docx", FileMode.Open, FileAccess.Read))
13+
{
14+
using (WordDocument document = new WordDocument(fileStream, FormatType.Docx))
15+
{
16+
foreach (WSection section in document.Sections)
17+
{
18+
//Accesses the Body of section where all the contents in document are apart
19+
WTextBody sectionBody = section.Body;
20+
IterateTextBody(sectionBody);
21+
WHeadersFooters headersFooters = section.HeadersFooters;
22+
//Consider that OddHeader and OddFooter are applied to this document
23+
//Iterates through the TextBody of OddHeader and OddFooter
24+
IterateTextBody(headersFooters.OddHeader);
25+
IterateTextBody(headersFooters.OddFooter);
26+
}
27+
using (FileStream outputStream = new FileStream("../../../Output/Result.docx", FileMode.Create, FileAccess.Write))
28+
{
29+
//Saves the stream as Word file
30+
document.Save(outputStream, FormatType.Docx);
31+
}
32+
}
33+
}
34+
}
35+
public static void IterateTextBody(WTextBody textBody)
36+
{
37+
//Iterates through each of the child items of WTextBody
38+
for (int i = 0; i < textBody.ChildEntities.Count; i++)
39+
{
40+
//IEntity is the basic unit in DocIO DOM.
41+
//Accesses the body items (should be either paragraph, table or block content control) as IEntity
42+
IEntity bodyItemEntity = textBody.ChildEntities[i];
43+
//A Text body has 3 types of elements - Paragraph, Table and Block Content Control
44+
//Decides the element type by using EntityType
45+
switch (bodyItemEntity.EntityType)
46+
{
47+
case EntityType.Paragraph:
48+
WParagraph paragraph = bodyItemEntity as WParagraph;
49+
//Processes the paragraph contents
50+
//Iterates through the paragraph's DOM
51+
IterateParagraph(paragraph.Items);
52+
break;
53+
case EntityType.Table:
54+
//Table is a collection of rows and cells
55+
//Iterates through table's DOM
56+
IterateTable(bodyItemEntity as WTable);
57+
break;
58+
case EntityType.BlockContentControl:
59+
BlockContentControl blockContentControl = bodyItemEntity as BlockContentControl;
60+
if (blockContentControl.ContentControlProperties.Type == ContentControlType.CheckBox)
61+
blockContentControl.ContentControlProperties.IsChecked = false;
62+
//Iterates to the body items of Block Content Control.
63+
IterateTextBody(blockContentControl.TextBody);
64+
break;
65+
}
66+
}
67+
}
68+
public static void IterateTable(WTable table)
69+
{
70+
//Iterates the row collection in a table
71+
foreach (WTableRow row in table.Rows)
72+
{
73+
//Iterates the cell collection in a table row
74+
foreach (WTableCell cell in row.Cells)
75+
{
76+
//Table cell is derived from (also a) TextBody
77+
//Reusing the code meant for iterating TextBody
78+
IterateTextBody(cell);
79+
}
80+
}
81+
}
82+
public static void IterateParagraph(ParagraphItemCollection paraItems)
83+
{
84+
for (int i = 0; i < paraItems.Count; i++)
85+
{
86+
Entity entity = paraItems[i];
87+
//A paragraph can have child elements such as text, image, hyperlink, symbols, etc.,
88+
//Decides the element type by using EntityType
89+
switch (entity.EntityType)
90+
{
91+
case EntityType.CheckBox:
92+
if (entity is WCheckBox)
93+
(entity as WCheckBox).Checked = false;
94+
break;
95+
case EntityType.TextBox:
96+
//Iterates to the body items of textbox.
97+
WTextBox textBox = entity as WTextBox;
98+
IterateTextBody(textBox.TextBoxBody);
99+
break;
100+
case EntityType.Shape:
101+
//Iterates to the body items of shape.
102+
Shape shape = entity as Shape;
103+
IterateTextBody(shape.TextBody);
104+
break;
105+
case EntityType.InlineContentControl:
106+
//Iterates to the paragraph items of inline content control.
107+
InlineContentControl inlineContentControl = entity as InlineContentControl;
108+
if (inlineContentControl.ContentControlProperties.Type == ContentControlType.CheckBox)
109+
inlineContentControl.ContentControlProperties.IsChecked = false;
110+
IterateParagraph(inlineContentControl.ParagraphItems);
111+
break;
112+
}
113+
}
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)