Welcome to MSDN Blogs Sign in | Join | Help

Pranab Paul's Blog - Development Tips on SharePoint, Office and Web

------------------------------------------Web Parts, Workflow, InfoPath Form Services, Features, Site Definition, Event Receivers, Excel Services, Business Data Catalog (BDC), Search

News

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:

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

        }

    }

}

Posted: Wednesday, September 24, 2008 4:37 PM by pranab

Comments

Al said:

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

# September 27, 2008 1:40 AM

Sheetal said:

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

# October 13, 2008 9:23 AM

Sheetal said:

I got the answer!!!

Just change the dcomconfig object properties for

microsoft word.

I changed the Identity to INTERACTIVE USER:)

and it worked!!!

# October 15, 2008 3:42 AM

gajendra said:

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.

# October 22, 2008 8:12 AM

Shamon Larson said:

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"

# March 10, 2009 10:20 AM

Manpreet said:

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.  

# August 11, 2009 3:50 PM

Mark said:

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?

# August 18, 2009 3:02 PM

Jasmim said:

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

# August 24, 2009 12:54 PM

nadine said:

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

# September 7, 2009 5:09 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker