-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #285 from Suriya-Balamurugan/ES-899193-Replace-lis…
…t-text-with-paragraphs Added sample to replace list paragraph text with multiple paragraphs from another Word document
- Loading branch information
Showing
6 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
...-and-Replace/Replace-list-text-with-paragraphs/.NET/Replace-list-text-with-paragraphs.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 added
BIN
+16.4 KB
...text-with-paragraphs/.NET/Replace-list-text-with-paragraphs/Data/DestinationDocument.docx
Binary file not shown.
Binary file added
BIN
+15.9 KB
...list-text-with-paragraphs/.NET/Replace-list-text-with-paragraphs/Data/SourceDocument.docx
Binary file not shown.
1 change: 1 addition & 0 deletions
1
.../Replace-list-text-with-paragraphs/.NET/Replace-list-text-with-paragraphs/Output/.gitkeep
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
66 changes: 66 additions & 0 deletions
66
...place/Replace-list-text-with-paragraphs/.NET/Replace-list-text-with-paragraphs/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...aragraphs/.NET/Replace-list-text-with-paragraphs/Replace-list-text-with-paragraphs.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |