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();

        }

    }

}