|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | + |
| 4 | +namespace Replace_content_with_bookmark |
| 5 | +{ |
| 6 | + internal class Program |
| 7 | + { |
| 8 | + static void Main(string[] args) |
| 9 | + { |
| 10 | + using (FileStream destinationStream = new FileStream(Path.GetFullPath("Data/DestinationDocument.docx"), FileMode.Open, FileAccess.Read)) |
| 11 | + { |
| 12 | + //Open the destination Word document. |
| 13 | + using (WordDocument destinationDocument = new WordDocument(destinationStream, FormatType.Docx)) |
| 14 | + { |
| 15 | + using (FileStream sourceStream = new FileStream(Path.GetFullPath(@"Data/SourceDocument.docx"), FileMode.Open, FileAccess.Read)) |
| 16 | + { |
| 17 | + //Open the source Word document. |
| 18 | + using (WordDocument sourceDocument = new WordDocument(sourceStream, FormatType.Docx)) |
| 19 | + { |
| 20 | + //Replace text "Text one" in the destination document with content from the bookmark "bkmk1" in the source document. |
| 21 | + DocxReplaceTextWithDocPart(destinationDocument, sourceDocument, "Text one", "bkmk1"); |
| 22 | + //Replace text "Text two" in the destination document with content from the bookmark "bkmk2" in the source document. |
| 23 | + DocxReplaceTextWithDocPart(destinationDocument, sourceDocument, "Text two", "bkmk2"); |
| 24 | + //Save the modified destination document to the output stream. |
| 25 | + using (FileStream output = new FileStream(Path.GetFullPath("Output/Output.docx"), FileMode.Create, FileAccess.Write)) |
| 26 | + { |
| 27 | + destinationDocument.Save(output, FormatType.Docx); |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Replaces specific text in a Word document with bookmarked content from another document, maintaining formatting. |
| 37 | + /// </summary> |
| 38 | + private static void DocxReplaceTextWithDocPart(WordDocument destinationDocument, WordDocument sourceDocument, string tokenToFind, string textBookmark) |
| 39 | + { |
| 40 | + string bookmarkRef = textBookmark + "_bm"; |
| 41 | + |
| 42 | + // Find the text in the destination document where the bookmark start needs to be inserted. |
| 43 | + TextSelection start = destinationDocument.Find(tokenToFind, true, true); |
| 44 | + if (start != null) |
| 45 | + { |
| 46 | + // Get the selected text range and its parent paragraph. |
| 47 | + WTextRange startText = start.GetAsOneRange(); |
| 48 | + WParagraph startParagraph = startText.OwnerParagraph; |
| 49 | + // Get the index of the selected text range in the paragraph. |
| 50 | + int index = startParagraph.Items.IndexOf(startText); |
| 51 | + // Remove the selected text at the identified index. |
| 52 | + startParagraph.Items.Remove(startText); |
| 53 | + // Create a BookmarkStart with a unique reference and insert it at the same index. |
| 54 | + BookmarkStart bookmarkStart = new BookmarkStart(destinationDocument, bookmarkRef); |
| 55 | + startParagraph.Items.Insert(index, bookmarkStart); |
| 56 | + // Append a BookmarkEnd with the same reference to mark the bookmark’s end. |
| 57 | + startParagraph.AppendBookmarkEnd(bookmarkRef); |
| 58 | + |
| 59 | + // Check if the specified bookmark exists in the source document. |
| 60 | + if (sourceDocument.Bookmarks.FindByName(textBookmark) != null) |
| 61 | + { |
| 62 | + // Move the navigator to the bookmark in the source document. |
| 63 | + BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(sourceDocument); |
| 64 | + bookmarksNavigator.MoveToBookmark(textBookmark); |
| 65 | + // Extract the content within the bookmark. |
| 66 | + WordDocumentPart wordDocumentPart = bookmarksNavigator.GetContent(); |
| 67 | + |
| 68 | + // Move the navigator to the newly created bookmark in the destination document. |
| 69 | + bookmarksNavigator = new BookmarksNavigator(destinationDocument); |
| 70 | + bookmarksNavigator.MoveToBookmark(bookmarkRef); |
| 71 | + // Get the paragraph containing the bookmark start in the destination document. |
| 72 | + WParagraph destinationPara = bookmarksNavigator.CurrentBookmark.BookmarkStart.OwnerParagraph; |
| 73 | + // Store the list style, first-line indent, and left indent of the paragraph. |
| 74 | + string listStyleName = destinationPara.ListFormat.CustomStyleName; |
| 75 | + float firstLineIndent = destinationPara.ParagraphFormat.FirstLineIndent; |
| 76 | + float leftIndent = destinationPara.ParagraphFormat.LeftIndent; |
| 77 | + // Replace the bookmark content with the extracted content from the source document. |
| 78 | + bookmarksNavigator.ReplaceContent(wordDocumentPart); |
| 79 | + // Reapply the original list style and indent settings to the paragraph. |
| 80 | + destinationPara.ListFormat.ApplyStyle(listStyleName); |
| 81 | + destinationPara.ParagraphFormat.FirstLineIndent = firstLineIndent; |
| 82 | + destinationPara.ParagraphFormat.LeftIndent = leftIndent; |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + // If the bookmark is not found, replace the content with an empty string. |
| 87 | + BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(destinationDocument); |
| 88 | + bookmarksNavigator.MoveToBookmark(bookmarkRef); |
| 89 | + bookmarksNavigator.ReplaceBookmarkContent(string.Empty, true); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments