Files
docs-desktop/dotnet-desktop-guide/framework/winforms/advanced/how-to-print-a-multi-page-text-file-in-windows-forms.md

4.8 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Print a Multi-Page Text File 03/30/2017
csharp
vb
printing [Windows Forms], printing multiple pages
text [Windows Forms], printing Windows Forms
Windows Forms, printing text
printing [Windows Forms], text
362427f8-03d4-4826-b49f-60ab066ad322

How to: Print a Multi-Page Text File in Windows Forms

It is very common for Windows-based applications to print text. The xref:System.Drawing.Graphics class provides methods for drawing objects (graphics or text) to a device, such as a screen or printer.

Note

The xref:System.Windows.Forms.TextRenderer.DrawText%2A methods of xref:System.Windows.Forms.TextRenderer are not supported for printing. You should always use the xref:System.Drawing.Graphics.DrawString%2A methods of xref:System.Drawing.Graphics, as shown in the following code example, to draw text for printing purposes.

To print text

  1. Add a xref:System.Drawing.Printing.PrintDocument component and a string to your form.

    [!code-csharpSystem.Drawing.Printing.PrintExamples#8] [!code-vbSystem.Drawing.Printing.PrintExamples#8]

  2. If printing a document, set the xref:System.Drawing.Printing.PrintDocument.DocumentName%2A property to the document you wish to print, and open and read the documents contents to the string you added previously.

    [!code-csharpSystem.Drawing.Printing.PrintExamples#1] [!code-vbSystem.Drawing.Printing.PrintExamples#1]

  3. In the xref:System.Drawing.Printing.PrintDocument.PrintPage event handler, use the xref:System.Drawing.Printing.PrintPageEventArgs.Graphics%2A property of the xref:System.Drawing.Printing.PrintPageEventArgs class and the document contents to calculate line length and lines per page. After each page is drawn, check to see if it is the last page, and set the xref:System.Drawing.Printing.PrintPageEventArgs.HasMorePages%2A property of the xref:System.Drawing.Printing.PrintPageEventArgs accordingly. The xref:System.Drawing.Printing.PrintDocument.PrintPage event is raised until xref:System.Drawing.Printing.PrintPageEventArgs.HasMorePages%2A is false. Also, make sure the xref:System.Drawing.Printing.PrintDocument.PrintPage event is associated with its event-handling method.

    In the following code example, the event handler is used to print the contents of the "testPage.txt" file in the same font as is used on the form.

    [!code-csharpSystem.Drawing.Printing.PrintExamples#2] [!code-vbSystem.Drawing.Printing.PrintExamples#2]

  4. Call the xref:System.Drawing.Printing.PrintDocument.Print%2A method to raise the xref:System.Drawing.Printing.PrintDocument.PrintPage event.

    [!code-csharpSystem.Drawing.Printing.PrintExamples#5] [!code-vbSystem.Drawing.Printing.PrintExamples#5]

Example

[!code-csharpSystem.Drawing.Printing.PrintExamples#0] [!code-vbSystem.Drawing.Printing.PrintExamples#0]

Compiling the Code

This example requires:

  • A text file named testPage.txt containing the text to print, located in the root of drive C:\. Edit the code to print a different file.

  • References to the System, System.Windows.Forms, System.Drawing assemblies.

  • For information about building this example from the command line for Visual Basic or Visual C#, see Building from the Command Line or Command-line Building With csc.exe. You can also build this example in Visual Studio by pasting the code into a new project.

See also