|
| 1 | +using Syncfusion.DocIO; |
| 2 | +using Syncfusion.DocIO.DLS; |
| 3 | +using Syncfusion.Drawing; |
| 4 | +using System.IO; |
| 5 | + |
| 6 | +namespace Apply_shading_to_fields |
| 7 | +{ |
| 8 | + class Program |
| 9 | + { |
| 10 | + static void Main(string[] args) |
| 11 | + { |
| 12 | + // Creates a new instance of WordDocument to work with. |
| 13 | + using (WordDocument document = new WordDocument()) |
| 14 | + { |
| 15 | + // Add a new section to the Word document. |
| 16 | + IWSection section = document.AddSection(); |
| 17 | + IWParagraph paragraph = section.AddParagraph(); |
| 18 | + |
| 19 | + // Add text before the field (e.g., "Date Field - "). |
| 20 | + IWTextRange firstText = paragraph.AppendText("Date Field - "); |
| 21 | + |
| 22 | + // Adds a new Date field to the paragraph with the specified format. |
| 23 | + WField field = paragraph.AppendField("Date", FieldType.FieldDate) as WField; |
| 24 | + // Set the field code to display the date in "MMMM d, yyyy" format. |
| 25 | + field.FieldCode = @"DATE \@" + "\"MMMM d, yyyy\""; |
| 26 | + |
| 27 | + // Reference the field as an entity. |
| 28 | + IEntity entity = field; |
| 29 | + |
| 30 | + // Apply shading to the field. |
| 31 | + ApplyShading(entity); |
| 32 | + |
| 33 | + // Add another paragraph for the "If" field. |
| 34 | + paragraph = section.AddParagraph(); |
| 35 | + paragraph.AppendText("If Field - "); |
| 36 | + |
| 37 | + // Creates a new IF field. |
| 38 | + field = paragraph.AppendField("If", FieldType.FieldIf) as WIfField; |
| 39 | + // Specifies the field code for the IF statement with true and false branches. |
| 40 | + field.FieldCode = "IF \"True\" = \"True\" \"The given statement is Correct\" \"The given statement is Wrong\""; |
| 41 | + entity = field; |
| 42 | + |
| 43 | + // Apply shading to the field. |
| 44 | + ApplyShading(entity); |
| 45 | + |
| 46 | + // Updates the fields in the document. |
| 47 | + document.UpdateDocumentFields(); |
| 48 | + |
| 49 | + // Create a file stream to save the document. |
| 50 | + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite)) |
| 51 | + { |
| 52 | + // Saves the Word document to the specified file stream in DOCX format. |
| 53 | + document.Save(outputFileStream, FormatType.Docx); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Method to apply shading (highlight) to the content of a field. |
| 59 | + static void ApplyShading(IEntity entity) |
| 60 | + { |
| 61 | + // Loop through the siblings of the current entity (field and its contents) until reaching the FieldEnd. |
| 62 | + while (entity.NextSibling != null) |
| 63 | + { |
| 64 | + // Check if the entity is a text range. |
| 65 | + if (entity is WTextRange) |
| 66 | + { |
| 67 | + // Set the highlight color to LightGray for the text range. |
| 68 | + (entity as WTextRange).CharacterFormat.HighlightColor = Color.LightGray; |
| 69 | + } |
| 70 | + // Check if the entity is a field mark and is of FieldEnd type. |
| 71 | + else if ((entity is WFieldMark) && (entity as WFieldMark).Type == FieldMarkType.FieldEnd) |
| 72 | + { |
| 73 | + // Break out of the loop once the end of the field is reached. |
| 74 | + break; |
| 75 | + } |
| 76 | + |
| 77 | + // Move to the next sibling entity (next part of the field). |
| 78 | + entity = entity.NextSibling; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments