65.9K
CodeProject 正在变化。 阅读更多。
Home

WPF 打印 - 基本文本打印

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.58/5 (15投票s)

2009年2月17日

CPOL

3分钟阅读

viewsIcon

86085

downloadIcon

2318

这篇文章教你如何在 WPF 中执行基本的文本打印。

引言

本文演示了如何在 WPF 中执行基本的文本打印。

要跳过文章并直接查看代码,请参阅下面的“完整且简化的打印例程”。

背景

最近,我创建了我的第一个 WPF 应用程序。它是一个非常简单的应用程序。它所做的只是处理一些数据,然后打印一份报告。嗯,处理数据的代码是标准的;然而,试图找出打印简单报告的代码让我抓狂!我在互联网上搜索了一些示例(并找到了一些),但并没有真正找到我想要的,我想要的是一些简单快捷的东西。所以,我开始怀疑 WPF 是否支持简单的打印。经过一番尝试,我发现它确实支持!

WPF 不仅支持打印,而且比 Windows Forms 打印更强大(信不信由你)。微软实际上投入了大量时间,并在为 WPF 设计打印模型时做了一些重大改进。唯一的缺点是它有点复杂。

由于我的主要目标只是做一些非常基本的打印,例如:进行一些处理并将文本报告发送到打印机,这就是本文将演示的内容。

使用 PrintDialog.PrintDocument 方法打印文本

首先,让我们创建一些要打印的文本

Dim text As String = "Print this text"

其次,让我们创建打印标题:这是当您打开打印机并查看打印作业时将出现在 Windows 打印队列中的文本。它仅在作业打印时显示。

Dim printCaption As String = "My first print job."

我们需要做的下一件事是创建一个 PrintDialog 类的新的实例。此类不仅可以用于显示打印对话框,供用户能够选择打印机和修改打印机选项,还可以用于实际执行打印,即使您决定不显示打印对话框

'Used to perform printing
Dim diag As New PrintDialog()  

您尝试的下一个合乎逻辑的事情是使用 PrintDocument 方法

'Send the document to the printer
diag.PrintDocument(text, printCaption) 

但是,您会很快发现 PrintDocument 无法以这种方式工作PrintDocument 方法接受 DocumentPaginator (System.Windows.Documents.DocumentPaginator)。由于 DocumentPaginator 是一个 "Must Inherit" (abstract) 类,您无法创建它的新实例。那么,我们从哪里获得一个呢?好问题...

这就是它变得有点复杂的地方(正如我提到的)... FlowDocument (System.Windows.Documents.FlowDocument) 类可用于存储和格式化我们想要打印的文本。尽管 FlowDocument 类不是 DocumentPaginator,但它实现了 IDocumentPaginatorSource.DocumentPaginator 接口,该接口提供了对其 DocumentPaginator 的访问!

不幸的是,FlowDocument 类构造函数不只是将文本(一个 String)作为参数,它接受一个 Block (System.Windows.Documents.Block),而这恰好是另一个 "Must Inherit" (abstract) 类。因为 Paragraph 类 (System.Windows.Documents.Paragraph) 继承了 Block 类,所以它可以用作参数。

Paragraph 类构造函数类似于 FlowDocument 类构造函数,因为它不将文本(一个 String)作为参数。相反,Paragraph 类构造函数接受一个 Inline 类作为参数,而这恰好(再次)是另一个 "Must Inherit" (abstract) 类。因为 Run 类 (System.Windows.Documents.Run) 继承了 Inline 类,所以它可以用作参数。

说了这么多,让我们看看其余的代码是什么样的

'Create a new Run class, passing it the text.
' The Run class contains 1 line of text
Dim r As New Run(text)

'Create a new paragraph, passing it the new run class instance
Dim ph As New Paragraph(r)

'Create the document, passing a new paragraph
Dim doc As New FlowDocument(ph)
doc.PagePadding = New Thickness(100) 'Creates margin around the page

'Send the document to the printer
diag.PrintDocument( _
    CType(doc, IDocumentPaginatorSource).DocumentPaginator, _
    printCaption)

完整且简化的打印例程

既然我们知道了如何对打印进行编程,让我们将其压缩并将其全部放在一个可以调用的子例程中来执行打印

Private Sub PrintUsingDocumentCondensed(ByVal text As String, _
        Optional ByVal printCaption As String = "")

    'Create the document, passing a new paragraph and new run using text
    Dim doc As New FlowDocument(New Paragraph(New Run(text)))
    doc.PagePadding = New Thickness(100) 'Creates margin around the page

    Dim diag As New PrintDialog() 'Used to perform printing

    'Send the document to the printer
    diag.PrintDocument( _
        CType(doc, IDocumentPaginatorSource).DocumentPaginator, _
        printCaption)

End Sub

最后,实际执行我们的打印

PrintUsingDocumentCondensed("Print this text", "My first print job")

结论

正如您所看到的,在 WPF 中打印基本文本非常容易!

我为您提供了一个源代码演示,该演示是用 Visual Studio 2008 中的 Visual Basic 创建的。

希望这篇文章对您有所帮助!

© . All rights reserved.