How to replace bookmark content in one Word document with all the content from another Word document?
Syncfusion Essential DocIO is a .NET Core Word library used to create, read, and edit Word documents programmatically without Microsoft Word or Interop dependencies. Using this library, you can replace bookmark content in one Word document with all the content from another Word document in C#.
Steps to replace bookmark content in one Word document with all the content from another Word document
- Create a new C# .NET Core console application project.
- Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your .NET Core applications from NuGet.org.
- Include the following namespace in the Program.cs file.
C#
using Syncfusion.DocIO; using Syncfusion.DocIO.DLS;
- Use the following code example to replace bookmark content in one Word document with all the content from another Word document.
C#
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"../../../Data/DestinationWordDocument.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { //Open destination Word document. using (WordDocument destinationWordDocument = new WordDocument(fileStreamPath, FormatType.Automatic)) { using (FileStream sourceFileStream = new FileStream(Path.GetFullPath(@"../../../Data/SourceWordDocument.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { //Open the source Word document to copy all the content. using (WordDocument sourceWordDocument = new WordDocument(sourceFileStream, FormatType.Automatic)) { //Get all the content as Word document part. WordDocumentPart wordDocumentPart = new WordDocumentPart(sourceWordDocument); //Create the bookmark navigator instance to access the bookmark. BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(destinationWordDocument); //Move the virtual cursor to the location before the end of the bookmark "Adventure_Bkmk". bookmarkNavigator.MoveToBookmark("Adventure_Bkmk"); //Replace the bookmark content with Word document part. bookmarkNavigator.ReplaceContent(wordDocumentPart); //Create file stream. using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Result.docx"), FileMode.Create, FileAccess.ReadWrite)) { //Save the Word document to file stream. destinationWordDocument.Save(outputFileStream, FormatType.Docx); } } } } }
A complete working sample to replace bookmark content in one Word document with all the content from another Word document in C# can be downloaded from GitHub.
Input destination Word document with highlighted bookmark and source Word documents as shown below.
By executing the program, you will get the output Word document as follows.
Take a moment to peruse the documentation where you can find basic Word document processing options along with the features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.
Explore more about the rich set of Syncfusion Word Framework features.
How to replace the content between two different existing bookmarks in a word document
How do I insert hidden bookmarks to the document?
How to replace text within bookmark content in Word document?
Is it possible to insert table in BookMark Content?
Conclusion
I hope you enjoyed learning about how to replace bookmark content in one Word document with all the content from another Word document.
You can refer to our ASP.NET Core DocIO’s feature tour page to know about its other groundbreaking feature representations. You can also explore our ASP.NET Core DocIO documentation to understand how to present and manipulate data.
For current customers, you can check out our ASP.NET Core components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our ASP.NET Core DocIO and other ASP.NET Core components.
If you have any queries or require clarifications, please let us know in the comment section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!