|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | + |
| 4 | + |
| 5 | +// Load the Word document |
| 6 | +using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx"))) |
| 7 | +{ |
| 8 | + // Iterate through all sections in the document |
| 9 | + foreach (WSection section in document.Sections) |
| 10 | + { |
| 11 | + // Iterate through tables in reverse order to safely remove them if needed |
| 12 | + for (int i = section.Tables.Count - 1; i >= 0; i--) |
| 13 | + { |
| 14 | + WTable table = (WTable)section.Tables[i]; |
| 15 | + |
| 16 | + #region RemoveNestedTableFirst |
| 17 | + // Iterate through rows of the table |
| 18 | + foreach (WTableRow row in table.Rows) |
| 19 | + { |
| 20 | + // Iterate through cells of the row |
| 21 | + foreach (WTableCell cell in row.Cells) |
| 22 | + { |
| 23 | + // Iterate through child entities in reverse order for safe removal |
| 24 | + for (int j = cell.ChildEntities.Count - 1; j >= 0; j--) |
| 25 | + { |
| 26 | + Entity entity = cell.ChildEntities[j]; |
| 27 | + |
| 28 | + // Check if entity is a table and if it's completely empty |
| 29 | + if (entity.EntityType == EntityType.Table && IsTableCompletelyEmpty(entity as WTable)) |
| 30 | + { |
| 31 | + cell.ChildEntities.Remove(entity); // Remove empty nested table |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + #endregion |
| 37 | + |
| 38 | + // If the entire table is empty, remove it from the section |
| 39 | + if (IsTableCompletelyEmpty(table)) |
| 40 | + { |
| 41 | + section.Body.ChildEntities.Remove(table); |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + // Save the modified document |
| 46 | + document.Save(Path.GetFullPath(@"Output/Result.docx"), FormatType.Docx); |
| 47 | +} |
| 48 | + |
| 49 | +/// <summary> |
| 50 | +/// Checks whether table is empty |
| 51 | +/// </summary> |
| 52 | +/// <param name="table"></param> |
| 53 | +/// <returns>True if table is empty, Otherwise False.</returns> |
| 54 | +static bool IsTableCompletelyEmpty(WTable table) |
| 55 | +{ |
| 56 | + for (int i = 0; i < table.Rows.Count; i++) |
| 57 | + { |
| 58 | + WTableRow row = table.Rows[i]; |
| 59 | + |
| 60 | + for (int j = 0; j < row.Cells.Count; j++) |
| 61 | + { |
| 62 | + WTableCell cell = row.Cells[j]; |
| 63 | + // If any cell contains content, the table is not empty |
| 64 | + if (!IsTextBodyEmpty(cell)) |
| 65 | + return false; |
| 66 | + } |
| 67 | + } |
| 68 | + return true; |
| 69 | +} |
| 70 | +/// <summary> |
| 71 | +/// Checks whether text body is empty |
| 72 | +/// </summary> |
| 73 | +/// <param name="textBody"></param> |
| 74 | +/// <returns></returns> |
| 75 | +static bool IsTextBodyEmpty(WTextBody textBody) |
| 76 | +{ |
| 77 | + for (int i = textBody.ChildEntities.Count - 1; i >= 0; i--) |
| 78 | + { |
| 79 | + Entity entity = textBody.ChildEntities[i]; |
| 80 | + |
| 81 | + switch (entity.EntityType) |
| 82 | + { |
| 83 | + case EntityType.Paragraph: |
| 84 | + if (!IsParagraphEmpty(entity as WParagraph)) |
| 85 | + return false; |
| 86 | + break; |
| 87 | + case EntityType.BlockContentControl: |
| 88 | + if (!IsTextBodyEmpty((entity as BlockContentControl).TextBody)) |
| 89 | + return false; |
| 90 | + break; |
| 91 | + } |
| 92 | + } |
| 93 | + return true; |
| 94 | +} |
| 95 | +/// <summary> |
| 96 | +/// Checks whether paragraph is empty |
| 97 | +/// </summary> |
| 98 | +/// <param name="textBody"></param> |
| 99 | +/// <returns></returns> |
| 100 | +static bool IsParagraphEmpty(WParagraph paragraph) |
| 101 | +{ |
| 102 | + for (int i = 0; i < paragraph.ChildEntities.Count; i++) |
| 103 | + { |
| 104 | + Entity entity = paragraph.ChildEntities[i]; |
| 105 | + switch (entity.EntityType) |
| 106 | + { |
| 107 | + case EntityType.TextRange: |
| 108 | + WTextRange textRange = entity as WTextRange; |
| 109 | + if (!string.IsNullOrEmpty(textRange.Text)) |
| 110 | + return false; |
| 111 | + break; |
| 112 | + case EntityType.BookmarkStart: |
| 113 | + case EntityType.BookmarkEnd: |
| 114 | + case EntityType.EditableRangeStart: |
| 115 | + case EntityType.EditableRangeEnd: |
| 116 | + break; |
| 117 | + default: |
| 118 | + return false; |
| 119 | + } |
| 120 | + } |
| 121 | + return true; |
| 122 | +} |
0 commit comments