I'll leave my answer here to help the people who need it to convert. In my logic I'm 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 the server needs Microsoft Office installed.