打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Saving Word 2007 Documents to PDF and XPS Formats

The 2007 Microsoft Office Add-in: Microsoft Save as PDF and 2007 Microsoft Office Add-in: Microsoft Save as XPS allow a Microsoft Office Word 2007 to export and save documents in the PDF and XPS formats. This article illustrates how to use the Microsoft Word 12.0 Object Library to access the Word 2007 Document.ExportAsFixedFormat method to programmatically convert an existing Word 2007 document to either the PDF format or the XPS format.

Download the Code Sample

To illustrate how to programmatically save a Word 2007 document to either the PDF format or the XPS format, this section walks through five key steps.

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 

 

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.

Imports 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 the Microsoft.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-level ApplicationClass object and declare a variable to hold the reference to the document.

Dim wordApplication As ApplicationClass = New ApplicationClass()Dim wordDocument As Document = Nothing

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 the Documents.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.

Dim paramSourceDocPath As String = "C:\Temp\Test.docx"

The following variables are used with the Document.ExportAsFixedFormat method. The paramExportFormat 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.wdExportFormatXPS value 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, see Docment.ExportAsFixedFormat.

Dim paramExportFilePath As String = "C:\Temp\Test.xps"Dim paramExportFormat As WdExportFormat = _    WdExportFormat.wdExportFormatXPSDim paramOpenAfterExport As Boolean = FalseDim paramExportOptimizeFor As WdExportOptimizeFor = _    WdExportOptimizeFor.wdExportOptimizeForPrintDim paramExportRange As WdExportRange = _    WdExportRange.wdExportAllDocumentDim paramStartPage As Int32 = 0Dim paramEndPage As Int32 = 0Dim paramExportItem As WdExportItem = _    WdExportItem.wdExportDocumentContentDim paramIncludeDocProps As Boolean = TrueDim paramKeepIRM As Boolean = TrueDim paramCreateBookmarks As WdExportCreateBookmarks = _    WdExportCreateBookmarks.wdExportCreateWordBookmarksDim paramDocStructureTags As Boolean = TrueDim paramBitmapMissingFonts As Boolean = TrueDim paramUseISO19005_1 As Boolean = 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(paramSourceDocPath)    ' Export it in the specified format.    If Not wordDocument Is Nothing Then        wordDocument.ExportAsFixedFormat(paramExportFilePath, _            paramExportFormat, paramOpenAfterExport, _            paramExportOptimizeFor, paramExportRange, paramStartPage, _            paramEndPage, paramExportItem, paramIncludeDocProps, _            paramKeepIRM, paramCreateBookmarks, _            paramDocStructureTags, paramBitmapMissingFonts, _            paramUseISO19005_1)    End IfCatch ex As Exception    ' Respond to the errorFinally    ' Close and release the Document object.    If Not wordDocument Is Nothing Then        wordDocument.Close(False)        wordDocument = Nothing    End If    ' Quit Word and release the ApplicationClass object.    If Not wordApplication Is Nothing Then        wordApplication.Quit()        wordApplication = nothing    End If    GC.Collect()    GC.WaitForPendingFinalizers()    GC.Collect()    GC.WaitForPendingFinalizers()End Try

There are a number of scenarios in which you may want to develop a Microsoft Office business application that requires both dynamic and static views of the same document. In Word 2007 you can use the 2007 Microsoft Office Add-in: Microsoft Save as PDF and 2007 Microsoft Office Add-in: Microsoft Save as XPS to save a Word 2007 document as either a PDF document or an XPS document.

The key object that is used to save a Word 2007 document in either the PDF format or the XPS format is the Document object. The Document object has a method called Document.ExportAsFixedFormat, which has a number of key parameters that it accepts to save the target document in the desired format. This article specifically explores how to use the Documents.Open method and Document.ExportAsFixedFormat method to programmatically save a Word 2007 document as either a PDF document or an XPS document.

To programmatically save a Word 2007 document as either a PDF document or an XPS document

  1. Add a reference to the Word 12.0 Object Library to the project. This marks that the project uses the Word 12.0 Object Library.

  2. Import the Microsoft.Office.Interop.Word namespace. This allows code to use the classes and types exposed as part of the Microsoft.Office.Interop.Word namespace without having to specify the fully qualified namespace path.

  3. Create an instance of the Word 2007 ApplicationClass object. This is the top-most class in the Word 2007 object model hierarchy, and is the starting point for working with the other classes in the object model.

  4. Declare variables to help with method calls. This helps to make the parameters that are passed to methods used in the conversion code easier to read.

  5. Implement the conversion code. The final step provides code to implement the programmatic conversion.

Note
The preceding example code used the Type.Missing object to specify that an optional parameter was not provided and that the default parameter value should be used. To change the behavior to use something other than the default parameter types and values, specify the appropriate parameter types and values. For more information about the Documents.Open method and Document.ExportAsFixedFormat method and the parameters that they accept, see the Word 2007 Object Model Reference.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
利用word将PDF转换DOC文件的方法 --电脑高手
PDF转换word格式的方法总结 | 善用佳软
Office组件轻松把PDF文件转成Word文档
VB关于webbrowser相关操作大全
用office 2003提取图片中的文字
如何将扫描文件转换成WORD形式
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服