|
| 1 | +using Convert_PDF_to_Word.Models; |
| 2 | +using Microsoft.AspNetCore.Mvc; |
| 3 | +using System.Diagnostics; |
| 4 | +using Syncfusion.DocIO; |
| 5 | +using Syncfusion.DocIO.DLS; |
| 6 | +using Syncfusion.SmartDataExtractor; |
| 7 | + |
| 8 | +namespace Convert_PDF_to_Word.Controllers |
| 9 | +{ |
| 10 | + public class HomeController : Controller |
| 11 | + { |
| 12 | + public IActionResult Index() |
| 13 | + { |
| 14 | + return View(); |
| 15 | + } |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Converts an entire PDF document to a Word document. |
| 19 | + /// </summary> |
| 20 | + public IActionResult ConvertPDFtoWord() |
| 21 | + { |
| 22 | + // Open the input PDF document. |
| 23 | + using FileStream stream = new FileStream("Data/Input-file.pdf", FileMode.Open, FileAccess.Read); |
| 24 | + |
| 25 | + // Create a DataExtractor instance. |
| 26 | + DataExtractor extractor = new DataExtractor(); |
| 27 | + |
| 28 | + // Extract PDF content as a Word document. |
| 29 | + WordDocument document = extractor.ExtractDataAsWordDocument(stream); |
| 30 | + |
| 31 | + // Create a memory stream for the output document. |
| 32 | + MemoryStream outputStream = new MemoryStream(); |
| 33 | + |
| 34 | + // Save the Word document in DOCX format. |
| 35 | + document.Save(outputStream, FormatType.Docx); |
| 36 | + |
| 37 | + // Close the Word document. |
| 38 | + document.Close(); |
| 39 | + |
| 40 | + // Reset the stream position. |
| 41 | + outputStream.Position = 0; |
| 42 | + |
| 43 | + // Return the generated DOCX file. |
| 44 | + return File( |
| 45 | + outputStream, |
| 46 | + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", |
| 47 | + "Output.docx"); |
| 48 | + } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Converts selected PDF pages to Word. |
| 52 | + /// </summary> |
| 53 | + public IActionResult PDFRangetoWord() |
| 54 | + { |
| 55 | + // Open the input PDF document. |
| 56 | + using FileStream stream = new FileStream("Data/Input-file.pdf", FileMode.Open, FileAccess.Read); |
| 57 | + |
| 58 | + // Create a DataExtractor instance. |
| 59 | + DataExtractor extractor = new DataExtractor(); |
| 60 | + |
| 61 | + // Specify the page range to convert. |
| 62 | + extractor.PageRange = new int[,] { { 1, 2 } }; |
| 63 | + |
| 64 | + // Extract PDF content as a Word document. |
| 65 | + WordDocument document = extractor.ExtractDataAsWordDocument(stream); |
| 66 | + |
| 67 | + // Create a memory stream for the output document. |
| 68 | + MemoryStream outputStream = new MemoryStream(); |
| 69 | + |
| 70 | + // Save the Word document in DOCX format. |
| 71 | + document.Save(outputStream, FormatType.Docx); |
| 72 | + |
| 73 | + // Close the Word document. |
| 74 | + document.Close(); |
| 75 | + |
| 76 | + // Reset the stream position. |
| 77 | + outputStream.Position = 0; |
| 78 | + |
| 79 | + // Return the generated DOCX file. |
| 80 | + return File( |
| 81 | + outputStream, |
| 82 | + "application/vnd.openxmlformats-officedocument.wordprocessingml.document", |
| 83 | + "Output.docx"); |
| 84 | + } |
| 85 | + |
| 86 | + public IActionResult Privacy() |
| 87 | + { |
| 88 | + return View(); |
| 89 | + } |
| 90 | + |
| 91 | + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] |
| 92 | + public IActionResult Error() |
| 93 | + { |
| 94 | + return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments