使用打印预览打印 您所在的位置:网站首页 开源打印控件 使用打印预览打印

使用打印预览打印

2023-12-30 20:24| 来源: 网络整理| 查看: 265

在 Visual Studio 中,使用“解决方案资源管理器”窗格,并双击要从中打印的窗体。 此操作后将打开可视化设计器。

在“工具箱”窗格中,双击 PrintDocument 组件和 PrintPreviewDialog 组件,将其添加到窗体中。

将 Button 添加到窗体,或使用窗体上已存在的按钮。

在窗体的可视化设计器中,选择该按钮。 在“属性”窗格中,选择“事件”筛选器按钮,然后双击 Click 事件以生成事件处理程序。

Click 事件代码应可见。 在事件处理程序的范围之外,将两个私有字符串变量添加到名为 documentContents 和 stringToPrint 的类:

// 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=""; ' 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

返回到 Click 事件处理程序代码,为你想要打印的文档设置 DocumentName 属性,然后打开并读取文档内容到之前添加的字符串。

string docName = "testPage.txt"; string docPath = @"C:\"; string fullPath = System.IO.Path.Combine(docPath, docName); printDocument1.DocumentName = docName; stringToPrint = System.IO.File.ReadAllText(fullPath); Dim docName As String = "testPage.txt" Dim docPath As String = "C:\Users\v-rsatao\Desktop\" Dim fullPath As String = System.IO.Path.Combine(docPath, docName) PrintDocument1.DocumentName = docName stringToPrint = System.IO.File.ReadAllText(fullPath)

就像你为了打印文件所执行的操作那样,在 PrintPage 事件处理程序中使用 Graphics 类的 PrintPageEventArgs 属性和文件内容来计算每页行数并呈现文档的内容。 绘制完每一页后,检查它是否是最后一页,并相应地设置 PrintPageEventArgs 的 HasMorePages 属性。 引发 PrintPage 事件,直到 HasMorePages 为 false。 当文档已完成呈现时,将字符串重置为已呈现。 此外,确保 PrintPage 事件与其事件处理方法关联。

注意

如果已在应用程序中实现打印,你可能已完成了步骤 5 和 6。

在下列代码示例中,事件处理程序用于打印“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; } 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 ' If there are no more pages, reset the string to be printed. If Not e.HasMorePages Then stringToPrint = documentContents End If End Sub

在窗体上,将 Document 控件的 PrintPreviewDialog 属性设置为 PrintDocument 组件。

printPreviewDialog1.Document = printDocument1; PrintPreviewDialog1.Document = PrintDocument1

调用 ShowDialog 控件上的 PrintPreviewDialog 方法。 请注意下面提供的突出显示的代码,通常从按钮的 Click 事件处理方法调用 ShowDialog。 调用 ShowDialog 会引发 PrintPage 事件,并将输出呈现到 PrintPreviewDialog 控件。 当用户选择对话框上的打印按钮时,会再次引发 PrintPage 事件,将输出发送至打印机而不是预览对话框。 因此,在步骤 4 中,该字符串会在呈现过程结尾重置。

下列代码示例显示了窗体上一个按钮的 Click 事件处理方法。 此事件处理方法调用方法来读取文档,并显示打印预览对话框。

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); printPreviewDialog1.Document = printDocument1; printPreviewDialog1.ShowDialog(); } Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim docName As String = "testPage.txt" Dim docPath As String = "C:\Users\v-rsatao\Desktop\" Dim fullPath As String = System.IO.Path.Combine(docPath, docName) PrintDocument1.DocumentName = docName stringToPrint = System.IO.File.ReadAllText(fullPath) PrintPreviewDialog1.Document = PrintDocument1 PrintPreviewDialog1.ShowDialog() End Sub


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有