Skip to content

Commit

Permalink
Merge pull request #285 from Suriya-Balamurugan/ES-899193-Replace-lis…
Browse files Browse the repository at this point in the history
…t-text-with-paragraphs

Added sample to replace list paragraph text with multiple paragraphs from another Word document
  • Loading branch information
MohanaselvamJothi authored Oct 30, 2024
2 parents 2f96f85 + 3f09e69 commit 3240559
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31911.196
MinimumVisualStudioVersion = 10.0.40219.1
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EF357FC6-E9E5-4E3C-B932-43F727BE1DE4}
EndGlobalSection
EndGlobal
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using System.IO;

namespace Replace_list_text_with_paragraphs
{
class Program
{
static void Main(string[] args)
{
using (FileStream destDocStream = new FileStream(Path.GetFullPath(@"Data/DestinationDocument.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
//Open the destination Word document.
using (WordDocument destDocument = new WordDocument(destDocStream, FormatType.Docx))
{
using (FileStream sourceDocStream = new FileStream(Path.GetFullPath(@"Data/SourceDocument.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
//Open the source Word document.
using (WordDocument sourceDocument = new WordDocument(sourceDocStream, FormatType.Docx))
{
//Find the text "[finder]" in the destination document.
TextSelection textSelection = destDocument.Find("[finder]", false, false);
if (textSelection != null)
{
//Get the paragraph containing the found text in destintaion document.
WParagraph destOwnerParagraph = textSelection.GetAsOneRange().OwnerParagraph;
//Get the owner section.
WSection destOwnerSection = destOwnerParagraph.OwnerTextBody.Owner as WSection;
//Get the owner paragraph index.
int destOwnerParaIndex = destOwnerSection.Paragraphs.IndexOf(destOwnerParagraph);
//Retrieve the first section of the source document.
WSection section = sourceDocument.Sections[0];
//Iterate through the each paragraph of the source document.
for (int i = 0; i < section.Paragraphs.Count; i++)
{
WParagraph sourcePara = section.Paragraphs[i];
//Replace the found text with the first paragraph text from the source document.
if (i == 0)
destOwnerParagraph.Replace(textSelection.SelectedText, sourcePara.Text, false, false);
//For remaining paragraphs in the source document.
else
{
//Clone the found destination paragraph.
WParagraph paraToInsert = (WParagraph)destOwnerParagraph.Clone();
//Change the paragraph text to the source paragraph text.
paraToInsert.Text = sourcePara.Text;
//Insert the cloned paragraph as the next item of the found destination paragraph.
destOwnerSection.Body.ChildEntities.Insert(destOwnerParaIndex, paraToInsert);
}
//Increase the paragraph index to insert the next cloned paragraph in the correct position.
destOwnerParaIndex++;
}
}
//Save the destination Word document.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
{
//Saves the Word document to file stream.
destDocument.Save(outputFileStream, FormatType.Docx);
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Replace_list_text_with_paragraphs</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Data\DestinationDocument.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\SourceDocument.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>

0 comments on commit 3240559

Please sign in to comment.