|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | + |
| 4 | +namespace Extract_Selected_CheckBox |
| 5 | +{ |
| 6 | + class Program |
| 7 | + { |
| 8 | + |
| 9 | + static void Main(string[] args) |
| 10 | + { |
| 11 | + // Load the word document |
| 12 | + using (FileStream fileStream = new FileStream("../../../Data/Template.docx", FileMode.Open, FileAccess.Read)) |
| 13 | + { |
| 14 | + using (WordDocument document = new WordDocument(fileStream, FormatType.Docx)) |
| 15 | + { |
| 16 | + foreach (WSection section in document.Sections) |
| 17 | + { |
| 18 | + //Accesses the Body of section where all the contents in document are apart |
| 19 | + WTextBody sectionBody = section.Body; |
| 20 | + IterateTextBody(sectionBody); |
| 21 | + WHeadersFooters headersFooters = section.HeadersFooters; |
| 22 | + //Consider that OddHeader and OddFooter are applied to this document |
| 23 | + //Iterates through the TextBody of OddHeader and OddFooter |
| 24 | + IterateTextBody(headersFooters.OddHeader); |
| 25 | + IterateTextBody(headersFooters.OddFooter); |
| 26 | + } |
| 27 | + using (FileStream outputStream = new FileStream("../../../Output/Result.docx", FileMode.Create, FileAccess.Write)) |
| 28 | + { |
| 29 | + //Saves the stream as Word file |
| 30 | + document.Save(outputStream, FormatType.Docx); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + public static void IterateTextBody(WTextBody textBody) |
| 36 | + { |
| 37 | + //Iterates through each of the child items of WTextBody |
| 38 | + for (int i = 0; i < textBody.ChildEntities.Count; i++) |
| 39 | + { |
| 40 | + //IEntity is the basic unit in DocIO DOM. |
| 41 | + //Accesses the body items (should be either paragraph, table or block content control) as IEntity |
| 42 | + IEntity bodyItemEntity = textBody.ChildEntities[i]; |
| 43 | + //A Text body has 3 types of elements - Paragraph, Table and Block Content Control |
| 44 | + //Decides the element type by using EntityType |
| 45 | + switch (bodyItemEntity.EntityType) |
| 46 | + { |
| 47 | + case EntityType.Paragraph: |
| 48 | + WParagraph paragraph = bodyItemEntity as WParagraph; |
| 49 | + //Processes the paragraph contents |
| 50 | + //Iterates through the paragraph's DOM |
| 51 | + IterateParagraph(paragraph.Items); |
| 52 | + break; |
| 53 | + case EntityType.Table: |
| 54 | + //Table is a collection of rows and cells |
| 55 | + //Iterates through table's DOM |
| 56 | + IterateTable(bodyItemEntity as WTable); |
| 57 | + break; |
| 58 | + case EntityType.BlockContentControl: |
| 59 | + BlockContentControl blockContentControl = bodyItemEntity as BlockContentControl; |
| 60 | + if (blockContentControl.ContentControlProperties.Type == ContentControlType.CheckBox) |
| 61 | + blockContentControl.ContentControlProperties.IsChecked = false; |
| 62 | + //Iterates to the body items of Block Content Control. |
| 63 | + IterateTextBody(blockContentControl.TextBody); |
| 64 | + break; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + public static void IterateTable(WTable table) |
| 69 | + { |
| 70 | + //Iterates the row collection in a table |
| 71 | + foreach (WTableRow row in table.Rows) |
| 72 | + { |
| 73 | + //Iterates the cell collection in a table row |
| 74 | + foreach (WTableCell cell in row.Cells) |
| 75 | + { |
| 76 | + //Table cell is derived from (also a) TextBody |
| 77 | + //Reusing the code meant for iterating TextBody |
| 78 | + IterateTextBody(cell); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + public static void IterateParagraph(ParagraphItemCollection paraItems) |
| 83 | + { |
| 84 | + for (int i = 0; i < paraItems.Count; i++) |
| 85 | + { |
| 86 | + Entity entity = paraItems[i]; |
| 87 | + //A paragraph can have child elements such as text, image, hyperlink, symbols, etc., |
| 88 | + //Decides the element type by using EntityType |
| 89 | + switch (entity.EntityType) |
| 90 | + { |
| 91 | + case EntityType.CheckBox: |
| 92 | + if (entity is WCheckBox) |
| 93 | + (entity as WCheckBox).Checked = false; |
| 94 | + break; |
| 95 | + case EntityType.TextBox: |
| 96 | + //Iterates to the body items of textbox. |
| 97 | + WTextBox textBox = entity as WTextBox; |
| 98 | + IterateTextBody(textBox.TextBoxBody); |
| 99 | + break; |
| 100 | + case EntityType.Shape: |
| 101 | + //Iterates to the body items of shape. |
| 102 | + Shape shape = entity as Shape; |
| 103 | + IterateTextBody(shape.TextBody); |
| 104 | + break; |
| 105 | + case EntityType.InlineContentControl: |
| 106 | + //Iterates to the paragraph items of inline content control. |
| 107 | + InlineContentControl inlineContentControl = entity as InlineContentControl; |
| 108 | + if (inlineContentControl.ContentControlProperties.Type == ContentControlType.CheckBox) |
| 109 | + inlineContentControl.ContentControlProperties.IsChecked = false; |
| 110 | + IterateParagraph(inlineContentControl.ParagraphItems); |
| 111 | + break; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments