How can I convert a byte[] of an Office document (.doc, .docx, .xlsx, .pptx) to a byte[] of a PDF document assuming Office is installed and Microsoft.Office.Interop is used?
I fetch the files' byteArray from the database as well as their name. I would like to first convert each file to a PDF and then combine all of the PDFs to one single PDF using PDFSharp (this part is already implemented).
Code:
foreach (Entity en in res.Entities)
{
byte[] fileByteArray = Convert.FromBase64String(en.GetAttributeValue<string>("documentbody"));
string fileName = en.GetAttributeValue<string>("filename");
string extension = fileName.Split('.')[1];
switch(extension)
{
case "doc":
case "docx":
byteArr.Add(ConvertWordToPdf(fileName, fileByteArray)); break;
case "xlsx":
byteArr.Add(ConvertExcelToPdf(fileName, fileByteArray)); break;
}
}
The problem is I'm not too sure how to implement these two methods. I tried using the following code:
private byte[] ConvertWordToPdf(string fileName, byte[] fileByteArray)
{
string tmpFile = Path.GetTempFileName();
File.WriteAllBytes(tmpFile, fileByteArray);
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
Document doc = app.Documents.Open(tmpFile);
// Save Word doc into a PDF
string pdfPath = fileName.Split('.')[0] + ".pdf";
doc.SaveAs2(pdfPath, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF);
doc.Close();
app.Quit();
byte[] pdfFileBytes = File.ReadAllBytes(pdfPath);
File.Delete(tmpFile);
return pdfFileBytes;
}
But it saves the file to disk and that's something I would like to avoid. Is doing the same operation without saving to disk possible?