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.37614.0 d17.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Merge-Documents-At-Bookmark-Without-Header-Footer", "Merge-Documents-At-Bookmark-Without-Header-Footer\Merge-Documents-At-Bookmark-Without-Header-Footer.csproj", "{FB3D4F30-E961-D224-3D61-28B0F1558896}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{FB3D4F30-E961-D224-3D61-28B0F1558896}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FB3D4F30-E961-D224-3D61-28B0F1558896}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FB3D4F30-E961-D224-3D61-28B0F1558896}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FB3D4F30-E961-D224-3D61-28B0F1558896}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F7241591-498A-45D3-987A-67EC1A189B3C}
EndGlobalSection
EndGlobal
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Merge_Documents_At_Bookmark_Without_Header_Footer</RootNamespace>
</PropertyGroup>

<ItemGroup>
<None Update="Data\DestinationDocument.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Data\SourceDocument.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>



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

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using System.IO;

namespace Merge_Documents_At_Bookmark_Without_Header_Footer
{
class Program
{
static void Main(string[] args)
{
//Load the destination Word document as a stream.
            using (FileStream destinationStreamPath = new FileStream(Path.GetFullPath(@"Data/DestinationDocument.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
                //Open the destination Word document.
                using (WordDocument destinationDocument = new WordDocument(destinationStreamPath, FormatType.Automatic))
{
//Load the source Word document as a stream.
using (FileStream sourceDocumentPathStream = new FileStream(Path.GetFullPath(@"Data/SourceDocument.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
//Open the source Word document.
using (WordDocument sourceDocument = new WordDocument(sourceDocumentPathStream, FormatType.Docx))
{
//Iterate source Word document sections.
foreach (WSection sourceDocumentSection in sourceDocument.Sections)
{
//Remove the first page header.
sourceDocumentSection.HeadersFooters.FirstPageHeader.ChildEntities.Clear();
//Remove the first page footer.
sourceDocumentSection.HeadersFooters.FirstPageFooter.ChildEntities.Clear();
//Remove the even header.
sourceDocumentSection.HeadersFooters.EvenHeader.ChildEntities.Clear();
//Remove the even footer.
sourceDocumentSection.HeadersFooters.EvenFooter.ChildEntities.Clear();
//Remove the odd header.
sourceDocumentSection.HeadersFooters.OddHeader.ChildEntities.Clear();
//Remove the odd footer.
sourceDocumentSection.HeadersFooters.OddFooter.ChildEntities.Clear();
}
//Get all the content as word document part
WordDocumentPart documentPart = new WordDocumentPart(sourceDocument);
//To maintain source document formattings
destinationDocument.ImportOptions = ImportOptions.KeepSourceFormatting;
//Create the bookmark navigator instance to access the bookmark.
BookmarksNavigator bkmkNavigator = new BookmarksNavigator(destinationDocument);
//Move the virtual cursor to the location before the end of the bookmark "MergeDocument".
bkmkNavigator.MoveToBookmark("MergeDocument");
//Replace the bookmark content with Word document part.
bkmkNavigator.ReplaceContent(documentPart);
//Remove the bookmark in the merged document
destinationDocument.Bookmarks.Remove(bkmkNavigator.CurrentBookmark);
}
}
                    //Create a file stream.
                    using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Sample.docx"), FileMode.Create, FileAccess.ReadWrite))
{
                        //Save the Word document to the file stream.
                        destinationDocument.Save(outputFileStream, FormatType.Docx);
}
}
}
}
}
}
Loading