Solved

Multiple Page Printing - **URGENT**

Posted on 2007-03-18
1
194 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:Michelle_Pitter
1 Comment
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 18743486
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now