MSWord_SaveAsPDF

Microsoft provides add-ins to the Office products to support saving into PDF or XPS file formats. These add-ins include an API to perform this as well.

The add-ins may be found at:
Save as PDF: http://www.microsoft.com/downloads/details.aspx?FamilyId=F1FC413C-6D89-4F15-991B-63B07BA5F2E5&displaylang=en
Save as XPS: http://www.microsoft.com/downloads/details.aspx?FamilyId=BCE8F991-F0A4-47A0-866B-2FD84A329E02&displaylang=en
Save as PDF or XPS (single package): http://www.microsoft.com/downloads/details.aspx?FamilyId=4D951911-3E7E-4AE6-B059-A2E79ED87041&displaylang=en

Once installed, add a reference to the Microsoft.Office.Interop.[product]
Note: multiple product versions are available as references: version 12 = Office 2007, version 11 = Office 2003
Language:
C#
Keywords:
Office
Code Snippet

using System;
using System.IO;
using MSWord = Microsoft.Office.Interop.Word;

/***
* must install: addin for SaveAsPDF for Office 2007
* works with .doc and .docx files
* must add COM references for Word
***/

public
void MakeWordPDF(string SourceDoc, string DestDoc)
{
    //for all missing types.
    object Unknown = Type.Missing;

    MSWord.
Application newApp = new Microsoft.Office.Interop.Word.Application();

    object
src_doc = SourceDoc;
    object des_doc = DestDoc;

    //open the doc
    MSWord.Document newdoc =
        
newApp.Documents.Open(
ref src_doc, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
                              ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
                              ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);

    //specify the format you need.
    //     format = MSWord.WdSaveFormat.wdFormatDocument97;

    //     format = MSWord.WdSaveFormat.wdFormatHTML;
    //     format = MSWord.WdSaveFormat.wdFormatXPS;
    object
format = MSWord.WdSaveFormat
.wdFormatPDF;

    //save as .pdf
    newdoc.SaveAs(ref des_doc, ref format, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
                  ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
                  ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);

    newApp.Quit(
ref Unknown, ref Unknown, ref Unknown);
}

 


Created 2012-02-11
comments powered by Disqus
Login