|
| 1 | +using System.Diagnostics; |
| 2 | +using Create_IF_Field.Models; |
| 3 | +using Microsoft.AspNetCore.Mvc; |
| 4 | +using Syncfusion.DocIO; |
| 5 | +using Syncfusion.DocIO.DLS; |
| 6 | + |
| 7 | +namespace Create_IF_Field.Controllers |
| 8 | +{ |
| 9 | + public class HomeController : Controller |
| 10 | + { |
| 11 | + private readonly ILogger<HomeController> _logger; |
| 12 | + |
| 13 | + public HomeController(ILogger<HomeController> logger) |
| 14 | + { |
| 15 | + _logger = logger; |
| 16 | + } |
| 17 | + |
| 18 | + public IActionResult SimpleIfField() |
| 19 | + { |
| 20 | + // Create a new Word document |
| 21 | + WordDocument document = new WordDocument(); |
| 22 | + |
| 23 | + // Add a section and a title |
| 24 | + document.EnsureMinimal(); |
| 25 | + IWParagraph titleParagraph = document.LastSection.AddParagraph(); |
| 26 | + titleParagraph.AppendText("Student Grade Evaluation Report"); |
| 27 | + titleParagraph.ApplyStyle(BuiltinStyle.Heading1); |
| 28 | + titleParagraph.ParagraphFormat.LineSpacing = 18f; |
| 29 | + |
| 30 | + // Add introductory explanation |
| 31 | + IWParagraph introParagraph = document.LastSection.AddParagraph(); |
| 32 | + introParagraph.AppendText("This report evaluates a student's performance based on their exam score. " |
| 33 | + + "If the score meets or exceeds the passing mark, the student is considered to have passed. " |
| 34 | + + "Otherwise, the student is marked as failed."); |
| 35 | + introParagraph.ParagraphFormat.LineSpacing = 18f; |
| 36 | + |
| 37 | + // Add details of the evaluation criteria |
| 38 | + IWParagraph criteriaParagraph = document.LastSection.AddParagraph(); |
| 39 | + criteriaParagraph.AppendText("Evaluation Criteria:\n" |
| 40 | + + "• Passing Mark: 50\n" |
| 41 | + + "• Student Score: 75\n" |
| 42 | + + "• Condition: IF Student Score >= Passing Mark"); |
| 43 | + criteriaParagraph.ParagraphFormat.LineSpacing = 18f; |
| 44 | + |
| 45 | + // Add a paragraph with the IF field |
| 46 | + IWParagraph resultParagraph = document.LastSection.AddParagraph(); |
| 47 | + resultParagraph.AppendText("Evaluation Result: "); |
| 48 | + resultParagraph.ParagraphFormat.LineSpacing = 18f; |
| 49 | + |
| 50 | + // Add the IF field to show pass/fail result |
| 51 | + WIfField? gradeIfField = resultParagraph.AppendField("If", FieldType.FieldIf) as WIfField; |
| 52 | + gradeIfField!.FieldCode = "IF 75 >= 50 \"Pass ✅\" \"Fail ❌\""; |
| 53 | + |
| 54 | + // Update fields to evaluate the IF condition |
| 55 | + document.UpdateDocumentFields(); |
| 56 | + |
| 57 | + // Return the document as a downloadable file |
| 58 | + return CreateFileResult(document, "SimpleIfField.docx"); |
| 59 | + } |
| 60 | + |
| 61 | + public IActionResult IfFieldWithRichContent() |
| 62 | + { |
| 63 | + // Load an existing Word document from the specified file path |
| 64 | + WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Sample.docx")); |
| 65 | + |
| 66 | + // Get the last paragraph in the document to append the IF field |
| 67 | + WParagraph paragraph = document.LastParagraph; |
| 68 | + |
| 69 | + // Define the variable value to be used in the IF condition |
| 70 | + string product = "cycle"; |
| 71 | + |
| 72 | + // Append an IF field to the paragraph |
| 73 | + WField field = (WField)paragraph.AppendField("If", FieldType.FieldIf); |
| 74 | + |
| 75 | + // Insert field code and rich content (tables and text) for true/false statements |
| 76 | + InsertIfFieldCode(paragraph, field, product); |
| 77 | + |
| 78 | + // Update all fields in the document |
| 79 | + document.UpdateDocumentFields(); |
| 80 | + |
| 81 | + // Return the generated Word document as a downloadable file |
| 82 | + return CreateFileResult(document, "IfFieldWithRichContent.docx"); |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Insert IF field code with complex true/false content (tables and text) |
| 87 | + /// </summary> |
| 88 | + private static void InsertIfFieldCode(WParagraph paragraph, WField field, string product) |
| 89 | + { |
| 90 | + // Get the index of the field in the paragraph |
| 91 | + int fieldIndex = paragraph.Items.IndexOf(field) + 1; |
| 92 | + |
| 93 | + // Define the IF field condition |
| 94 | + field.FieldCode = $"IF \"{product}\" = \"cycle\" "; |
| 95 | + |
| 96 | + // Move field separator and end marks to a temporary paragraph |
| 97 | + WParagraph lastPara = new WParagraph(paragraph.Document); |
| 98 | + MoveFieldMark(paragraph, fieldIndex + 1, lastPara); |
| 99 | + |
| 100 | + // Insert true statement (when condition is true) |
| 101 | + paragraph = InsertTrueStatement(paragraph); |
| 102 | + |
| 103 | + // Insert false statement (when condition is false) |
| 104 | + paragraph = InsertFalseStatement(paragraph); |
| 105 | + |
| 106 | + // Move remaining field marks back from temporary paragraph to the original |
| 107 | + MoveFieldMark(lastPara, 0, paragraph); |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Moves remaining field items to another paragraph |
| 112 | + /// </summary> |
| 113 | + private static void MoveFieldMark(WParagraph paragraph, int fieldIndex, WParagraph lastPara) |
| 114 | + { |
| 115 | + // Move all items after the field index to the destination paragraph |
| 116 | + for (int i = fieldIndex; i < paragraph.Items.Count;) |
| 117 | + lastPara.Items.Add(paragraph.Items[i]); |
| 118 | + } |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Insert the true part of the IF field with rich content (text + table) |
| 122 | + /// </summary> |
| 123 | + private static WParagraph InsertTrueStatement(WParagraph paragraph) |
| 124 | + { |
| 125 | + WTextBody ownerTextBody = paragraph.OwnerTextBody; |
| 126 | + |
| 127 | + // Append heading text for the true statement |
| 128 | + WTextRange text = (WTextRange)paragraph.AppendText("\"Product Overview"); |
| 129 | + text.CharacterFormat.Bold = true; |
| 130 | + paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center; |
| 131 | + |
| 132 | + // Create a table with product details |
| 133 | + WTable table = (WTable)ownerTextBody.AddTable(); |
| 134 | + |
| 135 | + // Add rows and cells to the table with data |
| 136 | + WTableRow row = table!.AddRow() as WTableRow; |
| 137 | + row.AddCell().AddParagraph().AppendText("Mountain-200"); |
| 138 | + row.AddCell().AddParagraph().AppendText("$2,294.99"); |
| 139 | + |
| 140 | + row = table.AddRow() as WTableRow; |
| 141 | + row.Cells[0].AddParagraph().AppendText("Mountain-300"); |
| 142 | + row.Cells[1].AddParagraph().AppendText("$1,079.99"); |
| 143 | + |
| 144 | + row = table.AddRow() as WTableRow; |
| 145 | + row.Cells[0].AddParagraph().AppendText("Road-150"); |
| 146 | + row.Cells[1].AddParagraph().AppendText("$3,578.27"); |
| 147 | + |
| 148 | + // Add a paragraph to close the true statement string |
| 149 | + WParagraph lastPara = (WParagraph)ownerTextBody.AddParagraph(); |
| 150 | + lastPara.AppendText("\" "); |
| 151 | + return lastPara; |
| 152 | + } |
| 153 | + |
| 154 | + /// <summary> |
| 155 | + /// Insert the false part of the IF field with rich content (text + table) |
| 156 | + /// </summary> |
| 157 | + private static WParagraph InsertFalseStatement(WParagraph paragraph) |
| 158 | + { |
| 159 | + WTextBody ownerTextBody = paragraph.OwnerTextBody; |
| 160 | + |
| 161 | + // Append heading text for the false statement |
| 162 | + WTextRange text = (WTextRange)paragraph.AppendText("\"Juice Corner"); |
| 163 | + text.CharacterFormat.Bold = true; |
| 164 | + paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center; |
| 165 | + |
| 166 | + // Create a table with juice product details |
| 167 | + WTable table = (WTable)ownerTextBody.AddTable(); |
| 168 | + |
| 169 | + // Add rows and cells to the table with data |
| 170 | + WTableRow row = table.AddRow() as WTableRow; |
| 171 | + row.AddCell().AddParagraph().AppendText("Apple Juice"); |
| 172 | + row.AddCell().AddParagraph().AppendText("$12.00"); |
| 173 | + |
| 174 | + row = table.AddRow() as WTableRow; |
| 175 | + row.Cells[0].AddParagraph().AppendText("Grape Juice"); |
| 176 | + row.Cells[1].AddParagraph().AppendText("$15.00"); |
| 177 | + |
| 178 | + row = table.AddRow() as WTableRow; |
| 179 | + row.Cells[0].AddParagraph().AppendText("Hot Soup"); |
| 180 | + row.Cells[1].AddParagraph().AppendText("$20.00"); |
| 181 | + |
| 182 | + row = table.AddRow() as WTableRow; |
| 183 | + row.Cells[0].AddParagraph().AppendText("Tender Coconut"); |
| 184 | + row.Cells[1].AddParagraph().AppendText("$20.00"); |
| 185 | + |
| 186 | + row = table.AddRow() as WTableRow; |
| 187 | + row.Cells[0].AddParagraph().AppendText("Cherry"); |
| 188 | + row.Cells[1].AddParagraph().AppendText("$25.00"); |
| 189 | + |
| 190 | + // Add a paragraph to close the false statement string |
| 191 | + WParagraph lastPara = (WParagraph)ownerTextBody.AddParagraph(); |
| 192 | + lastPara.AppendText("\" "); |
| 193 | + return lastPara; |
| 194 | + } |
| 195 | + |
| 196 | + private FileStreamResult CreateFileResult(WordDocument document, string fileName) |
| 197 | + { |
| 198 | + MemoryStream outputStream = new MemoryStream(); |
| 199 | + document.Save(outputStream, FormatType.Docx); |
| 200 | + outputStream.Position = 0; |
| 201 | + return File(outputStream, "application/docx", fileName); |
| 202 | + } |
| 203 | + |
| 204 | + public IActionResult Index() |
| 205 | + { |
| 206 | + return View(); |
| 207 | + } |
| 208 | + |
| 209 | + public IActionResult Privacy() |
| 210 | + { |
| 211 | + return View(); |
| 212 | + } |
| 213 | + |
| 214 | + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] |
| 215 | + public IActionResult Error() |
| 216 | + { |
| 217 | + return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); |
| 218 | + } |
| 219 | + } |
| 220 | +} |
0 commit comments