--- title: "How to: Print a Multi-Page Text File" ms.date: "03/30/2017" 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 --- # How to: Print a Multi-Page Text File in Windows Forms It is very common for Windows-based applications to print text. The class provides methods for drawing objects (graphics or text) to a device, such as a screen or printer. > [!NOTE] > The methods of are not supported for printing. You should always use the methods of , as shown in the following code example, to draw text for printing purposes. ### To print text 1. Add a component and a string to your form. [!code-csharp[System.Drawing.Printing.PrintExamples#8](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.Printing.PrintExamples/CS/Form1.cs#8)] [!code-vb[System.Drawing.Printing.PrintExamples#8](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.Printing.PrintExamples/VB/Form1.vb#8)] 2. If printing a document, set the property to the document you wish to print, and open and read the documents contents to the string you added previously. [!code-csharp[System.Drawing.Printing.PrintExamples#1](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.Printing.PrintExamples/CS/Form1.cs#1)] [!code-vb[System.Drawing.Printing.PrintExamples#1](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.Printing.PrintExamples/VB/Form1.vb#1)] 3. In the event handler, use the property of the 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 property of the accordingly. The event is raised until is `false`. Also, make sure the 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-csharp[System.Drawing.Printing.PrintExamples#2](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.Printing.PrintExamples/CS/Form1.cs#2)] [!code-vb[System.Drawing.Printing.PrintExamples#2](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.Printing.PrintExamples/VB/Form1.vb#2)] 4. Call the method to raise the event. [!code-csharp[System.Drawing.Printing.PrintExamples#5](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.Printing.PrintExamples/CS/Form1.cs#5)] [!code-vb[System.Drawing.Printing.PrintExamples#5](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.Printing.PrintExamples/VB/Form1.vb#5)] ## Example [!code-csharp[System.Drawing.Printing.PrintExamples#0](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.Printing.PrintExamples/CS/Form1.cs#0)] [!code-vb[System.Drawing.Printing.PrintExamples#0](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.Printing.PrintExamples/VB/Form1.vb#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](/dotnet/visual-basic/reference/command-line-compiler/building-from-the-command-line) or [Command-line Building With csc.exe](/dotnet/csharp/language-reference/compiler-options/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 - - - [Windows Forms Print Support](windows-forms-print-support.md)