How to export Word 2007 document to PDF and/or XPS?

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

If you are sick of exporting of PDF by using of Print driver you can try this solution. The sample below shows how to export word 2007 document to PDF and.or XPS.
Please note that prerequisites for this sample are 2007 Microsoft Office Add-in: Microsoft Save as PDF and 2007 Microsoft Office Add-in: Microsoft Save as XPS or booth in once: http://www.microsoft.com/downloads/thankyou.aspx?familyId=4d951911-3e7e-4ae6-b059-a2e79ed87041&displayLang=en (installe Office 2007 and package behind this link before you start with the sample below).
The sample shown below is based on Word Automation (not Open XML!) and makes  usage of Microsoft Word 12.0 Object Library to access the Word 2007 objects.
Export is based on method Document.ExportAsFixedFormat method and enumeration WdExportFormat.

 

/// <summary>
///
Exparts word document to PDF or XPS.
/// </summary>
private static void exportWordDoc2Pdf()
{
            Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();

            object fileName =@"Quick Guide.docx";
            object falseValue = false;
            object trueValue = true;
            object missing = Type.Missing;
            word.Visible = true;

            //
            // Loads an existing word document.
            Microsoft.Office.Interop.Word.Document doc =
            word.Documents.Open(ref fileName,
            ref missing, ref trueValue, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing);


            // Sets export format. Another possiblity: wdExportFormatXPS
            Microsoft.Office.Interop.Word.WdExportFormat paramExportFormat =
                 
            Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
            bool paramOpenAfterExport = false;
            Microsoft.Office.Interop.Word.WdExportOptimizeFor
            paramExportOptimizeFor =  
           

        Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint
        Microsoft.Office.Interop.Word.WdExportRange paramExportRange =
                 
         Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument;
            int paramStartPage = 0;
            int paramEndPage = 0;
            Microsoft.Office.Interop.Word.WdExportItem paramExportItem =
            
         Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentContent;
            bool paramIncludeDocProps = true;
            bool paramKeepIRM = true;
            Microsoft.Office.Interop.Word.WdExportCreateBookmarks
           
paramCreateBookmarks =  
           Microsoft.Office.Interop.Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
            bool paramDocStructureTags = true;
            bool paramBitmapMissingFonts = true;
            bool paramUseISO19005_1 = false;
            string newFileName = "c:\\Temp\\test.pdf";

            //
            // Export the file to PDF.
            doc.ExportAsFixedFormat(newFileName,
          paramExportFormat, paramOpenAfterExport,
          paramExportOptimizeFor, paramExportRange, paramStartPage,
          paramEndPage, paramExportItem, paramIncludeDocProps,
          paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
          paramBitmapMissingFonts, paramUseISO19005_1,
          ref missing);

            doc.Close(ref missing, ref missing, ref missing);

            doc = null;

            word.Quit(ref missing, ref missing, ref missing);

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
}

 

For more information about this take a look on this MSDN article: http://msdn.microsoft.com/en-us/library/bb412305.aspx


Posted Jul 12 2009, 07:36 PM by Damir Dobric

Comments

Amit wrote re: How to export Word 2007 document to PDF and/or XPS?
on 11-19-2009 18:11

Hello friend nice work

I just want to ask if this code works only with word2007?

Thanks

Amit is a friend wrote re: How to export Word 2007 document to PDF and/or XPS?
on 12-20-2009 1:12

yes it only works with word 2007 as you cam tell from "The sample shown below is based on Word Automation (not Open XML!) and makes  usage of Microsoft Word 12.0 Object Library to access the Word 2007 objects."

JEFF PAYETTE wrote re: How to export Word 2007 document to PDF and/or XPS?
on 06-21-2010 16:11

I tried your code in my wcf, it work great in debug mode but it doesn't work in live mode. Probably a security reason with iis7, Do you have any hint for me?

Mickey wrote re: How to export Word 2007 document to PDF and/or XPS?
on 10-03-2011 20:04

Wonderful explanation of facts aavilbale here.

Fahrie wrote re: How to export Word 2007 document to PDF and/or XPS?
on 10-30-2012 16:03

So ecexitd I found this article as it made things much quicker!

developers.de is a .Net Community Blog powered by daenet GmbH.