Skip to content

Commit 6bd021e

Browse files
Merge pull request #296 from VijayadharshiniMathiyalagan/ES-893350-How-to-get-text-with-newline-characters-from-TextFormField-in-Word-document
Add Sample for get text with new line characters
2 parents c77fd56 + 85094bc commit 6bd021e

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35417.141 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Get-text-with-newline-characters", "Get-text-with-newline-characters\Get-text-with-newline-characters.csproj", "{813B45AF-210D-41A8-895F-9342D7B135B2}"
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+
{813B45AF-210D-41A8-895F-9342D7B135B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{813B45AF-210D-41A8-895F-9342D7B135B2}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{813B45AF-210D-41A8-895F-9342D7B135B2}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{813B45AF-210D-41A8-895F-9342D7B135B2}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Binary file not shown.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Get_text_with_newline_characters</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\Template.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
</ItemGroup>
20+
21+
</Project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
using (FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite))
5+
{
6+
// Opens the template Word document.
7+
using (WordDocument document = new WordDocument(inputStream, FormatType.Docx))
8+
{
9+
// Find WTextFormField by its name.
10+
WTextFormField textFormField = document.FindItemByProperty(EntityType.TextFormField, "Name", "Text1") as WTextFormField;
11+
if (textFormField != null)
12+
{
13+
// Get the bookmark name from the textFormField next sibiling.
14+
string bookmarkName = textFormField.Name;
15+
// Creates the bookmark navigator instance to access the bookmark.
16+
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
17+
// Moves the virtual cursor to the location before the end of the bookmark.
18+
bookmarkNavigator.MoveToBookmark(bookmarkName);
19+
// Gets the bookmark content as WordDocumentPart.
20+
WordDocumentPart wordDocumentPart = bookmarkNavigator.GetContent();
21+
// Saves the WordDocumentPart as separate Word document.
22+
using (WordDocument newDocument = wordDocumentPart.GetAsWordDocument())
23+
{
24+
// Close the WordDocumentPart instance.
25+
wordDocumentPart.Close();
26+
// Get the text with new line characters.
27+
string textFieldText = newDocument.GetText();
28+
// Replace FORMTEXT word from the extracted text.
29+
textFieldText = textFieldText.Replace(" FORMTEXT ", "");
30+
// Write the output in console window.
31+
Console.WriteLine(textFieldText);
32+
// Press any key in console window to continue.
33+
Console.ReadKey();
34+
}
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)