Skip to content

Commit be0a336

Browse files
Added sample
1 parent b88ae2e commit be0a336

6 files changed

Lines changed: 179 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.36930.0 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Find-And-Remove-LongText", "Find-And-Remove-LongText\Find-And-Remove-LongText.csproj", "{E7831400-4839-7597-D279-1825650F1DB7}"
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+
{E7831400-4839-7597-D279-1825650F1DB7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{E7831400-4839-7597-D279-1825650F1DB7}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{E7831400-4839-7597-D279-1825650F1DB7}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{E7831400-4839-7597-D279-1825650F1DB7}.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 = {9A56DF1A-9E81-4559-B4B8-B1018E6490C8}
24+
EndGlobalSection
25+
EndGlobal
Binary file not shown.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Find-And-Remove-LongText</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+
<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+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Binary file not shown.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using Syncfusion.DocIORenderer;
4+
using Syncfusion.Pdf;
5+
using System.IO;
6+
7+
8+
namespace Find_And_Remove_LongText
9+
{
10+
class Program
11+
{
12+
// Set text length to be remove from the document
13+
static int requiredLongTextLength = 300;
14+
15+
static void Main(string[] args)
16+
{
17+
// Open an existing word document
18+
using (FileStream inputFileStream = new FileStream(Path.GetFullPath(@"../../../Data/Input.docx"), FileMode.Open))
19+
{
20+
//Loads an existing Word document.
21+
using (WordDocument wordDocument = new WordDocument(inputFileStream, FormatType.Docx))
22+
{
23+
// Remove long texts
24+
CheckAndRemoveLongText(wordDocument);
25+
//Creates an instance of DocIORenderer.
26+
using (DocIORenderer renderer = new DocIORenderer())
27+
{
28+
//Converts Word document into PDF document.
29+
using (PdfDocument pdfDocument = renderer.ConvertToPDF(wordDocument))
30+
{
31+
//Saves the PDF file to file system.
32+
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"..\..\..\Output\WordToPDF.pdf"), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
33+
{
34+
pdfDocument.Save(outputStream);
35+
}
36+
}
37+
}
38+
}
39+
}
40+
}
41+
private static void CheckAndRemoveLongText(WordDocument document)
42+
{
43+
List<Entity> paragraphs = document.FindAllItemsByProperty(EntityType.Paragraph, null, null);
44+
foreach (Entity paragraph in paragraphs)
45+
{
46+
WParagraph wParagraph = paragraph as WParagraph;
47+
IterateParagraph(wParagraph.Items);
48+
}
49+
}
50+
private static void IterateTextBody(WTextBody textBody)
51+
{
52+
//Iterates through each of the child items of WTextBody
53+
for (int i = 0; i < textBody.ChildEntities.Count; i++)
54+
{
55+
//IEntity is the basic unit in DocIO DOM.
56+
//Accesses the body items (should be either paragraph, table or block content control) as IEntity
57+
IEntity bodyItemEntity = textBody.ChildEntities[i];
58+
//A Text body has 3 types of elements - Paragraph, Table and Block Content Control
59+
//Decides the element type by using EntityType
60+
switch (bodyItemEntity.EntityType)
61+
{
62+
case EntityType.Paragraph:
63+
WParagraph paragraph = bodyItemEntity as WParagraph;
64+
//Processes the paragraph contents
65+
//Iterates through the paragraph's DOM
66+
IterateParagraph(paragraph.Items);
67+
break;
68+
case EntityType.Table:
69+
//Table is a collection of rows and cells
70+
//Iterates through table's DOM
71+
IterateTable(bodyItemEntity as WTable);
72+
break;
73+
case EntityType.BlockContentControl:
74+
BlockContentControl blockContentControl = bodyItemEntity as BlockContentControl;
75+
//Iterates to the body items of Block Content Control.
76+
IterateTextBody(blockContentControl.TextBody);
77+
break;
78+
}
79+
}
80+
}
81+
private static void IterateTable(WTable table)
82+
{
83+
//Iterates the row collection in a table
84+
foreach (WTableRow row in table.Rows)
85+
{
86+
//Iterates the cell collection in a table row
87+
foreach (WTableCell cell in row.Cells)
88+
{
89+
//Table cell is derived from (also a) TextBody
90+
//Reusing the code meant for iterating TextBody
91+
IterateTextBody(cell);
92+
}
93+
}
94+
}
95+
private static void IterateParagraph(ParagraphItemCollection paraItems)
96+
{
97+
for (int i = 0; i < paraItems.Count; i++)
98+
{
99+
Entity entity = paraItems[i];
100+
//A paragraph can have child elements such as text, image, hyperlink, symbols, etc.,
101+
//Decides the element type by using EntityType
102+
switch (entity.EntityType)
103+
{
104+
case EntityType.TextRange:
105+
WTextRange textRange = entity as WTextRange;
106+
//Find and remove the long text in Word document.
107+
if (textRange.Text.Length >= requiredLongTextLength)
108+
{
109+
(entity as WTextRange).Text = string.Empty;
110+
}
111+
break;
112+
case EntityType.TextBox:
113+
//Iterates to the body items of textbox.
114+
WTextBox textBox = entity as WTextBox;
115+
IterateTextBody(textBox.TextBoxBody);
116+
break;
117+
case EntityType.Shape:
118+
//Iterates to the body items of shape.
119+
Shape shape = entity as Shape;
120+
IterateTextBody(shape.TextBody);
121+
break;
122+
case EntityType.InlineContentControl:
123+
//Iterates to the paragraph items of inline content control.
124+
InlineContentControl inlineContentControl = entity as InlineContentControl;
125+
IterateParagraph(inlineContentControl.ParagraphItems);
126+
break;
127+
}
128+
}
129+
}
130+
}
131+
}

0 commit comments

Comments
 (0)