Skip to content

Commit eb0729b

Browse files
Added sample
1 parent 327a930 commit eb0729b

9 files changed

Lines changed: 11063 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.37301.10 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Merge_Documents_by_Page_Orientation", "Merge_Documents_by_Page_Orientation\Merge_Documents_by_Page_Orientation.csproj", "{421CAFAF-E3E5-F807-5391-D1D713B859DA}"
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+
{421CAFAF-E3E5-F807-5391-D1D713B859DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{421CAFAF-E3E5-F807-5391-D1D713B859DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{421CAFAF-E3E5-F807-5391-D1D713B859DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{421CAFAF-E3E5-F807-5391-D1D713B859DA}.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 = {B953DA91-D41B-43BE-926C-A232CEFA4A8E}
24+
EndGlobalSection
25+
EndGlobal

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

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

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

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

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

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

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

Lines changed: 232 additions & 0 deletions
Large diffs are not rendered by default.
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>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
namespace Merge_Documents_by_Page_Orientation
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+
bool skipSection = false;
19+
// Check if the section has landscape orientation which contain table
20+
if (section.PageSetup.Orientation == PageOrientation.Landscape)
21+
{
22+
for (int i = 0; i < section.Body.ChildEntities.Count; i++)
23+
{
24+
if (section.Body.ChildEntities[i] is WTable)
25+
{
26+
skipSection = true;
27+
break;
28+
}
29+
}
30+
}
31+
// If the section is not marked to be skipped, clone and add it to the destination document
32+
if (!skipSection)
33+
{
34+
destinationDocument.Sections.Add(section.Clone());
35+
}
36+
}
37+
38+
// Copy header from other document
39+
//If document already have an header clear the header part before copying
40+
using (FileStream headerStream = new FileStream(Path.GetFullPath("../../../Data/header.rtf"), FileMode.Open))
41+
{
42+
WordDocument headerDoc = new WordDocument(headerStream, FormatType.Rtf);
43+
MoveHeader(headerDoc, destinationDocument);
44+
headerDoc.Close();
45+
}
46+
// Copy footer from other document
47+
using (FileStream footerStream = new FileStream(Path.GetFullPath("../../../Data/footer.rtf"), FileMode.Open))
48+
{
49+
WordDocument footerDoc = new WordDocument(footerStream, FormatType.Rtf);
50+
MoveFooter(footerDoc, destinationDocument);
51+
footerDoc.Close();
52+
}
53+
54+
//Saves the Word document to FileStream.
55+
using (FileStream outputStream = new FileStream(Path.GetFullPath("../../../Output/Result.docx"), FileMode.Create, FileAccess.Write))
56+
{
57+
destinationDocument.Save(outputStream, FormatType.Docx);
58+
}
59+
}
60+
}
61+
// Method to move header content from the template document to the main document
62+
static void MoveHeader(WordDocument templateDocument, WordDocument mainDocument)
63+
{
64+
//Entity Collection
65+
EntityCollection header = templateDocument.Sections[0].Body.ChildEntities;
66+
//Move all the items from one collection to another collection
67+
for (int i = 0; i < mainDocument.Sections.Count; i++)
68+
{
69+
WSection mainDocSection = mainDocument.Sections[i] as WSection;
70+
// Move the header items from the template to the main document's Header
71+
MoveItems(mainDocSection.HeadersFooters.OddHeader.ChildEntities, header);
72+
MoveItems(mainDocSection.HeadersFooters.EvenHeader.ChildEntities, header);
73+
MoveItems(mainDocSection.HeadersFooters.FirstPageHeader.ChildEntities, header);
74+
}
75+
}
76+
// Method to move footer content from the template document to the main document
77+
static void MoveFooter(WordDocument templateDocument, WordDocument mainDocument)
78+
{
79+
EntityCollection footer = templateDocument.Sections[0].Body.ChildEntities;
80+
//Move all the items from one collection to another collection
81+
for (int i = 0; i < mainDocument.Sections.Count; i++)
82+
{
83+
WSection mainDocSection = mainDocument.Sections[i] as WSection;
84+
// Move the footer items from the template to the main document's Footer
85+
MoveItems(mainDocSection.HeadersFooters.OddFooter.ChildEntities, footer);
86+
MoveItems(mainDocSection.HeadersFooters.EvenFooter.ChildEntities, footer);
87+
MoveItems(mainDocSection.HeadersFooters.FirstPageFooter.ChildEntities, footer);
88+
}
89+
}
90+
// Move the footer items from the template to the main document's header footer
91+
static void MoveItems(EntityCollection destinationDoc, EntityCollection sourceDoc)
92+
{
93+
for (int i = 0; i < sourceDoc.Count; i++)
94+
{
95+
destinationDoc.Add(sourceDoc[i].Clone());
96+
}
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)