Skip to content

Commit 94d409f

Browse files
Merge pull request #268 from VijayadharshiniMathiyalagan/ES-893365-How-to-copy-style-between-Word-documents
Add sample for copy style between Word documents
2 parents cc073d6 + a12b009 commit 94d409f

File tree

6 files changed

+83
-0
lines changed

6 files changed

+83
-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 17
4+
VisualStudioVersion = 17.12.35309.182
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Copy-style-between-documents", "Copy-style-between-documents\Copy-style-between-documents.csproj", "{967C6B91-304A-427A-9D6D-F60D8976FA2D}"
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+
{967C6B91-304A-427A-9D6D-F60D8976FA2D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{967C6B91-304A-427A-9D6D-F60D8976FA2D}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{967C6B91-304A-427A-9D6D-F60D8976FA2D}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{967C6B91-304A-427A-9D6D-F60D8976FA2D}.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 = {2C5AE214-1314-4F36-9675-CFF7B505AC22}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Copy_style_between_documents</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\DestinationDocument.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Data\SourceDocument.docx">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
<None Update="Output\.gitkeep">
23+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
24+
</None>
25+
</ItemGroup>
26+
27+
</Project>
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Syncfusion.DocIO.DLS;
2+
using Syncfusion.DocIO;
3+
4+
using (FileStream destinationStream = new FileStream(Path.GetFullPath(@"Data/DestinationDocument.docx"), FileMode.Open, FileAccess.ReadWrite))
5+
{
6+
//Open the destination document.
7+
using (WordDocument destinationDocument = new WordDocument(destinationStream, FormatType.Docx))
8+
{
9+
using (FileStream sourceStream = new FileStream(Path.GetFullPath(@"Data/SourceDocument.docx"), FileMode.Open, FileAccess.ReadWrite))
10+
{
11+
//Open the source document.
12+
using (WordDocument sourceDocument = new WordDocument(sourceStream, FormatType.Docx))
13+
{
14+
// Retrieve the specific style from the source document.
15+
WParagraphStyle paraStyle = sourceDocument.Styles.FindByName("MyStyle") as WParagraphStyle;
16+
// Add the retrieved style to the destination document.
17+
destinationDocument.Styles.Add(paraStyle.Clone());
18+
// Get the first paragraph in the destination document.
19+
WParagraph paragraph = destinationDocument.Sections[0].Body.ChildEntities[0] as WParagraph;
20+
// Apply the retrieved style to the paragraph in the destination document.
21+
paragraph.ApplyStyle("MyStyle");
22+
// Save the destination document.
23+
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
24+
{
25+
destinationDocument.Save(outputStream, FormatType.Docx);
26+
}
27+
}
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)