Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.37301.10 d17.14
MinimumVisualStudioVersion = 10.0.40219.1
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{421CAFAF-E3E5-F807-5391-D1D713B859DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{421CAFAF-E3E5-F807-5391-D1D713B859DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{421CAFAF-E3E5-F807-5391-D1D713B859DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{421CAFAF-E3E5-F807-5391-D1D713B859DA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B953DA91-D41B-43BE-926C-A232CEFA4A8E}
EndGlobalSection
EndGlobal

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>


<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>
<ItemGroup>
<None Update="Data\Template.rtf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\footer.rtf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\header.rtf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\MainDocument.rtf">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;

namespace Merge_Documents_by_Page_Orientation
{
class Program
{
static void Main(string[] args)
{
using (FileStream sourceStream = new FileStream(Path.GetFullPath("../../../Data/MainDocument.rtf"), FileMode.Open))
using (FileStream destinationStream = new FileStream(Path.GetFullPath("../../../Data/Template.rtf"), FileMode.Open))
using (WordDocument sourceDocument = new WordDocument(sourceStream, FormatType.Automatic))
using (WordDocument destinationDocument = new WordDocument(destinationStream, FormatType.Rtf))
{
//Iterate through each section in the source document
foreach (WSection section in sourceDocument.Sections)
{
bool skipSection = false;
// Check if the section has landscape orientation which contain table
if (section.PageSetup.Orientation == PageOrientation.Landscape)
{
for (int i = 0; i < section.Body.ChildEntities.Count; i++)
{
if (section.Body.ChildEntities[i] is WTable)
{
skipSection = true;
break;
}
}
}
// If the section is not marked to be skipped, clone and add it to the destination document
if (!skipSection)
{
destinationDocument.Sections.Add(section.Clone());
}
}

// Copy header from other document
//If document already have an header clear the header part before copying
using (FileStream headerStream = new FileStream(Path.GetFullPath("../../../Data/header.rtf"), FileMode.Open))
{
WordDocument headerDoc = new WordDocument(headerStream, FormatType.Rtf);
MoveHeader(headerDoc, destinationDocument);
headerDoc.Close();
}
// Copy footer from other document
using (FileStream footerStream = new FileStream(Path.GetFullPath("../../../Data/footer.rtf"), FileMode.Open))
{
WordDocument footerDoc = new WordDocument(footerStream, FormatType.Rtf);
MoveFooter(footerDoc, destinationDocument);
footerDoc.Close();
}

//Saves the Word document to FileStream.
using (FileStream outputStream = new FileStream(Path.GetFullPath("../../../Output/Result.docx"), FileMode.Create, FileAccess.Write))
{
destinationDocument.Save(outputStream, FormatType.Docx);
}
}
}
// Method to move header content from the template document to the main document
static void MoveHeader(WordDocument templateDocument, WordDocument mainDocument)
{
//Entity Collection
EntityCollection header = templateDocument.Sections[0].Body.ChildEntities;
//Move all the items from one collection to another collection
for (int i = 0; i < mainDocument.Sections.Count; i++)
{
WSection mainDocSection = mainDocument.Sections[i] as WSection;
// Move the header items from the template to the main document's Header
MoveItems(mainDocSection.HeadersFooters.OddHeader.ChildEntities, header);
MoveItems(mainDocSection.HeadersFooters.EvenHeader.ChildEntities, header);
MoveItems(mainDocSection.HeadersFooters.FirstPageHeader.ChildEntities, header);
}
}
// Method to move footer content from the template document to the main document
static void MoveFooter(WordDocument templateDocument, WordDocument mainDocument)
{
EntityCollection footer = templateDocument.Sections[0].Body.ChildEntities;
//Move all the items from one collection to another collection
for (int i = 0; i < mainDocument.Sections.Count; i++)
{
WSection mainDocSection = mainDocument.Sections[i] as WSection;
// Move the footer items from the template to the main document's Footer
MoveItems(mainDocSection.HeadersFooters.OddFooter.ChildEntities, footer);
MoveItems(mainDocSection.HeadersFooters.EvenFooter.ChildEntities, footer);
MoveItems(mainDocSection.HeadersFooters.FirstPageFooter.ChildEntities, footer);
}
}
// Move the footer items from the template to the main document's header footer
static void MoveItems(EntityCollection destinationDoc, EntityCollection sourceDoc)
{
for (int i = 0; i < sourceDoc.Count; i++)
{
destinationDoc.Add(sourceDoc[i].Clone());
}
}
}
}
Loading