简单的文本打印机






4.91/5 (7投票s)
一个用于打印文本、图像和线条(包括完整文本文件)的例程
注意:打印输出已发送到 PDF 打印机以生成此示例。
引言
此例程允许打印文本、水平线和图像。它提供了指定常用页面设置(大小、方向、边距等)的属性,以及设置文本、线条和图像以及相关格式选项(即字体、颜色)的方法。它在内部使用 PrintDocument
(命名空间: System.Drawing.Printing
) 和 StandardPrintController
来生成打印输出。 StandardPrintController
还用于隐藏默认的 PrinterDialog
并直接打印到用户的标准打印机。
背景
通常,需要一个简单的例程来打印完整的文本文件或应用程序消息,并处理多个页面。此外,应该很容易设置常用的页面设置,并为输出使用标准格式。它应该在内部设置正确的 x 和 y 绘图位置,而用户只需提供打印对象(加上可选的与对象相关的选项)。为了满足所有这些要求,我决定编写这个类。
Using the Code
要打印您的内容,您只需创建一个新的 TextPrinter
实例
Using prt As New TextPrinter(Printing.PaperKind.Letter)
With prt
With .Header1stPage
.TextLeft = "Printed by: " & Environment.UserName
.TextRight = "Page: [PAGE]" & vbCrLf & Now.ToString
End With
.Landscape = True
End With
'... further code
End Using
此外,您可以设置以下属性
DefaultFont
:如果您没有为打印文本指定字体,则为默认打印字体DocumentName
:用于打印队列和打印准备对话框的文档名称Footer1stPage
:在第 1 页上显示的页脚(包括格式 - 即字体、颜色)FooterPage
:在除第 1 页之外的所有其他页面上显示的页脚(包括格式 - 即字体、颜色)Header1stPage
:在第 1 页上显示的页眉(包括格式 - 即字体、颜色)HeaderPage
:在除第 1 页之外的所有其他页面上显示的页眉(包括格式 - 即字体、颜色)Landscape
:定义使用的页面方向PaperKind
:使用的纸张类型 - 即 A4、Letter 等TabCharWidth
:替换文本中制表符的空格字符数(默认值:4)。
对于页面页眉和页脚,您可以为左侧、中间和/或右侧位置设置单独的文本。 [PAGE]
是一个自动填充的文本变量(仅在页眉/页脚中使用),用于显示页码。
下一步,您定义应该打印的对象。 在这种情况下,是通过 SetText
方法从外部文件获取的文本。 您可以选择使用的字体、前景色和/或水平对齐方式作为可选参数
.SetText(My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\print.xml", _
System.Text.Encoding.Default), New Font("Courier New", 10), Color.Blue)
要打印水平线,您可以在 SetLine
方法中定义线条颜色、宽度和水平对齐方式。 如果您提供的宽度值 <=1
,则该宽度将被解释为百分比值。 这意味着值 0.5
是可用页面宽度的 50%
.SetLine(Color.Red)
.SetLine(Color.Blue, 0.5, HorizontalAlignment.Center)
可以通过 SetImage
打印图像。 作为可选参数,您可以定义水平对齐方式和图像大小。 如果图像大小大于页面大小,则图像将自动调整为页面大小
.SetImage(My.Resources.IMG_3392, HorizontalAlignment.Center)
以下代码生成上述示例文档。它使用方法:SetLine
、SetText
、SetImage
和 Print
来创建打印输出。 或者,您可以使用方法:ShowPreview
来显示 PrinterPrintPreview
并直接从该预览中打印文档
'read an external xml file as sample text file
Dim fileContent As String = String.Empty
Using file As New IO.StreamReader(Application.StartupPath & "\print.xml", True)
fileContent = file.ReadToEnd
End Using
'create new instance and set paper size to A4
Using prt As New TextPrinter(PaperKind.A4)
With prt
'set header for the 1st page
With .Header1stPage
.TextLeft = "Printed by: " & Environment.UserName
.TextRight = "Page: [PAGE]" & vbCrLf & Now.ToString
End With
'set footer for the 1st page
With .Footer1stPage
.TextCenter = "FOR INTERNAL USE ONLY!"
.ColorCenter = Color.Red
End With
'for all other pages we are using a different header and footer:
With .HeaderPage
.TextRight = "Page: [PAGE]" & vbCrLf & Now.ToString
End With
With .FooterPage
.TextCenter = "FOR INTERNAL USE ONLY!"
.ColorCenter = Color.Red
.TextLeft = "Application:" & vbCrLf & Assembly.GetExecutingAssembly.Location
End With
.DefaultFont = New Font("Tahoma", 10)
'set the objects those will be printed
.SetLine() 'no parameters specified: color black is used
.SetText("START OF PRINTING.", New Font(.DefaultFont, FontStyle.Bold))
.SetLine()
.SetText(fileContent, New Font("Courier New", 10), Color.Blue)
.SetLine()
.SetText(vbCrLf & vbCrLf & "Now we are printing an image (taken in Nassfeld [Austria]:", _
New Font(.DefaultFont, FontStyle.Italic))
.SetImage(My.Resources.IMG_3392, HorizontalAlignment.Center)
.SetText("END OF PRINTING.", New Font(.DefaultFont, FontStyle.Bold))
.SetLine()
'print or show preview instead
If isPreview Then
'Show preview
.ShowPreview(Me)
Else
'Prints directly:
.Print()
End If
End With
End Using
就是这样!
兴趣点(或更深入的视角)
所有绘图都在类 TextPrinter
中完成。 最重要的方法是 _printPage
(它处理页眉和页脚以及打印对象)和 _printCharacters
。
_printCharacters
用于测量可用绘图大小的文本,并返回已打印的字符数
Private Function _printCharacters(ByVal g As Graphics, ByVal text As String, _
ByVal font As Font, ByVal align As HorizontalAlignment, _
ByVal forecolor As Color, ByRef r As Rectangle) As Integer
Dim chrFitted As Integer
Using sf As New StringFormat
With sf
If text.Contains(vbTab) Then .FormatFlags = StringFormatFlags.MeasureTrailingSpaces
'set the horizontal text alignment
If align = HorizontalAlignment.Center Then
.Alignment = StringAlignment.Center
ElseIf align = HorizontalAlignment.Right Then
.Alignment = StringAlignment.Far
End If
End With
'get size of printable text
Dim sz As SizeF = g.MeasureString(text, font, New Size(r.Width, r.Height - font.Height), _
sf, chrFitted, 0)
With g
Using b As New SolidBrush(forecolor)
'draw characters those are fitting the rectangle
.DrawString(Left(text, chrFitted), font, b, r, sf)
End Using
End With
'reduce the rectangle by drawn characters
r.Y += sz.Height
r.Height -= sz.Height
End Using
'return the drawn number of characters
Return chrFitted
End Function
历史
- 1.0.0.0 - 初始发布
- 1.0.0.1 - 小修改(处理字体和图像;如果提供的纸张类型不受支持,则方法
_print
使用打印机默认的pagesize
)