|
| 1 | +# Replace bookmark content with Word document part using C# |
| 2 | + |
| 3 | +The Syncfusion [.NET Word Library](https://www.syncfusion.com/document-processing/word-framework/net/word-library) (DocIO) enables you to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can **replace bookmark content in a Word document with another Word document content** using C#. |
| 4 | + |
| 5 | +## Steps to replace bookmark content programmatically |
| 6 | + |
| 7 | +Step 1: Create a new .NET Core console application project. |
| 8 | + |
| 9 | +Step 2: Install the [Syncfusion.DocIO.Net.Core](https://www.nuget.org/packages/Syncfusion.DocIO.Net.Core) NuGet package as a reference to your project from [NuGet.org](https://www.nuget.org/). |
| 10 | + |
| 11 | +Step 3: Include the following namespaces in the Program.cs file. |
| 12 | + |
| 13 | +```csharp |
| 14 | +using Syncfusion.DocIO; |
| 15 | +using Syncfusion.DocIO.DLS; |
| 16 | +using System.IO; |
| 17 | +``` |
| 18 | + |
| 19 | +Step 4: Add the following code snippet in Program.cs file to replace bookmark content with another Word document part. |
| 20 | + |
| 21 | +```csharp |
| 22 | +using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
| 23 | +{ |
| 24 | + //Opens an existing Word document. |
| 25 | + using (WordDocument templateDocument = new WordDocument(fileStreamPath, FormatType.Automatic)) |
| 26 | + { |
| 27 | + //Creates the bookmark navigator instance to access the bookmark. |
| 28 | + BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(templateDocument); |
| 29 | + //Moves the virtual cursor to the location before the end of the bookmark "Northwind". |
| 30 | + bookmarkNavigator.MoveToBookmark("Northwind"); |
| 31 | + //Gets the bookmark content as WordDocumentPart. |
| 32 | + WordDocumentPart wordDocumentPart = bookmarkNavigator.GetContent(); |
| 33 | + //Loads the Word document with bookmark NorthwindDB. |
| 34 | + using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Bookmarks.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
| 35 | + { |
| 36 | + using (WordDocument document = new WordDocument(fileStream, FormatType.Docx)) |
| 37 | + { |
| 38 | + //Creates the bookmark navigator instance to access the bookmark. |
| 39 | + bookmarkNavigator = new BookmarksNavigator(document); |
| 40 | + //Moves the virtual cursor to the location before the end of the bookmark "NorthwindDB". |
| 41 | + bookmarkNavigator.MoveToBookmark("NorthwindDB"); |
| 42 | + //Replaces the bookmark content with word body part. |
| 43 | + bookmarkNavigator.ReplaceContent(wordDocumentPart); |
| 44 | + //Close the WordDocumentPart instance. |
| 45 | + wordDocumentPart.Close(); |
| 46 | + //Creates file stream. |
| 47 | + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.docx"), FileMode.Create, FileAccess.ReadWrite)) |
| 48 | + { |
| 49 | + //Saves the Word document to file stream. |
| 50 | + document.Save(outputFileStream, FormatType.Docx); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +More information about the bookmarks can be found in this [documentation](https://help.syncfusion.com/document-processing/word/word-library/net/working-with-bookmarks) section. |
0 commit comments