word-->pdf

下载SaveAsPDF.exe 并安装

To programmatically save a Word 2007 document to either the PDF format or the XPS format

  1. Add a reference to the Word 12.0 Object Library.

  2. Import the Word 2007 interop assembly namespace.

  3. Create an instance of the ApplicationClass object.

  4. Declare the appropriate variables

  5. Implement the conversion code.

1. Add a Reference to the Word 12.0 Object Library

First, add a reference to the Microsoft Word 12.0 Object Library to the Visual Studio project. To do this, right-click the project in the Visual Studio Solution Explorer and select the Add Reference… menu item. Select the COM tab in the Add Reference dialog box, then scroll down to the Microsoft Word 12.0 Object Library component, select it, and then click OK to add the reference.

Figure 1. Adding a Reference

Adding a reference

2. Import the Word Interop Namespace

Next, import the Microsoft.Office.Interop.Word namespace. This allows objects that are defined in that namespace to be referred to without having to specify the fully qualified namespace path. To import the namespace, add the following line to the top of the source file.

using Microsoft.Office.Interop.Word;

For Microsoft Visual Basic projects, you can also import the namespace by right-clicking the project in the Visual Studio Solution Explorer and selecting the Properties menu item. On the project properties page, select the References tab and then select the check box next to theMicrosoft.Office.Interop.Word entry in the list of imported namespaces.

3. Create an Instance of the Word ApplicationClass Object

To work with the Word 2007 object model, create an instance of the Word 2007 top-levelApplicationClass object and declare a variable to hold the reference to the document.

ApplicationClass wordApplication = new ApplicationClass();
Document wordDocument = null;

4. Declare Appropriate Variables

The following code blocks declare variables that help to make the parameters that are passed to methods used in the conversion code easier to read. The following variables are used with theDocuments.Open method and ApplicationClass.Quit method.

Use the paramSourceDocPath variable to specify the path and filename of the Word 2007 document that is to be exported to either the PDF format or the XPS format.

Use the paramMissing variable when calling methods that accept optional parameters. Optional parameters are only optional when you use Microsoft Visual Basic. You must specify a value for optional parameters when you use Microsoft Visual C#. Using Type.Missing as the value for an optional parameter indicates that the parameter is not specified and that the method should use the parameter's default value.

object paramSourceDocPath = @"C:\Temp\Test.docx";
object paramMissing = Type.Missing;

The following variables are used with the Document.ExportAsFixedFormat method. TheparamExportFormat variable is important because it is used to specify the format in which to export the document. The paramExportFormat variable is of the WdExportFormat type. It is an enumerated type which has two values, wdExportFormatXPS and wdExportFormatPDF. The following sample code sets the paramExportFormat variable to the WdExportFormat.wdExportFormatXPSvalue to export a document to the XPS format. To change the code to export a document in the PDF format, set the variable to the WdExportFormat.wdExportFormatPDF value. For more information on the ExportAsFixedFormat method and the parameters that it accepts, seeDocment.ExportAsFixedFormat.

string paramExportFilePath = @"C:\Temp\Test.xps";
WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatXPS;
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;

5. Implement the Conversion Code

Next add code that opens the source document, exports it to the specified format, and exits Word 2007. Making the call to the Document.ExportAsFixedFormat method throws an exception if the add-in for the format is not currently installed. To handle this situation, the conversion code is wrapped in a Try…Catch block. The code that exits Word 2007, which allows it to unload from memory, is located in a Finally block. The shutdown-related code closes the Word 2007 document, exits the Word 2007 application, releases references to the underlying Word 2007 COM objects, and makes calls to the .NET garbage collector. For more information about how to release COM objects when you use managed code, see Chapter 2: Basics of Office Interoperability (Part 2 of 3) from the book Microsoft .NET Development for Microsoft Office.

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

posted @ 2011-01-26 15:00  Nina  阅读(487)  评论(0编辑  收藏  举报