Skip to content

Commit 0eb42c4

Browse files
ES-942067-Extract text from Plain text content control
1 parent 3d293e6 commit 0eb42c4

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-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 15
4+
VisualStudioVersion = 15.0.28307.136
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Extract-text-in-plain-content-control", "Extract-text-in-plain-content-control\Extract-text-in-plain-content-control.csproj", "{AE9996D9-17F0-4A69-AB1D-CA10E8605CF7}"
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+
{AE9996D9-17F0-4A69-AB1D-CA10E8605CF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{AE9996D9-17F0-4A69-AB1D-CA10E8605CF7}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{AE9996D9-17F0-4A69-AB1D-CA10E8605CF7}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{AE9996D9-17F0-4A69-AB1D-CA10E8605CF7}.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 = {2D02C5F8-31E9-4F9E-96E9-D370D58B7826}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Extract_text_in_plain_content_control</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="Data\Template.docx">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
</ItemGroup>
18+
19+
</Project>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using Syncfusion.DocIO;
5+
using Syncfusion.DocIO.DLS;
6+
7+
namespace Extract_text_in_plain_content_control
8+
{
9+
class Program
10+
{
11+
static List<string> extractedTextCollection = new List<string>();
12+
static void Main(string[] args)
13+
{
14+
//Open the file as Stream
15+
using (FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read))
16+
{
17+
//Creates an instance of WordDocument class
18+
using (WordDocument document = new WordDocument(docStream, FormatType.Automatic))
19+
{
20+
//Find all plain text content control by EntityType in Word document.
21+
List<Entity> plainTextContentControls = document.FindAllItemsByProperty(EntityType.InlineContentControl, "ContentControlProperties.Type", "Text");
22+
// Extract text from all plain text content controls.
23+
for (int i = 0; i < plainTextContentControls.Count; i++)
24+
{
25+
InlineContentControl plainTextContentControl = plainTextContentControls[i] as InlineContentControl;
26+
ExtractTextInPlainTextContentControl(plainTextContentControl);
27+
}
28+
// Print extracted text to the console
29+
Console.WriteLine("Extracted Text from Plain Text Content Controls:");
30+
foreach (string extractedText in extractedTextCollection)
31+
Console.WriteLine(extractedText);
32+
Console.ReadLine();
33+
}
34+
}
35+
}
36+
37+
/// <summary>
38+
/// Extract Text in Plain text content control
39+
/// </summary>
40+
private static void ExtractTextInPlainTextContentControl(InlineContentControl plainTextContentControl)
41+
{
42+
foreach (ParagraphItem item in plainTextContentControl.ParagraphItems)
43+
{
44+
if (item is WTextRange)
45+
{
46+
WTextRange textRange = (item as WTextRange);
47+
string text = textRange.Text;
48+
extractedTextCollection.Add(text);
49+
}
50+
}
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)