Skip to content

Commit ca42c26

Browse files
Added sample
1 parent 1cfc871 commit ca42c26

7 files changed

Lines changed: 107 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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\SourceDocument.docx">
16+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
17+
</None>
18+
<None Update="Data\DestinationDocument.docx">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</None>
21+
<None Update="Output\.gitkeep">
22+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
23+
</None>
24+
</ItemGroup>
25+
26+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
namespace Import_Headers_and_Footers_from_Another_WordDocument
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
using (FileStream sourceStreamPath = new FileStream(Path.GetFullPath("Data/SourceDocument.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
11+
{
12+
using (FileStream destinationStreamPath = new FileStream(Path.GetFullPath("Data/DestinationDocument.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
13+
{
14+
//Opens an source document from file system through constructor of WordDocument class
15+
using (WordDocument document = new WordDocument(sourceStreamPath, FormatType.Automatic))
16+
{
17+
//Opens the destination document
18+
using (WordDocument destinationDocument = new WordDocument(destinationStreamPath, FormatType.Docx))
19+
{
20+
21+
WSection section = document.Sections[0] as WSection;
22+
//Move all the items from one collection to another collection
23+
for (int i = 0; i < destinationDocument.Sections.Count; i++)
24+
{
25+
MoveItems(destinationDocument.Sections[i].HeadersFooters.EvenHeader.ChildEntities, section.HeadersFooters.Header.ChildEntities);
26+
MoveItems(destinationDocument.Sections[i].HeadersFooters.EvenFooter.ChildEntities, section.HeadersFooters.Footer.ChildEntities);
27+
MoveItems(destinationDocument.Sections[i].HeadersFooters.OddHeader.ChildEntities, section.HeadersFooters.Header.ChildEntities);
28+
MoveItems(destinationDocument.Sections[i].HeadersFooters.OddFooter.ChildEntities, section.HeadersFooters.Footer.ChildEntities);
29+
MoveItems(destinationDocument.Sections[i].HeadersFooters.FirstPageHeader.ChildEntities, section.HeadersFooters.Header.ChildEntities);
30+
MoveItems(destinationDocument.Sections[i].HeadersFooters.FirstPageFooter.ChildEntities, section.HeadersFooters.Footer.ChildEntities);
31+
destinationDocument.Sections[i].PageSetup.DifferentOddAndEvenPages = section.PageSetup.DifferentOddAndEvenPages;
32+
destinationDocument.Sections[i].PageSetup.DifferentFirstPage = section.PageSetup.DifferentFirstPage;
33+
}
34+
using (FileStream outputStream = new FileStream(Path.GetFullPath("Output/Result.docx"), FileMode.Create))
35+
{
36+
destinationDocument.Save(outputStream, FormatType.Docx);
37+
}
38+
}
39+
}
40+
}
41+
}
42+
}
43+
/// <summary>
44+
/// Move all the items from one collection to another collection.
45+
/// </summary>
46+
/// <param name="childEntities1"></param>
47+
/// <param name="childEntities2"></param>
48+
private static void MoveItems(EntityCollection childEntities1, EntityCollection childEntities2)
49+
{
50+
for (int i = 0; i < childEntities2.Count; i++)
51+
childEntities1.Add(childEntities2[i].Clone());
52+
}
53+
}
54+
}
55+
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.37216.2 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Import_Headers_and_Footers_from_Another_WordDocument", "Import-Headers-and-Footers-from-Another-WordDocument\Import_Headers_and_Footers_from_Another_WordDocument.csproj", "{578DCB93-2F8F-DBAB-C1CE-AB9532F653E6}"
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+
{578DCB93-2F8F-DBAB-C1CE-AB9532F653E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{578DCB93-2F8F-DBAB-C1CE-AB9532F653E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{578DCB93-2F8F-DBAB-C1CE-AB9532F653E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{578DCB93-2F8F-DBAB-C1CE-AB9532F653E6}.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 = {62F21DF8-8D09-4911-83B2-3A4008C795A3}
24+
EndGlobalSection
25+
EndGlobal

0 commit comments

Comments
 (0)