使用 VB 2008 和 WPF 从 Word 文档生成 XPS






2.77/5 (4投票s)
一种通过 Visual Basic 代码和 VSTO 程序集创建 XPS 文档的替代方法。
引言
随着 .NET Framework 3.0 的发布,Microsoft 引入了一种名为 XPS(XML Paper Specifications)的新文件格式,其扩展名为.Xps。 这种文件格式被认为是文档可移植性的标准; 事实上,XPS 文档可以在每个系统上读取,而无需安装原始应用程序(就像 PDF 文档一样)。
.NET Framework 3.x 完全支持这种文档,可以使用极高的精度创建和操作它们。 通过代码操作这种文档(尤其是写入)通常并不简单,特别是如果您需要添加图片、缩略图、数字签名和其他支持的内容。
即使 .NET Framework 3.x 公开了几个类来读取、写入和管理 XPS 文档,在本文中,我想向您展示如何通过几个步骤从 Microsoft Word 2007 文档(.docx 和 .doc 格式)创建 XPS 文档。
事实上,我们可以通过编写几行 Visual Basic 2008 代码并引用 Visual Studio Tools for Office 主要程序集来创建 XPS 文档。 我们将在 WPF 应用程序中看到它的实际效果。 您必须遵守两个先决条件:您需要安装 Microsoft Word 2007 和 XPS/PDF 导出器加载项。
使用代码
首先,打开 Visual Studio 2008 并为 Visual Basic 创建一个新的 WPF 应用程序。 然后,添加对以下程序集的引用
- ReachFramework.dll
- Microsoft.Office.Interop.Word.dll
- Microsoft.Office.Tools.v9.0.dll
- Microsoft.Office.Tools.Word.v9.0dll
- Microsoft.VisualStudio.Tools.Office.Runtime.v9.0.dll
正如您所理解的,我们正在为 Visual Studio 2008 添加对所有 VSTO 程序集的引用,但没有创建 Office 解决方案。
在表示层上,我们希望让用户通过单击按钮选择 Word 2007 文档,并自动从 WPF DocumentViewer
控件中显示导出的文档。 以下 XAML 代码可以完成此操作
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<DocumentViewer Grid.Row="1" Name="DocumentViewer1" />
<Button HorizontalAlignment="Left"
Margin="10,10,10,5" Name="Button1"
Height="25" Width="150">
Select a document</Button>
</Grid>
</Window>
然后,我们可以编写托管代码。 首先,我们必须添加以下 Imports
指令
Imports Microsoft.Office.Interop.Word
Imports Microsoft.Win32
Imports System.Windows.Xps.Packaging
其次,我们可以编写一种方法来将用户选择的 Word 文档导出为 XPS。 代码中的注释应该有帮助
Private Function exportAsXps(ByVal fileName As String) As Boolean
Try
'Starts a background instance of Word 2007
'and adds the specified document to the
‘documents collection
Dim wordInstance As New _
Microsoft.Office.Interop.Word.Application
wordInstance.Documents.Add(fileName)
'Obtains the output file name,
‘replacing the extension with.Xps
Dim outputName As String = _
String.Concat(IO.Path.GetDirectoryName(fileName), "\", _
IO.Path.GetFileNameWithoutExtension(fileName), ".xps")
'Retrieves the active document instance
‘and saves specifying name and format
Dim doc As Document = wordInstance.ActiveDocument
doc.SaveAs(outputName, WdSaveFormat.wdFormatXPS)
'Shuts down Word 2007
wordInstance.Quit()
'View the generated XPS Document
Dim xpsDoc As New XpsDocument(outputName, IO.FileAccess.Read)
DocumentViewer1.Document = xpsDoc.GetFixedDocumentSequence
Return True
'TODO: Add here specific exceptions
Catch ex As Exception
MessageBox.Show(ex.ToString, "", _
MessageBoxButton.OK, MessageBoxImage.Information)
Return False
End Try
End Function
一旦我们有了我们的方法,使用它就非常简单了。 我们可以从我们实现的用于选择 Word 文档的按钮的 Click
事件处理程序中调用它
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
Dim ofd As New OpenFileDialog
'The Win32 OpenFileDialog can return True, False or Nothing
Dim result As Nullable(Of Boolean)
With ofd
.Title = "Select a Word document"
.Filter = "Documents|*.docx;*.doc|All files|*.*"
result = .ShowDialog
'If the dialog returns True, starts exporting
If result = True Then
Dim exportResult As Boolean = exportAsXps(.FileName)
'TODO: Add here check code
End If
End With
End Sub
上图显示了我的 .docx (Word 2007) 文档转换的结果。
关注点
用很少的几行代码,并且没有执行 Microsoft Word,我们可以从我们的 Word 内容创建 XPS 文档,即使是非常复杂的文档。 代码肯定可以增强,但我认为如果您对此类操作感兴趣,它可以是一个很好的起点。
使用此技术,您不仅可以将文档导出为 XPS,还可以导出为 Microsoft Word 2007 支持的所有格式。