|
| 1 | +using System.Diagnostics; |
| 2 | +using Fields.Models; |
| 3 | +using Microsoft.AspNetCore.Mvc; |
| 4 | +using Syncfusion.DocIO; |
| 5 | +using Syncfusion.DocIO.DLS; |
| 6 | + |
| 7 | +namespace Fields.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 AddField() |
| 19 | + { |
| 20 | + using (FileStream fileStream = new FileStream("Data\\Template.docx", FileMode.Open, FileAccess.Read)) |
| 21 | + { |
| 22 | + WordDocument document = new WordDocument(fileStream, FormatType.Automatic); |
| 23 | + |
| 24 | + foreach (WSection section in document.Sections) |
| 25 | + { |
| 26 | + WParagraph footerParagraph = (WParagraph)section.HeadersFooters.Footer.AddParagraph(); |
| 27 | + |
| 28 | + footerParagraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center; |
| 29 | + |
| 30 | + footerParagraph.AppendText("Page "); |
| 31 | + footerParagraph.AppendField("Page", FieldType.FieldPage); |
| 32 | + |
| 33 | + footerParagraph.AppendText(" of "); |
| 34 | + footerParagraph.AppendField("NumPages", FieldType.FieldNumPages); |
| 35 | + } |
| 36 | + |
| 37 | + return CreateFileResult(document, "AddField.docx"); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public IActionResult UpdateField() |
| 42 | + { |
| 43 | + using (FileStream fileStream = new FileStream("Data\\Input.docx", FileMode.Open, FileAccess.Read)) |
| 44 | + { |
| 45 | + WordDocument document = new WordDocument(fileStream, FormatType.Automatic); |
| 46 | + |
| 47 | + document.UpdateDocumentFields(); |
| 48 | + |
| 49 | + return CreateFileResult(document, "UpdateField.docx"); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + public IActionResult UnlinkField() |
| 54 | + { |
| 55 | + using (FileStream fileStream = new FileStream("Data\\InputTemplate.docx", FileMode.Open, FileAccess.Read)) |
| 56 | + { |
| 57 | + WordDocument document = new WordDocument(fileStream, FormatType.Automatic); |
| 58 | + |
| 59 | + foreach (WSection section in document.Sections) |
| 60 | + { |
| 61 | + foreach (WParagraph paragraph in section.Body.Paragraphs) |
| 62 | + { |
| 63 | + for (int i = 0; i < paragraph.Items.Count; i++) |
| 64 | + { |
| 65 | + if (paragraph.Items[i] is WField field) |
| 66 | + { |
| 67 | + if (field.FieldType == FieldType.FieldDate) |
| 68 | + { |
| 69 | + field.Unlink(); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return CreateFileResult(document, "UnlinkField.docx"); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private FileStreamResult CreateFileResult(WordDocument document, string fileName) |
| 81 | + { |
| 82 | + MemoryStream outputStream = new MemoryStream(); |
| 83 | + document.Save(outputStream, FormatType.Docx); |
| 84 | + outputStream.Position = 0; |
| 85 | + return File(outputStream, "application/docx", fileName); |
| 86 | + } |
| 87 | + |
| 88 | + public IActionResult Index() |
| 89 | + { |
| 90 | + return View(); |
| 91 | + } |
| 92 | + |
| 93 | + public IActionResult Privacy() |
| 94 | + { |
| 95 | + return View(); |
| 96 | + } |
| 97 | + |
| 98 | + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] |
| 99 | + public IActionResult Error() |
| 100 | + { |
| 101 | + return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments