|
| 1 | +using System.Diagnostics; |
| 2 | +using Microsoft.AspNetCore.Mvc; |
| 3 | +using WordToRTF.Models; |
| 4 | +using Syncfusion.DocIO; |
| 5 | +using Syncfusion.DocIO.DLS; |
| 6 | + |
| 7 | +namespace WordToRTF.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 WordToRTF() |
| 19 | + { |
| 20 | + // Open the Word document as a file stream |
| 21 | + FileStream fileStream = new FileStream("Data\\Template.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); |
| 22 | + |
| 23 | + // Load the stream into a DocIO WordDocument instance |
| 24 | + WordDocument document = new WordDocument(fileStream, FormatType.Docx); |
| 25 | + |
| 26 | + // Create a memory stream to store the converted RTF content |
| 27 | + MemoryStream outputStream = new MemoryStream(); |
| 28 | + |
| 29 | + // Save the document in RTF format |
| 30 | + document.Save(outputStream, FormatType.Rtf); |
| 31 | + |
| 32 | + // Close the document to release resources |
| 33 | + document.Close(); |
| 34 | + |
| 35 | + // Return the downloadable file |
| 36 | + return File(outputStream, "application/rtf", "WordToRTF.rtf"); |
| 37 | + } |
| 38 | + |
| 39 | + public IActionResult RTFToWord() |
| 40 | + { |
| 41 | + // Open the RTF file as a file stream |
| 42 | + FileStream fileStream = new FileStream("Data\\Input.rtf", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); |
| 43 | + |
| 44 | + // Load the stream into a DocIO WordDocument instance |
| 45 | + WordDocument document = new WordDocument(fileStream, FormatType.Rtf); |
| 46 | + |
| 47 | + // Create a memory stream to store the converted Word content |
| 48 | + MemoryStream outputStream = new MemoryStream(); |
| 49 | + |
| 50 | + // Save the document in DOCX format |
| 51 | + document.Save(outputStream, FormatType.Docx); |
| 52 | + |
| 53 | + // Close the document to release resources |
| 54 | + document.Close(); |
| 55 | + |
| 56 | + // Return the downloadable file |
| 57 | + return File(outputStream, "application/docx", "RTFToWord.docx"); |
| 58 | + } |
| 59 | + |
| 60 | + public IActionResult Index() |
| 61 | + { |
| 62 | + return View(); |
| 63 | + } |
| 64 | + |
| 65 | + public IActionResult Privacy() |
| 66 | + { |
| 67 | + return View(); |
| 68 | + } |
| 69 | + |
| 70 | + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] |
| 71 | + public IActionResult Error() |
| 72 | + { |
| 73 | + return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments