Skip to content

Commit df1fcb1

Browse files
Merge pull request #376 from SyncfusionExamples/ES-935024-Copy_HeaderFooter_from_one_doc_to_another_doc
ES-935024-Add the sample Copy_HeaderFooter_from_one_doc_to_another_doc
2 parents b79db59 + 097975f commit df1fcb1

File tree

6 files changed

+125
-0
lines changed

6 files changed

+125
-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}") = "Copy-HeaderFooter-from-one-doc-to-another-doc", "Copy-HeaderFooter-from-one-doc-to-another-doc\Copy-HeaderFooter-from-one-doc-to-another-doc.csproj", "{C17B90BC-F559-456B-B189-90B53FF6CDD4}"
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+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.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 = {EF357FC6-E9E5-4E3C-B932-43F727BE1DE4}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Copy_HeaderFooter_from_one_doc_to_another_doc</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="Data\MainDocument.docx">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
<None Update="Data\Template.docx">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
<None Update="Output\.gitkeep">
21+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
22+
</None>
23+
</ItemGroup>
24+
25+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System.IO;
4+
5+
namespace Copy_HeaderFooter_from_one_doc_to_another_doc
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
// Open the template document stream for reading.
12+
using (FileStream templateStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
13+
{
14+
// Open the main document stream for reading.
15+
using (FileStream maindocumentStreamPath = new FileStream(Path.GetFullPath(@"Data/MainDocument.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
16+
{
17+
// Load the main Word document.
18+
using (WordDocument mainDocument = new WordDocument(maindocumentStreamPath, FormatType.Docx))
19+
{
20+
// Load the template Word document.
21+
using (WordDocument templateDocument = new WordDocument(templateStreamPath, FormatType.Docx))
22+
{
23+
// Copy header and footer from the template document to the main document.
24+
MoveHeaderFooter(templateDocument, mainDocument);
25+
26+
// Create a file stream for saving the updated document.
27+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
28+
{
29+
// Save the modified main document to the output file.
30+
mainDocument.Save(outputFileStream, FormatType.Docx);
31+
}
32+
}
33+
}
34+
}
35+
}
36+
}
37+
/// <summary>
38+
/// Move header and footer from one Word document to another Word document.
39+
/// </summary>
40+
/// <param name="templateDocument"></param>
41+
/// <param name="mainDocument"></param>
42+
public static void MoveHeaderFooter(WordDocument templateDocument, WordDocument mainDocument)
43+
{
44+
//Gets the section in the template word document
45+
WSection templateDocSection = templateDocument.Sections[0] as WSection;
46+
//Move all the items from one collection to another collection
47+
for (int i = 0; i < mainDocument.Sections.Count; i++)
48+
{
49+
WSection mainDocSection = mainDocument.Sections[i] as WSection;
50+
MoveItems(mainDocSection.HeadersFooters.EvenHeader.ChildEntities, templateDocSection.HeadersFooters.Header.ChildEntities);
51+
MoveItems(mainDocSection.HeadersFooters.EvenFooter.ChildEntities, templateDocSection.HeadersFooters.Footer.ChildEntities);
52+
MoveItems(mainDocSection.HeadersFooters.OddHeader.ChildEntities, templateDocSection.HeadersFooters.Header.ChildEntities);
53+
MoveItems(mainDocSection.HeadersFooters.OddFooter.ChildEntities, templateDocSection.HeadersFooters.Footer.ChildEntities);
54+
MoveItems(mainDocSection.HeadersFooters.FirstPageHeader.ChildEntities, templateDocSection.HeadersFooters.Header.ChildEntities);
55+
MoveItems(mainDocSection.HeadersFooters.FirstPageFooter.ChildEntities, templateDocSection.HeadersFooters.Footer.ChildEntities);
56+
57+
//Copy the page setup from template document to main document.
58+
mainDocSection.PageSetup.DifferentOddAndEvenPages = templateDocSection.PageSetup.DifferentOddAndEvenPages;
59+
mainDocSection.PageSetup.DifferentFirstPage = templateDocSection.PageSetup.DifferentFirstPage;
60+
mainDocSection.PageSetup.HeaderDistance = templateDocSection.PageSetup.HeaderDistance;
61+
mainDocSection.PageSetup.FooterDistance = templateDocSection.PageSetup.FooterDistance;
62+
mainDocSection.PageSetup.Margins = templateDocSection.PageSetup.Margins;
63+
}
64+
}
65+
/// <summary>
66+
/// Move all the items from one collection to another collection.
67+
/// </summary>
68+
private static void MoveItems(EntityCollection childEntities1, EntityCollection childEntities2)
69+
{
70+
for (int i = 0; i < childEntities2.Count; i++)
71+
childEntities1.Add(childEntities2[i].Clone());
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)