Print a multi-page text file (Windows Forms .NET)
It's common for Windows-based applications to print text. The Graphics class provides methods for drawing objects (graphics or text) to a device, such as a screen or printer. The following section describes in detail the process to print text file. This method doesn't support printing non-plain text files, such as an Office Word document or a PDF file.
Note
The DrawText methods of TextRenderer are not supported for printing. You should always use the DrawString methods of Graphics, as shown in the following code example, to draw text for printing purposes.
To print text
In Visual Studio, double-click the form you want to print from, in the Solution Explorer pane. This opens the Visual Designer.
From the Toolbox, double-click the PrintDocument component to add it to the form. This should create a
PrintDocument
component with the nameprintDocument1
.Either add a
Button
to the form, or use a button that is already on the form.In the Visual Designer of the form, select the button. In the Properties pane, select the Event filter button and then double-click the
Click
event to generate an event handler.The
Click
event code should be visible. Outside the scope of the event handler, add a private string variable to the class namedstringToPrint
.private string stringToPrint="";
'Private PrintDocument1 As New PrintDocument() Private stringToPrint As String
Back in the
Click
event handler code, set the DocumentName property to the name of the document. This information is sent to the printer. Next, read the document text content and store it in thestringToPrint
string. Finally, call the Print method to raise the PrintPage event. ThePrint
method is highlighted below.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); printDocument1.Print(); }
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim docName As String = "testPage.txt" Dim docPath As String = "C:\" Dim fullPath As String = System.IO.Path.Combine(docPath, docName) PrintDocument1.DocumentName = docName stringToPrint = System.IO.File.ReadAllText(fullPath) PrintDocument1.Print() End Sub
Go back to the Visual Designer of the form and select the
PrintDocument
component. On the Properties pane, select the Event filter and then double-click thePrintPage
event to generate an event handler.In the PrintPage event handler, use the Graphics property of the PrintPageEventArgs class and the document contents to calculate line length and lines per page. After each page is drawn, check if it's the last page, and set the HasMorePages property of the
PrintPageEventArgs
accordingly. ThePrintPage
event is raised untilHasMorePages
isfalse
.In the following code example, the event handler is used to print the contents of the "testPage.txt" file in the same font as it's used on the form.
private void PrintDocument1_PrintPage(object sender, System.Drawing.Printing.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 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 End Sub
See also
.NET Desktop feedback