Convert Office Documents (.docx, .pptx, .pub) into PDF Programmatically

Office 2007 has an option to convert different types of Documents into PDF or XPS. You can download the add-in from this location:

https://www.microsoft.com/downloads/details.aspx?familyid=4D951911-3E7E-4AE6-B059-A2E79ED87041&displaylang=en

You can convert an existing word document or other office files to PDF or XPS by choosing Save As>PDF or XPS. We also can convert documents to PDF (or XPS) programmatically.

Here is a nice example given in MSDN to convert .docx to pdf:

https://msdn.microsoft.com/en-us/library/bb412305.aspx

I am adding the same example and additional codes to convert PowerPoint (.pptx) and Publisher (.pub) files to pdf. All these 3 examples are console applications.

For each of these go to Reference>Add Reference in Solution Explorer of VS 2008 and select COM tab. Add reference of Microsoft Word 12.0 Object Library in case of Word, Microsoft Publisher 12.0 Object Library in case of Publisher, and Microsoft PowerPoint 12.0 Object Library in case of PowerPoint.

Word

========

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Office.Interop.Word;

namespace DocConverter

{

    class Program

    {

        static void Main(string[] args)

        {

            ApplicationClass wordApplication = new ApplicationClass();

            Document wordDocument = null;

            object paramSourceDocPath = @"D:\Fun\Test.docx";

            object paramMissing = Type.Missing;

            string paramExportFilePath = @"D:\Fun\Test.pdf";

            WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;

            bool paramOpenAfterExport = false;

            WdExportOptimizeFor paramExportOptimizeFor =

                WdExportOptimizeFor.wdExportOptimizeForPrint;

            WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;

            int paramStartPage = 0;

            int paramEndPage = 0;

            WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;

            bool paramIncludeDocProps = true;

            bool paramKeepIRM = true;

            WdExportCreateBookmarks paramCreateBookmarks =

                WdExportCreateBookmarks.wdExportCreateWordBookmarks;

            bool paramDocStructureTags = true;

            bool paramBitmapMissingFonts = true;

            bool paramUseISO19005_1 = false;

            try

            {

                // Open the source document.

                wordDocument = wordApplication.Documents.Open(

                    ref paramSourceDocPath, ref paramMissing, ref paramMissing,

                    ref paramMissing, ref paramMissing, ref paramMissing,

                    ref paramMissing, ref paramMissing, ref paramMissing,

                    ref paramMissing, ref paramMissing, ref paramMissing,

                    ref paramMissing, ref paramMissing, ref paramMissing,

                    ref paramMissing);

                // Export it in the specified format.

                if (wordDocument != null)

                    wordDocument.ExportAsFixedFormat(paramExportFilePath,

                        paramExportFormat, paramOpenAfterExport,

                        paramExportOptimizeFor, paramExportRange, paramStartPage,

                        paramEndPage, paramExportItem, paramIncludeDocProps,

                        paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,

                        paramBitmapMissingFonts, paramUseISO19005_1,

                        ref paramMissing);

            }

            catch (Exception ex)

            {

                // Respond to the error

            }

            finally

            {

                // Close and release the Document object.

                if (wordDocument != null)

                {

                    wordDocument.Close(ref paramMissing, ref paramMissing,

                        ref paramMissing);

                    wordDocument = null;

                }

                // Quit Word and release the ApplicationClass object.

                if (wordApplication != null)

                {

                    wordApplication.Quit(ref paramMissing, ref paramMissing,

                        ref paramMissing);

                    wordApplication = null;

                }

                GC.Collect();

                GC.WaitForPendingFinalizers();

                GC.Collect();

                GC.WaitForPendingFinalizers();

            }

        }

    }

}

PowerPoint

=============

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Office.Interop.PowerPoint;

namespace PowerPointConverter

{

    class Program

    {

        static void Main(string[] args)

        {

            Microsoft.Office.Interop.PowerPoint.Application ppApp = new Microsoft.Office.Interop.PowerPoint.Application();

            Microsoft.Office.Interop.PowerPoint.Presentation presentation = ppApp.Presentations.Open(@"D:\Fun\Test.pptx", Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);

            presentation.ExportAsFixedFormat(@"D:\Fun\Test.xps",

                PpFixedFormatType.ppFixedFormatTypeXPS,

                PpFixedFormatIntent.ppFixedFormatIntentPrint,

                Microsoft.Office.Core.MsoTriState.msoFalse,

                PpPrintHandoutOrder.ppPrintHandoutHorizontalFirst,

                PpPrintOutputType.ppPrintOutputSlides,

                Microsoft.Office.Core.MsoTriState.msoFalse,

                null,

                PpPrintRangeType.ppPrintAll,

                "",

                false,

                false,

                false,

                true,

                true,

                System.Reflection.Missing.Value);

                presentation.Close();

                presentation = null;

                ppApp = null;

                GC.Collect();

        }

    }

}

Publisher
===============

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Office.Interop.Publisher;

using System.Reflection;

namespace PublisherConverter

{

    class Program

    {

        static void Main(string[] args)

        {

            Microsoft.Office.Interop.Publisher.Application pbApp = new Microsoft.Office.Interop.Publisher.Application();

            Microsoft.Office.Interop.Publisher.Document pbDoc = pbApp.Open(@"D:\Fun\Test4.pub", true, true, PbSaveOptions.pbDoNotSaveChanges);

            pbDoc.ExportAsFixedFormat(PbFixedFormatType.pbFixedFormatTypePDF, @"D:\Fun\Test.pdf", PbFixedFormatIntent.pbIntentPrinting, true, 300, 450, 1200, 1800, 1, pbDoc.Pages.Count, 1, false, PbPrintStyle.pbPrintStyleDefault, false, false, false, System.Reflection.Missing.Value);

            pbDoc.Close();

            pbDoc = null;

            pbApp = null;

            GC.Collect();

        }

    }

}

Comments

  • Anonymous
    September 24, 2008
    PingBack from http://www.easycoded.com/convert-office-documents-docx-pptx-pub-into-pdf-programmatically/

  • Anonymous
    September 26, 2008
    So I have to have the Office Suite installed on the server to use that dll.

  • Anonymous
    October 13, 2008
    The comment has been removed

  • Anonymous
    October 14, 2008
    I got the answer!!! Just change the dcomconfig object properties for microsoft word. I changed the Identity to INTERACTIVE USER:) and it worked!!!

  • Anonymous
    October 22, 2008
    I used this code with vs.net2.0 and office2003. I am using word 11.0 reference .I am getting error "The type or namespace name 'WdExportFormat' could not be found (are you missing a using directive or an assembly reference?)" I added namespace Microsoft.office,interop.word. now what to do.

  • Anonymous
    March 10, 2009
    The comment has been removed

  • Anonymous
    August 11, 2009
    Hi, I found this article is good and great. But I am looking a solution for Slide library. I need to upload the PPT file in Sharepoint slide library through Code only. Do you have any idea, how to do this work.  

  • Anonymous
    August 18, 2009
    When I try to do the PowerPoint conversion I received a "The type or namespace name 'Core' does not exist in the namespace 'Microsoft.Office' message.  I have Microsoft.Office.Interop.PowerPoint referenced properly.  Can you please assist me?

  • Anonymous
    August 24, 2009
    Hi, I was googling for a solution about printing filtered views in sharepoint 2007 when i found you're post. Do you have any ideas how to do that? Regards

  • Anonymous
    September 07, 2009
    Hi, great article! But I have also problems with PowerPoint!? Has someone a resolution for it? I get Access Denied Errors when trying to open the Application Class!?

  • Anonymous
    April 26, 2010
    HI, IS THERE ANY POSSIBLE WAY TO TAKE HTML CODE FROM PUBLISHER DOCUMENT. ExportAsFixedFormat METHOD ONLY EXPORTS TO PDF AND XPS AND ExportEmailHTML METHOD ONLY WORKS FOR PUBLISHER DOCUMENTS PAGES. ITS USELESS FOR ME. PLEASE HELP ME IF YOU CAN. THANKS

  • Anonymous
    June 30, 2010
    please, show how to convert pdf to xps

  • Anonymous
    January 03, 2011
    Great work !

  • Anonymous
    August 21, 2011
    Hi, Nice Post, I am trying to run this on my SharePoint server which doesnot have Office installed.Is there a way to overcome this problem without having to install Office on the server.....Please help me out Thanks

  • Anonymous
    August 23, 2011
    can i have a copy of your program? tnx

  • Anonymous
    February 06, 2012
    Hi. Tank You.Very Good. انا لانضیع اجر من احسن عملا

  • Anonymous
    September 09, 2012
    Thank u so much..... !!

  • Anonymous
    July 07, 2016
    Thank you!!!! Works like a charm for me!!!This is what i was looking for about 5 ours in internet :D