If you want to convert a document to PDF you can save it as PDF (File -> Export -> PDF). But what if you have a lot of documents you want to convert to PDF. Then the Office Interop Assemblies could help you. This blog explains how you could use the Office Interop Assemblies to convert Word, PowerPoint and Excel files to PDF.
Prerequisites:
– Microsoft Office needs to be installed
– Some dev skills
This is not something you would like to use on a large production environment. But if you are in need of a onetime solution this could be a solution. Always keep in mind that you backup your Office documents and check the results after converting.
Word
Add Assembly reference to project: Microsoft.Office.Interop.Word
Code:
1 2 3 4 5 6 7 8 9 10 |
var wordApp = new Microsoft.Office.Interop.Word.Application(); var wordDocument = wordApp.Documents.Open(@"C:\WordDocument.docx"); wordDocument.ExportAsFixedFormat(@"C:\NewPDFFile.PDF", Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF); wordDocument.Close(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges, Microsoft.Office.Interop.Word.WdOriginalFormat.wdOriginalDocumentFormat, false); //Close document wordApp.Quit(); //Important: When you forget this Word keeps running in the background |
PowerPoint
Add Assembly reference to project: Microsoft.Office.Interop.PowerPoint
Code:
1 2 3 4 5 6 7 8 9 10 11 12 |
var powerpointApp = new Microsoft.Office.Interop.PowerPoint.Application(); var powerpointDocument = powerpointApp.Presentations.Open(@"C:\PowerPoint.pptx", Microsoft.Office.Core.MsoTriState.msoTrue, //ReadOnly Microsoft.Office.Core.MsoTriState.msoFalse, //Untitled Microsoft.Office.Core.MsoTriState.msoFalse); //Window not visible during converting powerpointDocument.ExportAsFixedFormat(@"C:\NewPDFFile.pdf", Microsoft.Office.Interop.PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF); powerpointDocument.Close(); //Close document powerpointApp.Quit(); //Important: When you forget this PowerPoint keeps running in the background |
Excel
Add assembly reference to project: Microsoft.Office.Interop.Excel
Code:
1 2 3 4 5 6 7 8 9 |
var excelApp = new Microsoft.Office.Interop.Excel.Application(); var excelDocument = excelApp.Workbooks.Open(@"C:\ExcelDocument.xlsx"); excelDocument.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, @"C:\NewPDFFile.pdf"); excelDocument.Close(false, "", false); //Close document excelApp.Quit(); //Important: When you forget this Excel keeps running in the background |
You know I am biased, but don’t build production systems based on this, you are in for a world of hurt.
Good for one-offs though, providing you keep the bulk small and keep a good eye on the results.
It’s indeed not something that you would like to use on a large scale production environment. But if you are in need of a onetime solution to convert a bulk of documents this could be a solution. And always make sure you test the results afterwards and make a backup of the original Office documents.
Thank you so much!!
You are a genius Stefan. It works like a charm. Thank you
useful, the c# code for converting word to pdf is what I need. thx