如何:打印 Windows 窗体中的多页文本文件
基于 Windows 的应用程序打印文本是很常见的。 Graphics 类提供将对象(图形或文本)绘制到设备(如屏幕或打印机)的方法。
注意
TextRenderer 的 DrawText 方法不支持打印。 如以下代码示例所示,应始终使用 Graphics 的 DrawString 方法来绘制文本以供打印。
打印文本
向窗体添加 PrintDocument 组件和字符串。
private PrintDocument printDocument1 = new PrintDocument(); private string stringToPrint;
Private printDocument1 As New PrintDocument() Private stringToPrint As String
打印文档时,将 DocumentName 属性设置要打印的文档,然后打开该文档,并将文档内容读取到以前添加的字符串中。
string docName = "testPage.txt"; string docPath = @"c:\"; printDocument1.DocumentName = docName; using (FileStream stream = new FileStream(docPath + docName, FileMode.Open)) using (StreamReader reader = new StreamReader(stream)) { stringToPrint = reader.ReadToEnd(); }
Dim docName As String = "testPage.txt" Dim docPath As String = "c:\" printDocument1.DocumentName = docName Dim stream As New FileStream(docPath + docName, FileMode.Open) Try Dim reader As New StreamReader(stream) Try stringToPrint = reader.ReadToEnd() Finally reader.Dispose() End Try Finally stream.Dispose() End Try
在 PrintPage 事件处理程序中,使用 PrintPageEventArgs 类的 Graphics 属性和文档内容来计算行长度和每页行数。 绘制完每一页后,检查它是否是最后一页,并相应地设置 HasMorePages 的 PrintPageEventArgs 属性。 引发 PrintPage 事件,直到 HasMorePages 为
false
。 此外,确保 PrintPage 事件与其事件处理方法关联。在下面的代码示例中,事件处理程序用于打印“testPage.txt”文件的内容(所用字体与窗体上使用的字体相同)。
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) { int charactersOnPage = 0; int linesPerPage = 0; // Sets the value of charactersOnPage to the number of characters // of stringToPrint that will fit within the bounds of the page. e.Graphics.MeasureString(stringToPrint, this.Font, e.MarginBounds.Size, StringFormat.GenericTypographic, out charactersOnPage, out linesPerPage); // Draws the string within the bounds of the page e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black, e.MarginBounds, StringFormat.GenericTypographic); // Remove the portion of the string that has been printed. stringToPrint = stringToPrint.Substring(charactersOnPage); // Check to see if more pages are to be printed. e.HasMorePages = (stringToPrint.Length > 0); }
Private Sub printDocument1_PrintPage(ByVal sender As Object, _ ByVal e As PrintPageEventArgs) Dim charactersOnPage As Integer = 0 Dim linesPerPage As Integer = 0 ' Sets the value of charactersOnPage to the number of characters ' of stringToPrint that will fit within the bounds of the page. e.Graphics.MeasureString(stringToPrint, Me.Font, e.MarginBounds.Size, _ StringFormat.GenericTypographic, charactersOnPage, linesPerPage) ' Draws the string within the bounds of the page e.Graphics.DrawString(stringToPrint, Me.Font, Brushes.Black, _ e.MarginBounds, StringFormat.GenericTypographic) ' Remove the portion of the string that has been printed. stringToPrint = stringToPrint.Substring(charactersOnPage) ' Check to see if more pages are to be printed. e.HasMorePages = stringToPrint.Length > 0 End Sub
-
printDocument1.Print();
printDocument1.Print()
示例
using System;
using System.Drawing;
using System.IO;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace PrintApp
{
public class Form1 : Form
{
private Button printButton;
private PrintDocument printDocument1 = new PrintDocument();
private string stringToPrint;
public Form1()
{
this.printButton = new System.Windows.Forms.Button();
this.printButton.Location = new System.Drawing.Point(12, 51);
this.printButton.Size = new System.Drawing.Size(75, 23);
this.printButton.Text = "Print";
this.printButton.Click += new System.EventHandler(this.printButton_Click);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.printButton);
// Associate the PrintPage event handler with the PrintPage event.
printDocument1.PrintPage +=
new PrintPageEventHandler(printDocument1_PrintPage);
}
private void ReadFile()
{
string docName = "testPage.txt";
string docPath = @"c:\";
printDocument1.DocumentName = docName;
using (FileStream stream = new FileStream(docPath + docName, FileMode.Open))
using (StreamReader reader = new StreamReader(stream))
{
stringToPrint = reader.ReadToEnd();
}
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
int charactersOnPage = 0;
int linesPerPage = 0;
// Sets the value of charactersOnPage to the number of characters
// of stringToPrint that will fit within the bounds of the page.
e.Graphics.MeasureString(stringToPrint, this.Font,
e.MarginBounds.Size, StringFormat.GenericTypographic,
out charactersOnPage, out linesPerPage);
// Draws the string within the bounds of the page
e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black,
e.MarginBounds, StringFormat.GenericTypographic);
// Remove the portion of the string that has been printed.
stringToPrint = stringToPrint.Substring(charactersOnPage);
// Check to see if more pages are to be printed.
e.HasMorePages = (stringToPrint.Length > 0);
}
private void printButton_Click(object sender, EventArgs e)
{
ReadFile();
printDocument1.Print();
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Imports System.Drawing
Imports System.IO
Imports System.Drawing.Printing
Imports System.Windows.Forms
Public Class Form1
Inherits Form
Private printButton As Button
Private printDocument1 As New PrintDocument()
Private stringToPrint As String
Public Sub New()
Me.printButton = New System.Windows.Forms.Button()
Me.printButton.Location = New System.Drawing.Point(12, 51)
Me.printButton.Size = New System.Drawing.Size(75, 23)
Me.printButton.Text = "Print"
Me.ClientSize = New System.Drawing.Size(292, 266)
End Sub
Private Sub ReadFile()
Dim docName As String = "testPage.txt"
Dim docPath As String = "c:\"
printDocument1.DocumentName = docName
Dim stream As New FileStream(docPath + docName, FileMode.Open)
Try
Dim reader As New StreamReader(stream)
Try
stringToPrint = reader.ReadToEnd()
Finally
reader.Dispose()
End Try
Finally
stream.Dispose()
End Try
End Sub
Private Sub printDocument1_PrintPage(ByVal sender As Object, _
ByVal e As PrintPageEventArgs)
Dim charactersOnPage As Integer = 0
Dim linesPerPage As Integer = 0
' Sets the value of charactersOnPage to the number of characters
' of stringToPrint that will fit within the bounds of the page.
e.Graphics.MeasureString(stringToPrint, Me.Font, e.MarginBounds.Size, _
StringFormat.GenericTypographic, charactersOnPage, linesPerPage)
' Draws the string within the bounds of the page
e.Graphics.DrawString(stringToPrint, Me.Font, Brushes.Black, _
e.MarginBounds, StringFormat.GenericTypographic)
' Remove the portion of the string that has been printed.
stringToPrint = stringToPrint.Substring(charactersOnPage)
' Check to see if more pages are to be printed.
e.HasMorePages = stringToPrint.Length > 0
End Sub
Private Sub printButton_Click(ByVal sender As Object, ByVal e As EventArgs)
ReadFile()
printDocument1.Print()
End Sub
<STAThread()> _
Shared Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New Form1())
End Sub
End Class
编译代码
此示例需要:
名为 testPage.txt 的文本文件,该文件包含要打印的文本且位于驱动器的根目录 C:\ 中。 编辑代码以打印不同的文件。
对 System、System.Windows.Forms 和 System.Drawing 程序集的引用。
若要了解如何从 Visual Basic 或 Visual C# 的命令行生成此示例,请参阅从命令行生成或在命令行上使用 csc.exe 生成。 还可以通过将代码粘贴到新项目,在 Visual Studio 中生成此示例。