I’ll leave my answer here to help the people who need to convert you. In my logic I am converting (word, excel and Powerpoint) to PDF and returns bytes[]
:
Before going to logic, you need to install 3 components, they are:
PM> Install-Package Microsoft.Office.Interop.Word -Version 15.0.4797.1003
PM> Install-Package Microsoft.Office.Interop.Excel -Version 15.0.4795.1000
PM> Install-Package Microsoft.Office.Interop.Powerpoint -Version 15.0.4420.1017
HttpPostedFileBase file = Request.Files[0];
string ext = Path.GetExtension(file.FileName);
byte[] PDF = ConvertToPDF(file, ext);
private byte[] ConvertToPDF(HttpPostedFileBase file, string ext)
{
//salva o arquivo na pasta App_Data
string path = Server.MapPath($"~/App_Data/nome_arquivo");
file.SaveAs($"{path}{ext}");
//Micrososft Word
if (ext == ".doc" || ext == ".docx")
{
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = app.Documents.Open($"{path}{ext}");
//Converter para PDF
doc.ExportAsFixedFormat($"{path}.pdf", Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
doc.Close();
app.Quit();
//Leia o arquivo e retorna bytes[]
return System.IO.File.ReadAllBytes($"{path}.pdf");
}
//Microsoft Excel
if (ext == ".xls" || ext == ".xlsx")
{
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wkb = app.Workbooks.Open($"{path}{ext}");
//Converter para PDF
wkb.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, $"{path}.pdf");
wkb.Close();
app.Quit();
//Leia o arquivo e retorna bytes[]
return System.IO.File.ReadAllBytes($"{path}.pdf");
}
//Microsoft PowerPoint
else
{
//ppt || pptx
Microsoft.Office.Interop.PowerPoint.Application app = new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Interop.PowerPoint.Presentation presentation = app.Presentations.Open($"{path}{ext}",
Microsoft.Office.Core.MsoTriState.msoTrue,
Microsoft.Office.Core.MsoTriState.msoFalse,
Microsoft.Office.Core.MsoTriState.msoFalse);
//Converter para PDF
presentation.ExportAsFixedFormat($"{path}.pdf", Microsoft.Office.Interop.PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF);
presentation.Close();
app.Quit();
//Leia o arquivo e retorna bytes[]
return System.IO.File.ReadAllBytes($"{path}.pdf");
}
}
Remembering that on the server you need to Microsoft Office installed.
William on the last line, you forgot to close:
wordDocument.Close()
andappWord.Quit()
. Take the example of my answer :)– Matheus Miranda
Actually, I did it in a hurry and when it worked out I ended up not revising the code. This closure is very important so that files are not "locked open" .
– Guilherme Batista
How the Office interoperability library creates COM objects that are managed by the Trash Collector. NET, the Word process may be "hanging" indefinitely while the application is running, so it is important to force a collection of trash. Look at that answer: How to save an XLSX (Excel) file instead of CSV?.
– Pedro Gaspar