@@ -19,7 +19,7 @@ public HomeController(ILogger<HomeController> logger)
1919 {
2020 _logger = logger ;
2121 }
22- public IActionResult WordToPDF ( string button )
22+ public IActionResult OfficeToPDF ( string button )
2323 {
2424 if ( button == null )
2525 return View ( "Index" ) ;
@@ -35,113 +35,101 @@ public IActionResult WordToPDF(string button)
3535 string extension = Path . GetExtension ( Request . Form . Files [ 0 ] . FileName ) . ToLower ( ) ;
3636 string fileName = Path . GetFileNameWithoutExtension ( Request . Form . Files [ 0 ] . FileName ) ;
3737
38- // Compares extension with supported extensions.
39- if ( extension == ".doc" || extension == ".docx" || extension == ".dot" || extension == ".dotx" || extension == ".dotm" || extension == ".docm"
40- || extension == ".xml" || extension == ".rtf" )
38+ try
4139 {
42- try
40+ PdfDocument pdfDocument = null ;
41+ // Switch on file extension to determine conversion method
42+ switch ( extension . ToLower ( ) )
4343 {
44- MemoryStream outputStream = new MemoryStream ( ) ;
45- //Open the Word document file stream.
46- using ( MemoryStream inputStream = new MemoryStream ( ) )
47- {
48- Request . Form . Files [ 0 ] . CopyTo ( inputStream ) ;
49- inputStream . Position = 0 ;
50- //Loads an existing Word document.
51- using ( WordDocument wordDocument = new WordDocument ( ) )
44+ // Word document formats
45+ case ".doc" :
46+ case ".docx" :
47+ case ".dot" :
48+ case ".dotx" :
49+ case ".dotm" :
50+ case ".docm" :
51+ case ".xml" :
52+ case ".rtf" :
53+ using ( MemoryStream inputStream = new MemoryStream ( ) )
5254 {
53- wordDocument . Open ( inputStream , Syncfusion . DocIO . FormatType . Automatic ) ;
54- //Creates an instance of DocIORenderer.
55- using ( DocIORenderer renderer = new DocIORenderer ( ) )
55+ // Copy uploaded file to memory stream
56+ Request . Form . Files [ 0 ] . CopyTo ( inputStream ) ;
57+ inputStream . Position = 0 ;
58+ // Open and load the Word document
59+ using ( WordDocument wordDocument = new WordDocument ( ) )
5660 {
57- //Converts Word document into PDF document.
58- using ( PdfDocument pdfDocument = renderer . ConvertToPDF ( wordDocument ) )
61+ // Convert Word document to PDF format
62+ wordDocument . Open ( inputStream , Syncfusion . DocIO . FormatType . Automatic ) ;
63+ using ( DocIORenderer renderer = new DocIORenderer ( ) )
5964 {
60- pdfDocument . Save ( outputStream ) ;
65+ pdfDocument = renderer . ConvertToPDF ( wordDocument ) ;
6166 }
6267 }
6368 }
64- }
65- outputStream . Position = 0 ;
66- return File ( outputStream , "application/pdf" , fileName + ".pdf" ) ;
67- }
68- catch ( Exception ex )
69- {
70- ViewBag . Message = string . Format ( ex . Message ) ;
71- //ViewBag.Message = string.Format("The input document could not be processed completely, Could you please email the document to support@syncfusion.com for troubleshooting.");
72- }
73- }
74- else if ( extension == ".xlsx" )
75- {
76- try
77- {
78- using ( MemoryStream inputStream = new MemoryStream ( ) )
79- {
80- Request . Form . Files [ 0 ] . CopyTo ( inputStream ) ;
81- inputStream . Position = 0 ;
82- //Loads an existing Excel document.
83- using ( ExcelEngine excelEngine = new ExcelEngine ( ) )
69+ break ;
70+ // Excel format
71+ case ".xlsx" :
72+ case ".xls" :
73+ case ".xltx" :
74+ case ".xlsm" :
75+ case ".csv" :
76+ case ".xlsb" :
77+ case ".xltm" :
78+ using ( MemoryStream inputStream = new MemoryStream ( ) )
8479 {
85- IApplication application = excelEngine . Excel ;
86- application . DefaultVersion = ExcelVersion . Xlsx ;
87- //Open the Excel document file stream.
88- IWorkbook workbook = application . Workbooks . Open ( inputStream ) ;
89- //Initialize XlsIO renderer.
90- XlsIORenderer renderer = new XlsIORenderer ( ) ;
91- //Convert Excel document into PDF document
92- PdfDocument pdfDocument = renderer . ConvertToPDF ( workbook ) ;
93- //Create the MemoryStream to save the converted PDF.
94- MemoryStream pdfStream = new MemoryStream ( ) ;
95- //Save the converted PDF document to MemoryStream.
96- pdfDocument . Save ( pdfStream ) ;
97- pdfStream . Position = 0 ;
98-
99- //Download PDF document in the browser.
100- return File ( pdfStream , "application/pdf" , "Sample.pdf" ) ;
80+ // Copy uploaded file to memory stream
81+ Request . Form . Files [ 0 ] . CopyTo ( inputStream ) ;
82+ inputStream . Position = 0 ;
83+ // Create Excel engine and load workbook
84+ using ( ExcelEngine excelEngine = new ExcelEngine ( ) )
85+ {
86+ IApplication application = excelEngine . Excel ;
87+ application . DefaultVersion = ExcelVersion . Xlsx ;
88+ IWorkbook workbook = application . Workbooks . Open ( inputStream ) ;
89+ // Convert Excel workbook to PDF format
90+ XlsIORenderer renderer = new XlsIORenderer ( ) ;
91+ pdfDocument = renderer . ConvertToPDF ( workbook ) ;
92+ }
10193 }
102- }
103-
104- }
105- catch ( Exception ex )
106- {
107- ViewBag . Message = string . Format ( ex . Message ) ;
108- }
109-
110- }
111- else if ( extension == ".pptx" )
112- {
113- try
114- {
115- using ( MemoryStream inputStream = new MemoryStream ( ) )
116- {
117- Request . Form . Files [ 0 ] . CopyTo ( inputStream ) ;
118- inputStream . Position = 0 ;
119- //Open the existing PowerPoint presentation with loaded stream.
120- using ( IPresentation pptxDoc = Presentation . Open ( inputStream ) )
94+ break ;
95+ // PowerPoint format
96+ case ".pptx" :
97+ using ( MemoryStream inputStream = new MemoryStream ( ) )
12198 {
122- //Convert the PowerPoint presentation to PDF document.
123- using ( PdfDocument pdfDocument = PresentationToPdfConverter . Convert ( pptxDoc ) )
99+ // Copy uploaded file to memory stream
100+ Request . Form . Files [ 0 ] . CopyTo ( inputStream ) ;
101+ inputStream . Position = 0 ;
102+ // Open PowerPoint presentation and convert to PDF
103+ using ( IPresentation pptxDoc = Presentation . Open ( inputStream ) )
124104 {
125- //Create the MemoryStream to save the converted PDF.
126- MemoryStream pdfStream = new MemoryStream ( ) ;
127- //Save the converted PDF document to MemoryStream.
128- pdfDocument . Save ( pdfStream ) ;
129- pdfStream . Position = 0 ;
130- //Download PDF document in the browser.
131- return File ( pdfStream , "application/pdf" , "Sample.pdf" ) ;
105+ pdfDocument = PresentationToPdfConverter . Convert ( pptxDoc ) ;
132106 }
133107 }
134- }
108+ break ;
109+ // Invalid file format
110+ default :
111+ ViewBag . Message = "Please choose Word, Excel or PowerPoint document to convert to PDF" ;
112+ return null ;
135113 }
136- catch ( Exception ex )
114+ // Save converted PDF and return as downloadable file
115+ if ( pdfDocument != null )
137116 {
138- ViewBag . Message = string . Format ( ex . Message ) ;
117+ using ( pdfDocument )
118+ {
119+ // Create memory stream to hold the PDF data
120+ MemoryStream pdfStream = new MemoryStream ( ) ;
121+ // Save the converted PDF to memory stream
122+ pdfDocument . Save ( pdfStream ) ;
123+ // Reset stream position to beginning for reading
124+ pdfStream . Position = 0 ;
125+ // Return PDF as downloadable file to browser
126+ return File ( pdfStream , "application/pdf" , fileName + ".pdf" ) ;
127+ }
139128 }
140-
141129 }
142- else
130+ catch ( Exception ex )
143131 {
144- ViewBag . Message = string . Format ( "Please choose Word, Excel or PowerPoint document to convert to PDF" ) ;
132+ ViewBag . Message = string . Format ( ex . Message ) ;
145133 }
146134 }
147135 else
0 commit comments