如何使用打印预览在 Windows 窗体中打印
除了打印服务之外,Windows 窗体编程中也非常常见地提供打印预览。 向应用程序添加打印预览服务的一种简单方法是将 PrintPreviewDialog 控件与用于打印文件的 PrintPage 事件处理逻辑结合使用。
使用 PrintPreviewDialog 控件预览文本文档
向窗体添加 PrintPreviewDialog、PrintDocument和两个字符串。
private PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog(); private PrintDocument printDocument1 = new PrintDocument(); // Declare a string to hold the entire document contents. private string documentContents; // Declare a variable to hold the portion of the document that // is not printed. private string stringToPrint;
Private printPreviewDialog1 As New PrintPreviewDialog() Private WithEvents printDocument1 As New PrintDocument() ' Declare a string to hold the entire document contents. Private documentContents As String ' Declare a variable to hold the portion of the document that ' is not printed. Private stringToPrint As String
将 DocumentName 属性设置为要打印的文档,然后打开文档并将其内容读取到之前添加的字符串中。
private void ReadDocument() { 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)) { documentContents = reader.ReadToEnd(); } stringToPrint = documentContents; }
Private Sub ReadDocument() 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 documentContents = reader.ReadToEnd() Finally reader.Dispose() End Try Finally stream.Dispose() End Try stringToPrint = documentContents End Sub
与打印文档一样,在 PrintPage 事件处理程序中,使用 PrintPageEventArgs 类的 Graphics 属性和文件内容来计算每页的行数并呈现文档的内容。 绘制每个页面后,检查每一页是否为最后一页,并相应地设置 PrintPageEventArgs 的 HasMorePages 属性。 将引发 PrintPage 事件,直到 HasMorePages
false
。 文档完成呈现后,重置要呈现的字符串。 此外,请确保 PrintPage 事件与其事件处理方法相关联。注意
如果已在应用程序中实现打印,则可能已完成步骤 2 和 3。
在下面的代码示例中,事件处理程序用于以窗体上所用的同一字体打印“testPage.txt”文件。
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); // If there are no more pages, reset the string to be printed. if (!e.HasMorePages) stringToPrint = documentContents; }
Sub printDocument1_PrintPage(ByVal sender As Object, _ ByVal e As PrintPageEventArgs) Handles printDocument1.PrintPage 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 ' If there are no more pages, reset the string to be printed. If Not e.HasMorePages Then stringToPrint = documentContents End If End Sub
将 PrintPreviewDialog 控件的 Document 属性设置为窗体上的 PrintDocument 组件。
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.Document = printDocument1
对 PrintPreviewDialog 控件调用 ShowDialog 方法。 通常,您会从按钮的 Click 事件处理方法中调用 ShowDialog。 调用 ShowDialog 将引发 PrintPage 事件,并将输出呈现到 PrintPreviewDialog 控件。 当用户单击对话框上的打印图标时,将再次引发 PrintPage 事件,将输出发送到打印机而不是预览对话框。 这就是在步骤 3 中呈现过程结束时重置字符串的原因。
下面的代码示例显示了窗体上按钮的 Click 事件处理方法。 此事件处理方法调用方法读取文档并显示打印预览对话框。
private void printPreviewButton_Click(object sender, EventArgs e) { ReadDocument(); printPreviewDialog1.Document = printDocument1; printPreviewDialog1.ShowDialog(); }
Private Sub printPreviewButton_Click(ByVal sender As Object, _ ByVal e As EventArgs) Handles printPreviewButton.Click ReadDocument() printPreviewDialog1.Document = printDocument1 printPreviewDialog1.ShowDialog() End Sub
例
using System;
using System.Drawing;
using System.IO;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace PrintPreviewApp
{
public partial class Form1 : Form
{
private Button printPreviewButton;
private PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
private PrintDocument printDocument1 = new PrintDocument();
// Declare a string to hold the entire document contents.
private string documentContents;
// Declare a variable to hold the portion of the document that
// is not printed.
private string stringToPrint;
public Form1()
{
this.printPreviewButton = new System.Windows.Forms.Button();
this.printPreviewButton.Location = new System.Drawing.Point(12, 12);
this.printPreviewButton.Size = new System.Drawing.Size(125, 23);
this.printPreviewButton.Text = "Print Preview";
this.printPreviewButton.Click += new System.EventHandler(this.printPreviewButton_Click);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.printPreviewButton);
printDocument1.PrintPage +=
new PrintPageEventHandler(printDocument1_PrintPage);
}
private void ReadDocument()
{
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))
{
documentContents = reader.ReadToEnd();
}
stringToPrint = documentContents;
}
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);
// If there are no more pages, reset the string to be printed.
if (!e.HasMorePages)
stringToPrint = documentContents;
}
private void printPreviewButton_Click(object sender, EventArgs e)
{
ReadDocument();
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
[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
Class Form1
Inherits Form
Private WithEvents printPreviewButton As Button
Private printPreviewDialog1 As New PrintPreviewDialog()
Private WithEvents printDocument1 As New PrintDocument()
' Declare a string to hold the entire document contents.
Private documentContents As String
' Declare a variable to hold the portion of the document that
' is not printed.
Private stringToPrint As String
Public Sub New()
Me.printPreviewButton = New System.Windows.Forms.Button()
Me.printPreviewButton.Location = New System.Drawing.Point(12, 12)
Me.printPreviewButton.Size = New System.Drawing.Size(125, 23)
Me.printPreviewButton.Text = "Print Preview"
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.printPreviewButton)
End Sub
Private Sub ReadDocument()
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
documentContents = reader.ReadToEnd()
Finally
reader.Dispose()
End Try
Finally
stream.Dispose()
End Try
stringToPrint = documentContents
End Sub
Sub printDocument1_PrintPage(ByVal sender As Object, _
ByVal e As PrintPageEventArgs) Handles printDocument1.PrintPage
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
' If there are no more pages, reset the string to be printed.
If Not e.HasMorePages Then
stringToPrint = documentContents
End If
End Sub
Private Sub printPreviewButton_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles printPreviewButton.Click
ReadDocument()
printPreviewDialog1.Document = printDocument1
printPreviewDialog1.ShowDialog()
End Sub
<STAThread()> _
Shared Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New Form1())
End Sub
End Class
编译代码
此示例需要:
- 对 System、System.Windows.Forms、System.Drawing 程序集的引用。
另请参阅
- 如何:在 Windows 窗体中打印多页文本文件
- Windows 窗体打印支持
- 在 Windows 窗体中更安全的打印