Solved

Multiple Page Printing - **URGENT**

Posted on 2007-03-18
1
192 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

744 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

16 Experts available now in Live!

Get 1:1 Help Now