|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | + |
| 4 | +namespace Remove_background_color_to_bookmark |
| 5 | +{ |
| 6 | + class Program |
| 7 | + { |
| 8 | + static void Main(string[] args) |
| 9 | + { |
| 10 | + // Create an input file stream to open the document |
| 11 | + using (FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read)) |
| 12 | + { |
| 13 | + //Creates a new Word document. |
| 14 | + using (WordDocument document = new WordDocument(inputStream,FormatType.Docx)) |
| 15 | + { |
| 16 | + // Create the bookmark navigator instance |
| 17 | + BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document); |
| 18 | + |
| 19 | + // Move to the bookmark |
| 20 | + bookmarkNavigator.MoveToBookmark("bkmk1"); |
| 21 | + |
| 22 | + // Get the bookmark content |
| 23 | + TextBodyPart part = bookmarkNavigator.GetBookmarkContent(); |
| 24 | + |
| 25 | + // Iterate through the content (implement this according to your needs) |
| 26 | + IterateTextBody(part.BodyItems); |
| 27 | + |
| 28 | + // Replace the bookmark content with modified content |
| 29 | + bookmarkNavigator.ReplaceBookmarkContent(part); |
| 30 | + |
| 31 | + //Creates file stream. |
| 32 | + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite)) |
| 33 | + { |
| 34 | + //Saves the Word document to file stream. |
| 35 | + document.Save(outputFileStream, FormatType.Docx); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + /// <summary> |
| 41 | + /// Iterates through the body contents of the Word document. |
| 42 | + /// </summary> |
| 43 | + /// <param name="textBodyItems"></param> |
| 44 | + private static void IterateTextBody(EntityCollection textBodyItems) |
| 45 | + { |
| 46 | + // Iterates through each of the child items of WTextBody |
| 47 | + for (int i = 0; i < textBodyItems.Count; i++) |
| 48 | + { |
| 49 | + // IEntity is the basic unit in DocIO DOM. |
| 50 | + // Accesses the body items (should be either paragraph, table or block content control) as IEntity |
| 51 | + IEntity bodyItemEntity = textBodyItems[i]; |
| 52 | + |
| 53 | + // A Text body has 3 types of elements - Paragraph, Table and Block Content Control |
| 54 | + // Decide the element type by using EntityType |
| 55 | + switch (bodyItemEntity.EntityType) |
| 56 | + { |
| 57 | + case EntityType.Paragraph: |
| 58 | + { |
| 59 | + WParagraph paragraph = bodyItemEntity as WParagraph; |
| 60 | + if (paragraph != null) |
| 61 | + { |
| 62 | + // Processes the paragraph contents |
| 63 | + // Iterates through the paragraph's DOM |
| 64 | + IterateParagraph(paragraph.Items); |
| 65 | + } |
| 66 | + break; |
| 67 | + } |
| 68 | + case EntityType.Table: |
| 69 | + { |
| 70 | + // Table is a collection of rows and cells |
| 71 | + // Iterates through table's DOM |
| 72 | + WTable table = bodyItemEntity as WTable; |
| 73 | + if (table != null) |
| 74 | + { |
| 75 | + IterateTable(table); |
| 76 | + } |
| 77 | + break; |
| 78 | + } |
| 79 | + case EntityType.BlockContentControl: |
| 80 | + { |
| 81 | + BlockContentControl blockContentControl = bodyItemEntity as BlockContentControl; |
| 82 | + if (blockContentControl != null && blockContentControl.TextBody != null) |
| 83 | + { |
| 84 | + // Iterates the body items of Block Content Control. |
| 85 | + IterateTextBody(blockContentControl.TextBody.ChildEntities); |
| 86 | + } |
| 87 | + break; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + /// <summary> |
| 93 | + /// Iterates through the table. |
| 94 | + /// </summary> |
| 95 | + /// <param name="table"></param> |
| 96 | + private static void IterateTable(WTable table) |
| 97 | + { |
| 98 | + //Iterates the row collection in a table |
| 99 | + foreach (WTableRow row in table.Rows) |
| 100 | + { |
| 101 | + //Iterates the cell collection in a table row |
| 102 | + foreach (WTableCell cell in row.Cells) |
| 103 | + { |
| 104 | + //Table cell is derived from (also a) TextBody |
| 105 | + //Reusing the code meant for iterating TextBody |
| 106 | + IterateTextBody(cell.ChildEntities); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + /// <summary> |
| 111 | + /// Iterates through the paragraph. |
| 112 | + /// </summary> |
| 113 | + /// <param name="paraItems"></param> |
| 114 | + private static void IterateParagraph(ParagraphItemCollection paraItems) |
| 115 | + { |
| 116 | + for (int i = 0; i < paraItems.Count; i++) |
| 117 | + { |
| 118 | + Entity entity = paraItems[i]; |
| 119 | + //A paragraph can have child elements such as text, image, hyperlink, symbols, etc., |
| 120 | + //Decides the element type by using EntityType |
| 121 | + switch (entity.EntityType) |
| 122 | + { |
| 123 | + case EntityType.TextRange: |
| 124 | + //Remove the text back color |
| 125 | + WTextRange textRange = entity as WTextRange; |
| 126 | + textRange.CharacterFormat.TextBackgroundColor = Syncfusion.Drawing.Color.Empty; |
| 127 | + break; |
| 128 | + case EntityType.TextBox: |
| 129 | + //Iterates to the body items of textbox. |
| 130 | + WTextBox textBox = entity as WTextBox; |
| 131 | + IterateTextBody(textBox.TextBoxBody.ChildEntities); |
| 132 | + break; |
| 133 | + case EntityType.Shape: |
| 134 | + //Iterates to the body items of shape. |
| 135 | + Shape shape = entity as Shape; |
| 136 | + IterateTextBody(shape.TextBody.ChildEntities); |
| 137 | + break; |
| 138 | + case EntityType.InlineContentControl: |
| 139 | + //Iterates to the paragraph items of inline content control. |
| 140 | + InlineContentControl inlineContentControl = entity as InlineContentControl; |
| 141 | + IterateParagraph(inlineContentControl.ParagraphItems); |
| 142 | + break; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments