Solved

Multiple Page Printing - **URGENT**

Posted on 2007-03-18
1
197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 63

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

734 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