Skip to content

Commit 3240559

Browse files
Merge pull request #285 from Suriya-Balamurugan/ES-899193-Replace-list-text-with-paragraphs
Added sample to replace list paragraph text with multiple paragraphs from another Word document
2 parents 2f96f85 + 3f09e69 commit 3240559

File tree

6 files changed

+117
-0
lines changed

6 files changed

+117
-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 Version 16
4+
VisualStudioVersion = 16.0.31911.196
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Replace-list-text-with-paragraphs", "Replace-list-text-with-paragraphs\Replace-list-text-with-paragraphs.csproj", "{C17B90BC-F559-456B-B189-90B53FF6CDD4}"
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+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.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 = {EF357FC6-E9E5-4E3C-B932-43F727BE1DE4}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System.IO;
4+
5+
namespace Replace_list_text_with_paragraphs
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
using (FileStream destDocStream = new FileStream(Path.GetFullPath(@"Data/DestinationDocument.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
12+
{
13+
//Open the destination Word document.
14+
using (WordDocument destDocument = new WordDocument(destDocStream, FormatType.Docx))
15+
{
16+
using (FileStream sourceDocStream = new FileStream(Path.GetFullPath(@"Data/SourceDocument.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
17+
{
18+
//Open the source Word document.
19+
using (WordDocument sourceDocument = new WordDocument(sourceDocStream, FormatType.Docx))
20+
{
21+
//Find the text "[finder]" in the destination document.
22+
TextSelection textSelection = destDocument.Find("[finder]", false, false);
23+
if (textSelection != null)
24+
{
25+
//Get the paragraph containing the found text in destintaion document.
26+
WParagraph destOwnerParagraph = textSelection.GetAsOneRange().OwnerParagraph;
27+
//Get the owner section.
28+
WSection destOwnerSection = destOwnerParagraph.OwnerTextBody.Owner as WSection;
29+
//Get the owner paragraph index.
30+
int destOwnerParaIndex = destOwnerSection.Paragraphs.IndexOf(destOwnerParagraph);
31+
//Retrieve the first section of the source document.
32+
WSection section = sourceDocument.Sections[0];
33+
//Iterate through the each paragraph of the source document.
34+
for (int i = 0; i < section.Paragraphs.Count; i++)
35+
{
36+
WParagraph sourcePara = section.Paragraphs[i];
37+
//Replace the found text with the first paragraph text from the source document.
38+
if (i == 0)
39+
destOwnerParagraph.Replace(textSelection.SelectedText, sourcePara.Text, false, false);
40+
//For remaining paragraphs in the source document.
41+
else
42+
{
43+
//Clone the found destination paragraph.
44+
WParagraph paraToInsert = (WParagraph)destOwnerParagraph.Clone();
45+
//Change the paragraph text to the source paragraph text.
46+
paraToInsert.Text = sourcePara.Text;
47+
//Insert the cloned paragraph as the next item of the found destination paragraph.
48+
destOwnerSection.Body.ChildEntities.Insert(destOwnerParaIndex, paraToInsert);
49+
}
50+
//Increase the paragraph index to insert the next cloned paragraph in the correct position.
51+
destOwnerParaIndex++;
52+
}
53+
}
54+
//Save the destination Word document.
55+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
56+
{
57+
//Saves the Word document to file stream.
58+
destDocument.Save(outputFileStream, FormatType.Docx);
59+
}
60+
}
61+
}
62+
}
63+
}
64+
}
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Replace_list_text_with_paragraphs</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="Data\DestinationDocument.docx">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
<None Update="Data\SourceDocument.docx">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
<None Update="Output\.gitkeep">
21+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
22+
</None>
23+
</ItemGroup>
24+
25+
</Project>

0 commit comments

Comments
 (0)