Skip to content

Commit 04ccc8c

Browse files
Added sample for Merge docs
1 parent 8a812fc commit 04ccc8c

9 files changed

Lines changed: 11046 additions & 0 deletions

File tree

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.14.37531.7 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Merge_Multiple_Documents", "Merge_Multiple_Documents\Merge_Multiple_Documents.csproj", "{9645325D-8921-7EE8-6C1A-C4A72D88D5FD}"
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+
{9645325D-8921-7EE8-6C1A-C4A72D88D5FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{9645325D-8921-7EE8-6C1A-C4A72D88D5FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{9645325D-8921-7EE8-6C1A-C4A72D88D5FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{9645325D-8921-7EE8-6C1A-C4A72D88D5FD}.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 = {67365E95-6921-4E8C-B27B-5F9EB2C2AA93}
24+
EndGlobalSection
25+
EndGlobal

Word-document/Merge_Multiple_Documents/.NET/Merge_Multiple_Documents/Data/MainDocument.rtf

Lines changed: 10210 additions & 0 deletions
Large diffs are not rendered by default.

Word-document/Merge_Multiple_Documents/.NET/Merge_Multiple_Documents/Data/Template.rtf

Lines changed: 231 additions & 0 deletions
Large diffs are not rendered by default.

Word-document/Merge_Multiple_Documents/.NET/Merge_Multiple_Documents/Data/footer.rtf

Lines changed: 233 additions & 0 deletions
Large diffs are not rendered by default.

Word-document/Merge_Multiple_Documents/.NET/Merge_Multiple_Documents/Data/header.rtf

Lines changed: 232 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
<ItemGroup>
15+
<None Update="Data\Template.rtf">
16+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
17+
</None>
18+
<None Update="Data\footer.rtf">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</None>
21+
<None Update="Data\header.rtf">
22+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
23+
</None>
24+
<None Update="Data\MainDocument.rtf">
25+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
26+
</None>
27+
<None Update="Output\.gitkeep">
28+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
29+
</None>
30+
</ItemGroup>
31+
32+
</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: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
namespace Merge_Multiple_Documents
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
using (FileStream sourceStream = new FileStream(Path.GetFullPath("../../../Data/MainDocument.rtf"), FileMode.Open))
11+
using (FileStream destinationStream = new FileStream(Path.GetFullPath("../../../Data/Template.rtf"), FileMode.Open))
12+
using (WordDocument sourceDocument = new WordDocument(sourceStream, FormatType.Automatic))
13+
using (WordDocument destinationDocument = new WordDocument(destinationStream, FormatType.Rtf))
14+
{
15+
//Iterate through each section in the source document
16+
foreach (WSection section in sourceDocument.Sections)
17+
{
18+
destinationDocument.Sections.Add(section.Clone());
19+
}
20+
21+
// Copy header from other document
22+
//If document already have an header clear the header part before copying
23+
using (FileStream headerStream = new FileStream(Path.GetFullPath("../../../Data/header.rtf"), FileMode.Open))
24+
{
25+
WordDocument headerDoc = new WordDocument(headerStream, FormatType.Rtf);
26+
MoveHeader(headerDoc, destinationDocument);
27+
headerDoc.Close();
28+
}
29+
// Copy footer from other document
30+
using (FileStream footerStream = new FileStream(Path.GetFullPath("../../../Data/footer.rtf"), FileMode.Open))
31+
{
32+
WordDocument footerDoc = new WordDocument(footerStream, FormatType.Rtf);
33+
MoveFooter(footerDoc, destinationDocument);
34+
footerDoc.Close();
35+
}
36+
37+
//Saves the Word document to FileStream.
38+
using (FileStream outputStream = new FileStream(Path.GetFullPath("../../../Output/Result.docx"), FileMode.Create, FileAccess.Write))
39+
{
40+
destinationDocument.Save(outputStream, FormatType.Docx);
41+
}
42+
}
43+
}
44+
// Method to move header content from the template document to the main document
45+
static void MoveHeader(WordDocument templateDocument, WordDocument mainDocument)
46+
{
47+
//Entity Collection
48+
EntityCollection header = templateDocument.Sections[0].Body.ChildEntities;
49+
//Move all the items from one collection to another collection
50+
for (int i = 0; i < mainDocument.Sections.Count; i++)
51+
{
52+
WSection mainDocSection = mainDocument.Sections[i] as WSection;
53+
// Move the header items from the template to the main document's Header
54+
MoveItems(mainDocSection.HeadersFooters.OddHeader.ChildEntities, header);
55+
MoveItems(mainDocSection.HeadersFooters.EvenHeader.ChildEntities, header);
56+
MoveItems(mainDocSection.HeadersFooters.FirstPageHeader.ChildEntities, header);
57+
}
58+
}
59+
// Method to move footer content from the template document to the main document
60+
static void MoveFooter(WordDocument templateDocument, WordDocument mainDocument)
61+
{
62+
EntityCollection footer = templateDocument.Sections[0].Body.ChildEntities;
63+
//Move all the items from one collection to another collection
64+
for (int i = 0; i < mainDocument.Sections.Count; i++)
65+
{
66+
WSection mainDocSection = mainDocument.Sections[i] as WSection;
67+
// Move the footer items from the template to the main document's Footer
68+
MoveItems(mainDocSection.HeadersFooters.OddFooter.ChildEntities, footer);
69+
MoveItems(mainDocSection.HeadersFooters.EvenFooter.ChildEntities, footer);
70+
MoveItems(mainDocSection.HeadersFooters.FirstPageFooter.ChildEntities, footer);
71+
}
72+
}
73+
// Move the footer items from the template to the main document's header footer
74+
static void MoveItems(EntityCollection destinationDoc, EntityCollection sourceDoc)
75+
{
76+
for (int i = 0; i < sourceDoc.Count; i++)
77+
{
78+
destinationDoc.Add(sourceDoc[i].Clone());
79+
}
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)