基于 Windows 的应用程序通常打印文本。 Graphics 类为设备(如屏幕或打印机)提供绘制对象(图形或文本)的方法。 以下部分详细介绍了打印文本文件的过程。 此方法不支持打印非纯文本文件,如 Office Word 文档或 PDF 文件。
注释
TextRenderer的方法DrawText不支持打印。 在绘制用于打印的文本时,应始终使用 DrawString的 Graphics 方法,如以下代码示例所示。
打印文本
在 Visual Studio 中,在“解决方案资源管理器”窗格中双击要从中打印的窗体。 此操作将打开视觉设计器。
在 工具箱中,双击 PrintDocument 组件以将其添加到窗体。 此操作创建一个名称为
printDocument1
的PrintDocument
组件。将
Button
添加到窗体,或使用窗体上已有的按钮。在窗体的可视化设计器中,选择该按钮。 在 属性 窗格中,选择 事件 筛选器按钮,然后双击
Click
事件以生成事件处理程序。Click
事件代码应可见。 在事件处理程序的作用域之外,将私有字符串变量添加到名为stringToPrint
的类。private string stringToPrint="";
'Private PrintDocument1 As New PrintDocument() Private stringToPrint As String
返回
Click
事件处理程序代码,将 DocumentName 属性设置为文档的名称。 此信息将发送到打印机。 接下来,读取文档文本内容并将其存储在stringToPrint
字符串中。 最后,调用 Print 方法以引发 PrintPage 事件。 以下代码中Print
方法被突出显示:private void button1_Click(object sender, EventArgs e) { string docName = "testPage.txt"; string docPath = @"C:\"; string fullPath = System.IO.Path.Combine(docPath, docName); printDocument1.DocumentName = docName; stringToPrint = System.IO.File.ReadAllText(fullPath); printDocument1.Print(); }
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim docName As String = "testPage.txt" Dim docPath As String = "C:\" Dim fullPath As String = System.IO.Path.Combine(docPath, docName) PrintDocument1.DocumentName = docName stringToPrint = System.IO.File.ReadAllText(fullPath) PrintDocument1.Print() End Sub
返回到窗体的可视化设计器,然后选择
PrintDocument
组件。 在 属性 窗格中,选择 事件 筛选器,然后双击PrintPage
事件以生成事件处理程序。在 PrintPage 事件处理程序中,使用 Graphics 类的 PrintPageEventArgs 属性和文档内容来计算每页的行长度和行数。 绘制完每一页后,检查它是否是最后一页,并相应地设置 HasMorePages 的
PrintPageEventArgs
属性。 引发PrintPage
事件,直到HasMorePages
为false
。在下面的代码示例中,事件处理程序用于以与窗体上使用的字体相同的字体打印“testPage.txt”文件的内容。
private void PrintDocument1_PrintPage(object sender, System.Drawing.Printing.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) 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 End Sub