Converting Office Documents to PDF with the Office Interop Assemblies

Office, SharePoint 2010, SharePoint 2013, Office Interop Assemblies, PDF Conversion, SharePoint 2010, SharePoint 2013

image

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:

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:

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:

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

 

VeryDOC has "DOC to Any Converter Command Line" and "DOC to Any Converter SDK/COM" products, these products are support Office Interop Assemblies and other technologies, you can use "DOC to Any Converter" Command Line or SDK/COM to convert office files to PDF files with or without MS Office installed in your system,

https://www.verydoc.com/doc-to-any.html

You can call "DOC to Any Converter Command Line" or "DOC to Any Converter SDK/COM" product to convert office files to PDF files from C#, PHP, ASP, ASP.NET, Javascript, VBScript, C++, VB, Delphi, FoxPro, Java, etc. program languages,

https://www.verydoc.com/blog/verydoc-doc-to-any-converter-command-line-v3-5-release-notes-convert-word-to-pdf-without-office-and-openoffice.html

https://www.verydoc.com/blog/verydoc-release-notes-verydoc-releases-an-exe-com-of-verypdfcomruncmd-exe-today-verypdf-exe-com-does-allow-you-to-call-ms-office-and-any-exe-application-from-asp-php-c-net-etc-program-languag.html

https://www.verydoc.com/blog/how-to-call-doc2any-exe-from-php-source-code.html

VN:F [1.9.20_1166]
Rating: 5.5/10 (2 votes cast)
VN:F [1.9.20_1166]
Rating: +1 (from 1 vote)
Converting Office Documents to PDF with the Office Interop Assemblies, 5.5 out of 10 based on 2 ratings

Related Posts

One Reply to “Converting Office Documents to PDF with the Office Interop Assemblies”

  1. I want to set Slide Size while converting PPT file to PDF using below syntax:
    powerpointDocument.PageSetup.SlideSize = PpSlideSizeType.ppSlideSizeA4Paper;

    But the moment debugger hits the above line, exception is thrown as “PageSetup (unknown member) : Failed.”
    Any clue what is causing this?

    [Reply]

    VA:F [1.9.20_1166]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.20_1166]
    Rating: 0 (from 0 votes)

Leave a Reply

Your email address will not be published. Required fields are marked *


Verify Code   If you cannot see the CheckCode image,please refresh the page again!