Skip to content

Latest commit

 

History

History
74 lines (57 loc) · 5.52 KB

File metadata and controls

74 lines (57 loc) · 5.52 KB
title How to: Print a Multi-Page Text File in Windows Forms
ms.custom
ms.date 03/30/2017
ms.prod .net-framework
ms.reviewer
ms.suite
ms.technology
dotnet-winforms
ms.tgt_pltfrm
ms.topic article
dev_langs
csharp
vb
helpviewer_keywords
printing [Windows Forms], printing multiple pages
text [Windows Forms], printing Windows Forms
Windows Forms, printing text
printing [Windows Forms], text
ms.assetid 362427f8-03d4-4826-b49f-60ab066ad322
caps.latest.revision 24
author dotnet-bot
ms.author dotnetcontent
manager wpickett
ms.workload
dotnet

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:

See Also

xref:System.Drawing.Graphics
xref:System.Drawing.Brush
Windows Forms Print Support