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

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

Rate This
  • Comments 17

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

http://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:

http://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();

        }

    }

}

Leave a Comment
  • Please add 3 and 8 and type the answer here:
  • Post
  • PingBack from http://www.easycoded.com/convert-office-documents-docx-pptx-pub-into-pdf-programmatically/

  • So I have to have the Office Suite installed on the server to use that dll.

  • hi

    Nice post!!

    i am trying to open a document from local drive.

    it works fine when i run on local machine .

    however, when i upload it to sharepoint server, the application does not work..

    it gives null object at this line

    wordapp.documents.open(..

    Please help me if you can.

    Thanks

    Sheetal

  • I got the answer!!!

    Just change the dcomconfig object properties for

    microsoft word.

    I changed the Identity to INTERACTIVE USER:)

    and it worked!!!

  • 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.

  • How to configure Office applications to run under the interactive user ...

    Start > Run > dcomcnfg

    Component Services > Computers > My Computer > DCOM Config

    Microsoft Office Word 97 - 2003 Document

    - right click: Properties

    - Security Tab

    - Customize the "Launch and Activation Permissions"

    - add Network Service

    - Customize the "Access Permissions"

    - add Network Service

    - Identity Tab

    - Set to "The interactive user"

  • 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.  

  • 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?

  • 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

  • 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!?

  • 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 DOCUMENT`S PAGES. IT`S USELESS FOR ME.

    PLEASE HELP ME IF YOU CAN.

    THANKS

  • please, show how to convert pdf to xps

  • Great work !

  • 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

  • can i have a copy of your program?

    tnx

Page 1 of 2 (17 items) 12