|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | +using Syncfusion.DocIORenderer; |
| 4 | +using Syncfusion.Pdf; |
| 5 | +using System.IO; |
| 6 | + |
| 7 | + |
| 8 | +namespace Find_And_Remove_LongText |
| 9 | +{ |
| 10 | + class Program |
| 11 | + { |
| 12 | + // Set text length to be remove from the document |
| 13 | + static int requiredLongTextLength = 300; |
| 14 | + |
| 15 | + static void Main(string[] args) |
| 16 | + { |
| 17 | + // Open an existing word document |
| 18 | + using (FileStream inputFileStream = new FileStream(Path.GetFullPath(@"../../../Data/Input.docx"), FileMode.Open)) |
| 19 | + { |
| 20 | + //Loads an existing Word document. |
| 21 | + using (WordDocument wordDocument = new WordDocument(inputFileStream, FormatType.Docx)) |
| 22 | + { |
| 23 | + // Remove long texts |
| 24 | + CheckAndRemoveLongText(wordDocument); |
| 25 | + //Creates an instance of DocIORenderer. |
| 26 | + using (DocIORenderer renderer = new DocIORenderer()) |
| 27 | + { |
| 28 | + //Converts Word document into PDF document. |
| 29 | + using (PdfDocument pdfDocument = renderer.ConvertToPDF(wordDocument)) |
| 30 | + { |
| 31 | + //Saves the PDF file to file system. |
| 32 | + using (FileStream outputStream = new FileStream(Path.GetFullPath(@"..\..\..\Output\WordToPDF.pdf"), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)) |
| 33 | + { |
| 34 | + pdfDocument.Save(outputStream); |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + private static void CheckAndRemoveLongText(WordDocument document) |
| 42 | + { |
| 43 | + List<Entity> paragraphs = document.FindAllItemsByProperty(EntityType.Paragraph, null, null); |
| 44 | + foreach (Entity paragraph in paragraphs) |
| 45 | + { |
| 46 | + WParagraph wParagraph = paragraph as WParagraph; |
| 47 | + IterateParagraph(wParagraph.Items); |
| 48 | + } |
| 49 | + } |
| 50 | + private static void IterateTextBody(WTextBody textBody) |
| 51 | + { |
| 52 | + //Iterates through each of the child items of WTextBody |
| 53 | + for (int i = 0; i < textBody.ChildEntities.Count; i++) |
| 54 | + { |
| 55 | + //IEntity is the basic unit in DocIO DOM. |
| 56 | + //Accesses the body items (should be either paragraph, table or block content control) as IEntity |
| 57 | + IEntity bodyItemEntity = textBody.ChildEntities[i]; |
| 58 | + //A Text body has 3 types of elements - Paragraph, Table and Block Content Control |
| 59 | + //Decides the element type by using EntityType |
| 60 | + switch (bodyItemEntity.EntityType) |
| 61 | + { |
| 62 | + case EntityType.Paragraph: |
| 63 | + WParagraph paragraph = bodyItemEntity as WParagraph; |
| 64 | + //Processes the paragraph contents |
| 65 | + //Iterates through the paragraph's DOM |
| 66 | + IterateParagraph(paragraph.Items); |
| 67 | + break; |
| 68 | + case EntityType.Table: |
| 69 | + //Table is a collection of rows and cells |
| 70 | + //Iterates through table's DOM |
| 71 | + IterateTable(bodyItemEntity as WTable); |
| 72 | + break; |
| 73 | + case EntityType.BlockContentControl: |
| 74 | + BlockContentControl blockContentControl = bodyItemEntity as BlockContentControl; |
| 75 | + //Iterates to the body items of Block Content Control. |
| 76 | + IterateTextBody(blockContentControl.TextBody); |
| 77 | + break; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + private static void IterateTable(WTable table) |
| 82 | + { |
| 83 | + //Iterates the row collection in a table |
| 84 | + foreach (WTableRow row in table.Rows) |
| 85 | + { |
| 86 | + //Iterates the cell collection in a table row |
| 87 | + foreach (WTableCell cell in row.Cells) |
| 88 | + { |
| 89 | + //Table cell is derived from (also a) TextBody |
| 90 | + //Reusing the code meant for iterating TextBody |
| 91 | + IterateTextBody(cell); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + private static void IterateParagraph(ParagraphItemCollection paraItems) |
| 96 | + { |
| 97 | + for (int i = 0; i < paraItems.Count; i++) |
| 98 | + { |
| 99 | + Entity entity = paraItems[i]; |
| 100 | + //A paragraph can have child elements such as text, image, hyperlink, symbols, etc., |
| 101 | + //Decides the element type by using EntityType |
| 102 | + switch (entity.EntityType) |
| 103 | + { |
| 104 | + case EntityType.TextRange: |
| 105 | + WTextRange textRange = entity as WTextRange; |
| 106 | + //Find and remove the long text in Word document. |
| 107 | + if (textRange.Text.Length >= requiredLongTextLength) |
| 108 | + { |
| 109 | + (entity as WTextRange).Text = string.Empty; |
| 110 | + } |
| 111 | + break; |
| 112 | + case EntityType.TextBox: |
| 113 | + //Iterates to the body items of textbox. |
| 114 | + WTextBox textBox = entity as WTextBox; |
| 115 | + IterateTextBody(textBox.TextBoxBody); |
| 116 | + break; |
| 117 | + case EntityType.Shape: |
| 118 | + //Iterates to the body items of shape. |
| 119 | + Shape shape = entity as Shape; |
| 120 | + IterateTextBody(shape.TextBody); |
| 121 | + break; |
| 122 | + case EntityType.InlineContentControl: |
| 123 | + //Iterates to the paragraph items of inline content control. |
| 124 | + InlineContentControl inlineContentControl = entity as InlineContentControl; |
| 125 | + IterateParagraph(inlineContentControl.ParagraphItems); |
| 126 | + break; |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments