使用英语阅读

通过


如何:在 Windows 窗体中打印多页文本文件

基于 Windows 的应用程序非常常见地打印文本。 Graphics 类为设备(如屏幕或打印机)提供绘制对象(图形或文本)的方法。

备注

打印不支持 TextRendererDrawText 方法。 应始终使用 GraphicsDrawString 方法绘制文本进行打印,如以下代码示例所示。

打印文本

  1. 向窗体添加 PrintDocument 组件和字符串。

    private PrintDocument printDocument1 = new PrintDocument();
    private string stringToPrint;
    
  2. 如果要打印文档,请将 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();
    }
    
  3. PrintPage 事件处理程序中,使用 PrintPageEventArgs 类的 Graphics 属性和文档内容来计算每页的行长度和行数。 绘制完每个页面后,检查它是否是最后一页,然后相应地设置 PrintPageEventArgsHasMorePages 属性。 会持续引发 PrintPage 事件,直到 HasMorePagesfalse。 此外,请确保 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);
    }
    
  4. 调用 Print 方法以引发 PrintPage 事件。

    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());
        }
    }
}

编译代码

此示例需要:

  • 一个名为 testPage.txt 的文本文件,其中包含要打印的文本,位于驱动器 C:\的根目录中。 编辑代码以打印其他文件。

  • 对 System、System.Windows.Forms、System.Drawing 程序集的引用。

  • 有关如何使用命令行从 Visual Basic 或 Visual C# 构建此示例的信息,请参阅 从命令行构建使用 csc.exe进行命令行构建。 还可以通过将代码粘贴到新项目中,在 Visual Studio 中生成此示例。

另请参阅