Results 1 to 6 of 6

Thread: print multiple pages based off size of file.

  1. #1

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    print multiple pages based off size of file.

    Hey guys,

    I have a question regarding multiple page printing. I know that it has to do with e.HasMorePages. I know that there has to be a condition that sets that flag to true and back to false or you get caught in a never ending print page loop. However, I have no idea where in my simple little print block here to implement that. Can someone give me a suggestion?

    As you can see I'm just printing a readline. Sometimes the file extends past a single page though depending on circumstances.

    Code:
            Dim strReader As StreamReader
            strReader = File.OpenText("auditlog.txt")
    
            Dim x As Integer = 10
            Dim y As Integer = 10
    
    
    
            Do While strReader.Peek <> -1
                e.Graphics.DrawString(strReader.ReadLine, New Font("Times New Roman", _
                                                                  10, FontStyle.Regular), Brushes.Black, x, y)
                y += 12
    
            Loop
    Thanks guys
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: print multiple pages based off size of file.

    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private printString As String
    4.  
    5.     Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    6.         Dim charsFitted As Integer
    7.         Dim linesFilled As Integer
    8.         e.Graphics.MeasureString(printString, New Font("Times New Roman", 10, FontStyle.Regular), e.PageBounds.Size, Drawing.StringFormat.GenericTypographic, charsFitted, linesFilled)
    9.         e.Graphics.DrawString(printString, New Font("Times New Roman", 10, FontStyle.Regular), Brushes.Black, e.PageBounds.Left, e.PageBounds.Top)
    10.         printString = printString.Substring(charsFitted)
    11.         If printString <> "" Then
    12.             e.HasMorePages = True
    13.         Else
    14.             e.HasMorePages = False
    15.         End If
    16.     End Sub
    17.  
    18.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    19.         printString = io.file.readalltext("auditlog.txt")
    20.         PrintDocument1.Print()
    21.     End Sub
    22.  
    23. End Class

  3. #3

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Re: print multiple pages based off size of file.

    where are charsFitted and linesFilled populated at? I'm trying to implement this and not having a lot of luck. I tried to merge the code together. This is what I have.

    Code:
            Dim strReader As StreamReader
            strReader = File.OpenText("auditlog.txt")
            Dim moreLines As String = ""
            Dim x As Integer = 10
            Dim y As Integer = 10
    
    
    
            Do While strReader.Peek <> -1
                e.Graphics.MeasureString(strReader.ReadLine, New Font("Times New Roman", 10, FontStyle.Regular _
                                                                      ), e.PageBounds.Size, Drawing.StringFormat.GenericTypographic _
                                                                      , x, y)
                e.Graphics.DrawString(strReader.ReadLine, New Font("Times New Roman", _
                                                                  10, FontStyle.Regular), Brushes.Black, x, y)
                y += 12
                moreLines = moreLines.Substring(x)
    
                If moreLines <> "" Then
                    e.HasMorePages = True
                Else
                    e.HasMorePages = False
                End If
                moreLines = ""
            Loop
    I am starting to get the idea that the e.HasMorePages can't be in a loop at all.... Just a hunch, lol.
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: print multiple pages based off size of file.

    you have to pass the whole of the text file, then truncate it after printing a page, depending on charsFitted which along with linesFilled (which isn't used in my example) are passed in the measurestring arguments byref so they return a value

  5. #5

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Re: print multiple pages based off size of file.

    Ok, I got it to where the charsFitted and linesFilled are populated now its just looping. Do I need to trim off what is printed before checking if charsfitted <> "" again?

    Code:
            Dim strReader As StreamReader
            strReader = File.OpenText("auditlog.txt")
            Dim docLines As String = ""
            Dim x As Integer = 10
            Dim y As Integer = 10
            Dim charsFitted As Integer
            Dim linesFilled As Integer
    
            Do While strReader.Peek <> -1
                docLines = docLines & strReader.ReadLine & vbCrLf
            Loop
            strReader.Close()
    
            MsgBox(docLines)
            e.Graphics.MeasureString(docLines, New Font("Times New Roman", 10, FontStyle.Regular _
                                                                            ), e.PageBounds.Size, Drawing.StringFormat.GenericTypographic _
                                                                            , charsFitted, linesFilled)
            e.Graphics.DrawString(docLines, New Font("Times New Roman", _
                                                                         10, FontStyle.Regular), Brushes.Black, e.PageBounds.Left, _
                                                                         e.PageBounds.Top)
            docLines = docLines.Substring(charsFitted)
    
            If docLines <> "" Then
                e.HasMorePages = True
            Else
                e.HasMorePages = False
            End If
    
    
        End Sub
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: print multiple pages based off size of file.

    the way you've got it it loads the whole file into docLines every time the printPage sub runs, so it'll loop forever.

    when you set e.hasmorepages = true, what happens is that after exiting the printPage sub it runs the printPage sub again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width