|
| 1 | +using Microsoft.AspNetCore.Mvc; |
| 2 | +using Syncfusion.DocIO; |
| 3 | +using Syncfusion.DocIO.DLS; |
| 4 | +using System.Diagnostics; |
| 5 | +using WordToText.Models; |
| 6 | + |
| 7 | +namespace WordToText.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 WordToText() |
| 19 | + { |
| 20 | + // Loads an existing Word document into DocIO instance |
| 21 | + FileStream fileStreamPath = new FileStream("Data\\Template.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); |
| 22 | + |
| 23 | + // Load the document into DocIO |
| 24 | + WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx); |
| 25 | + |
| 26 | + // Create an output memory stream to hold the document content |
| 27 | + MemoryStream outputStream = new MemoryStream(); |
| 28 | + |
| 29 | + // Save the new document to the output stream |
| 30 | + document.Save(outputStream, FormatType.Txt); |
| 31 | + |
| 32 | + // Close the document |
| 33 | + document.Close(); |
| 34 | + |
| 35 | + //Return the output file |
| 36 | + return File(outputStream, "application/txt", "WordToText.txt"); |
| 37 | + } |
| 38 | + |
| 39 | + public IActionResult TextToWord() |
| 40 | + { |
| 41 | + //Loads an existing Word document into DocIO instance |
| 42 | + FileStream fileStreamPath = new FileStream("Data\\Template.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); |
| 43 | + |
| 44 | + // Load the document into DocIO |
| 45 | + WordDocument document = new WordDocument(fileStreamPath, FormatType.Txt); |
| 46 | + |
| 47 | + //Create an output memory stream to hold the document content |
| 48 | + MemoryStream outputStream = new MemoryStream(); |
| 49 | + |
| 50 | + // Save the new document to the output stream |
| 51 | + document.Save(outputStream, FormatType.Docx); |
| 52 | + |
| 53 | + //Close the document |
| 54 | + document.Close(); |
| 55 | + |
| 56 | + //Return the output file |
| 57 | + return File(outputStream, "application/docx", "TextToWord.docx"); |
| 58 | + } |
| 59 | + |
| 60 | + public IActionResult ExtractPlainText() |
| 61 | + { |
| 62 | + //Loads an existing Word document into DocIO instance |
| 63 | + FileStream fileStreamPath = new FileStream("Data\\Template.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); |
| 64 | + |
| 65 | + // Load the document into DocIO |
| 66 | + WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx); |
| 67 | + |
| 68 | + //Get the plain text from the document |
| 69 | + string text = document.GetText(); |
| 70 | + |
| 71 | + //Create a new Word document to hold the extracted text |
| 72 | + WordDocument newdocument = new WordDocument(); |
| 73 | + |
| 74 | + //Adds new section |
| 75 | + IWSection section = newdocument.AddSection(); |
| 76 | + |
| 77 | + //Adds new paragraph |
| 78 | + IWParagraph paragraph = section.AddParagraph(); |
| 79 | + |
| 80 | + //Append the extracted text to the new document. |
| 81 | + paragraph.AppendText(text); |
| 82 | + |
| 83 | + //Create an output stream for the new document |
| 84 | + MemoryStream outputStream = new MemoryStream(); |
| 85 | + |
| 86 | + //Save the new document to the output stream |
| 87 | + newdocument.Save(outputStream, FormatType.Docx); |
| 88 | + |
| 89 | + //Close the document |
| 90 | + newdocument.Close(); |
| 91 | + |
| 92 | + //Return the output file |
| 93 | + return File(outputStream, "application/docx", "ExtractPlainText.docx"); |
| 94 | + } |
| 95 | + |
| 96 | + public IActionResult Index() |
| 97 | + { |
| 98 | + return View(); |
| 99 | + } |
| 100 | + |
| 101 | + public IActionResult Privacy() |
| 102 | + { |
| 103 | + return View(); |
| 104 | + } |
| 105 | + |
| 106 | + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] |
| 107 | + public IActionResult Error() |
| 108 | + { |
| 109 | + return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments