Multiple Page Printing - **URGENT**

Hi,

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.

Michelle
Michelle_PitterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Fernando
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.