Skip to content

Commit 343bef1

Browse files
committed
Added Merge-documents-with-headers-footers sample
1 parent 405a8eb commit 343bef1

File tree

7 files changed

+111
-0
lines changed

7 files changed

+111
-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}") = "Merge-documents-with-headers-footers", "Merge-documents-with-headers-footers\Merge-documents-with-headers-footers.csproj", "{D3AF529E-DB54-4294-A876-DD42E1E472D0}"
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+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{D3AF529E-DB54-4294-A876-DD42E1E472D0}.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 = {58137FF9-5AE1-4514-9929-3A8A7DA1DFEB}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Merge_documents_with_headers_footers</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\sourceDocuments\SourceDocument1.docx">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
<None Update="Data\sourceDocuments\SourceDocument2.docx">
21+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
22+
</None>
23+
<None Update="Output\.gitkeep">
24+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
25+
</None>
26+
</ItemGroup>
27+
28+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System.Collections;
4+
using System.Collections.Generic;
5+
using System.Dynamic;
6+
using System.IO;
7+
8+
namespace Merge_documents_with_headers_footers
9+
{
10+
class Program
11+
{
12+
static void Main(string[] args)
13+
{
14+
using (FileStream destStream = new FileStream(@"Data/DestinationDocument.docx", FileMode.Open, FileAccess.Read))
15+
{
16+
using (WordDocument destinationDocument = new WordDocument(destStream, FormatType.Docx))
17+
{
18+
//Get source document from the specified directory
19+
string[] sourceData = Directory.GetFiles(@"Data/sourceDocuments");
20+
foreach (string inputFile in sourceData)
21+
{
22+
using (FileStream sourceStream = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
23+
{
24+
using (WordDocument sourceDocument = new WordDocument(sourceStream, FormatType.Docx))
25+
{
26+
foreach (WSection section in sourceDocument.Sections)
27+
{
28+
WSection currentSection = section;
29+
// If the source document doesn't have a header or footer, it will retain the destination's header or footer when merged.
30+
// To prevent this, add an empty paragraph to the source's header or footer
31+
if (section.HeadersFooters.FirstPageHeader.Count == 0)
32+
section.HeadersFooters.FirstPageHeader.AddParagraph();
33+
if (section.HeadersFooters.FirstPageFooter.Count == 0)
34+
section.HeadersFooters.FirstPageFooter.AddParagraph();
35+
if (section.HeadersFooters.OddHeader.Count == 0)
36+
section.HeadersFooters.OddHeader.AddParagraph();
37+
if (section.HeadersFooters.OddFooter.Count == 0)
38+
section.HeadersFooters.OddFooter.AddParagraph();
39+
if (section.HeadersFooters.EvenHeader.Count == 0)
40+
section.HeadersFooters.EvenHeader.AddParagraph();
41+
if (section.HeadersFooters.EvenFooter.Count == 0)
42+
section.HeadersFooters.EvenFooter.AddParagraph();
43+
}
44+
// Import the entire content of the source document into the destination document.
45+
destinationDocument.ImportContent(sourceDocument);
46+
}
47+
}
48+
}
49+
using (FileStream outputStream = new FileStream(@"Output/CombinedDocument.docx", FileMode.Create, FileAccess.ReadWrite))
50+
{
51+
destinationDocument.Save(outputStream, FormatType.Docx);
52+
}
53+
}
54+
}
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)