• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

Multiple Page Printing - **URGENT**


I have a problem that needs sorting really quickly as I am due to have things completed by tonight.  I know it's probably been answered before and I have just spent 25 minutes looking it up but gave up because I don't really have time to go sifting through other people questions.  I am using VB 2005 Express and have a text box that I need to print, I have successfuly writtent code to print the first page but I need to make sure that if the text overuns onto a second page this also gets printed.  I have looked at MS online help and to be honest it was just totally confusing.  Below is the code I have so far :

    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        'counts header length
        Dim textcount As Integer = Len(My.Computer.FileSystem.ReadAllTex ("C:\dobtxtfiles\CompanyName.txt"))
        Dim textcount2 As Integer = Len(My.Computer.FileSystem.ReadAllText("C:\dobtxtfiles\SiteNumber.txt"))
        Dim textcount3 As Integer = Len(" Site - ")
        Dim totaltext As Integer = textcount + textcount2 + textcount3
        'finds centre of page
        Dim centre As Integer = ((88 - totaltext) \ 2) * 10
        Dim textcountline2 As Integer = Len(My.Computer.FileSystem.ReadAllText("C:\dobtxtfiles\SiteAddy.txt"))
        Dim centreline2 As Integer = ((90 - textcountline2) \ 2) * 10
        'sets printer fontstyle i.e bold, italic
        Dim instance As New FontStyle
        'set 3 printer fonts for certan part of the printout
        Dim font As New Font("Tamoha", 10, FontStyle.Regular)
        Dim font2 As New Font("Tamoha", 10, FontStyle.Bold)
        Dim font3 As New Font("Courier New", 10, FontStyle.Bold)
        'prints a 2 line header based on info stored in txt files
        e.Graphics.DrawString(My.Computer.FileSystem.ReadAllText("C:\dobtxtfiles\CompanyName.txt") & " Site - " & My.Computer.FileSystem.ReadAllText("C:\dobtxtfiles\SiteNumber.txt"), font3, Brushes.Black, centre, 20)
        e.Graphics.DrawString(My.Computer.FileSystem.ReadAllText("C:\dobtxtfiles\SiteAddy.txt"), font3, Brushes.Black, centreline2, 40)
        'sets the lines to print
        e.Graphics.DrawString(TextBox1.Text, font2, Brushes.Black, 20, 100)
        'prints the date and time printed as a footer
        e.Graphics.DrawString("Printed On: " & My.Computer.Clock.LocalTime.ToString, font3, Brushes.Black, 290, 1100)
    End Sub

The code basically sets a header of the company name and address and that is created from 2 text files, this is centred on the screen and then text from a text box needs to be printed.  Everything works fine for the first page but I can't get a second page to print.

Any help would be reaally great as I need this as soon as possible.

1 Solution
Fernando SotoRetiredCommented:
Hi Michelle;

When you print the TextBox1 text to the graphics object you will need to determine if you have enough space to do so. If you do then only one page is needed and all is well. If TextBox1 text needs more space then you have then only print what you have room for and keep an index to the next character to be printed from the TextBox1 object and set e.HasMorePages to true. This function will then be called again to print the next page. When the next page starts printing, start printing the text in TextBox1 from where you left off from the last time, setting HasMorePages as needed as well as the next position to be printed.

I would also use the following to prevent from creating multiple times the font and headers

    'set 3 printer fonts for certan part of the printout
    Private font As Font
    Private font2 As Font
    Private font3 As Font

    Private Sub PrintDocument1_BeginPrint(ByVal sender As Object, _
        ByVal e As System.Drawing.Printing.PrintEventArgs) _
        Handles PrintDocument1.BeginPrint

        'sets printer fontstyle i.e bold, italic
        Dim instance As New FontStyle
        'set 3 printer fonts for certan part of the printout
        font = New Font("Tamoha", 10, FontStyle.Regular)
        font2 = New Font("Tamoha", 10, FontStyle.Bold)
        font3 = New Font("Courier New", 10, FontStyle.Bold)

    End Sub

    Private Sub PrintDocument1_EndPrint(ByVal sender As Object, _
        ByVal e As System.Drawing.Printing.PrintEventArgs) _
        Handles PrintDocument1.EndPrint

        font = Nothing
        font2 = Nothing
        font3 = Nothing

    End Sub


Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now