|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | + |
| 4 | + |
| 5 | +//Opens an existing Word document |
| 6 | +using (FileStream inputStream = new FileStream("../../../Data/Sample.docx", FileMode.Open, FileAccess.Read)) |
| 7 | +{ |
| 8 | + using (WordDocument document = new WordDocument(inputStream, FormatType.Docx)) |
| 9 | + { |
| 10 | + WTextBody textBody = null; |
| 11 | + //Iterates sections in Word document. |
| 12 | + for (int i = document.Sections.Count - 1; i >= 0; i--) |
| 13 | + { |
| 14 | + //Accesses the Body of section where all the contents in document are apart |
| 15 | + textBody = document.Sections[i].Body; |
| 16 | + //Removes the last empty page in the Word document |
| 17 | + RemoveEmptyItems(textBody); |
| 18 | + //Removes the empty sections in the document |
| 19 | + if (textBody.ChildEntities.Count == 0) |
| 20 | + { |
| 21 | + int SectionIndex = document.ChildEntities.IndexOf(document.Sections[i]); |
| 22 | + document.ChildEntities.RemoveAt(SectionIndex); |
| 23 | + } |
| 24 | + } |
| 25 | + using (FileStream outputStream = new FileStream("../../../Data/Output.docx", FileMode.Create, FileAccess.Write)) |
| 26 | + { |
| 27 | + //Saves and closes the Word document |
| 28 | + document.Save(outputStream, FormatType.Docx); |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | +/// <summary> |
| 33 | +/// Remove the empty paragraph in the Word document. |
| 34 | +/// </summary> |
| 35 | +void RemoveEmptyItems(WTextBody textBody) |
| 36 | +{ |
| 37 | + //A flag to determine any renderable item found in the Word document. |
| 38 | + bool IsRenderableItem = false; |
| 39 | + //A flag to determine a page break is found as the previous item |
| 40 | + bool HasPrevPageBreak = false; |
| 41 | + //Iterates into textbody items. |
| 42 | + for (int itemIndex = textBody.ChildEntities.Count - 1; itemIndex >= 0; itemIndex--) |
| 43 | + { |
| 44 | + //Checks item is empty paragraph and removes it. |
| 45 | + if (textBody.ChildEntities[itemIndex] is WParagraph) |
| 46 | + { |
| 47 | + WParagraph paragraph = textBody.ChildEntities[itemIndex] as WParagraph; |
| 48 | + //Iterates into paragraph |
| 49 | + for (int pIndex = paragraph.Items.Count - 1; pIndex >= 0; pIndex--) |
| 50 | + { |
| 51 | + ParagraphItem paragraphItem = paragraph.Items[pIndex]; |
| 52 | + |
| 53 | + //Removes page breaks |
| 54 | + if ((paragraphItem is Break && (paragraphItem as Break).BreakType == BreakType.PageBreak)) |
| 55 | + { |
| 56 | + if (HasPrevPageBreak) |
| 57 | + paragraph.Items.RemoveAt(pIndex); |
| 58 | + else |
| 59 | + HasPrevPageBreak = true; |
| 60 | + } |
| 61 | + //Check paragraph contains any renderable items. |
| 62 | + else |
| 63 | + { |
| 64 | + HasPrevPageBreak = false; |
| 65 | + if (!(paragraphItem is BookmarkStart || paragraphItem is BookmarkEnd)) |
| 66 | + { |
| 67 | + //Found renderable item and break the iteration. |
| 68 | + IsRenderableItem = true; |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + //Remove empty paragraph and the paragraph with bookmarks only |
| 74 | + if (paragraph.Items.Count == 0 || !IsRenderableItem) |
| 75 | + textBody.ChildEntities.RemoveAt(itemIndex); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments