Hello Guys,
Simlple problem and I am hoping for a simple solution!
My software saves all my randomly generated product keys to a text file. It saves this using the code below.

 My.Computer.FileSystem.WriteAllText(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\TempValuesForAKG.txt", PTB.Text, True)

That works fine. I then try to use the Print Document control and display it with the Print Preview Dialog and it does indeed display the data, I am using the code below.

  e.Graphics.DrawString(My.Computer.FileSystem.ReadAllText(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\TempValuesForAKG.txt"), Font, Brushes.Black, 50, 50)

That is in the PrintDocument_PrintPage part and it works - the data is displayed. However, if the document is too long it doesn't go onto multiple pages. How can I get it to do this?
Sorry for a drawn out question but I thought some of the information might be relevant!
Cheers guys,
Luke

Dani AI

Generated

Brief diagnosis: the PrintPage handler is drawing the whole file at one fixed coordinate so the graphics surface simply clips the rest of the text. The PrintDocument model expects the handler to draw one page's worth of text and set e.HasMorePages = True when more remains. 's pointer to MSDN and 's tutorial are on the right track; below is a compact, practical pattern that keeps printing state between pages and wraps text to the printable rectangle.

' form-level fields
Private fileText As String
Private filePos As Integer

Private Sub PrintDocument1_BeginPrint(sender As Object, e As Printing.PrintEventArgs) Handles PrintDocument1.BeginPrint
    Dim filePath As String = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "TempValuesForAKG.txt")
    fileText = IO.File.ReadAllText(filePath)   ' or use a stream for very large files
    filePos = 0
End Sub

Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    Dim f As New Font("Arial", 10)
    Dim layout As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
    Dim charsFitted As Integer = 0
    Dim linesFilled As Integer = 0

    Dim remaining As String = fileText.Substring(filePos)
    e.Graphics.MeasureString(remaining, f, layout.Size, StringFormat.GenericDefault, charsFitted, linesFilled)
    Dim pageText As String = remaining.Substring(0, Math.Max(0, charsFitted))
    e.Graphics.DrawString(pageText, f, Brushes.Black, layout)

    filePos += charsFitted
    e.HasMorePages = (filePos < fileText.Length)
End Sub

Private Sub PrintDocument1_EndPrint(sender As Object, e As Printing.PrintEventArgs) Handles PrintDocument1.EndPrint
    fileText = Nothing
End Sub

Quick tips and caveats: 1) Use e.MarginBounds (not hard-coded 50,50) so margins and preview match printer settings. 2) Initialize/read the file once in BeginPrint — do not call ReadAllText inside PrintPage (it resets state). 3) For huge files, stream with StringReader to avoid high memory use. 4) MeasureString is convenient but not pixel-perfect for complex wrapping; for tighter control use line-by-line measuring or StringFormat flags. This pattern will produce multiple pages in PrintPreview and actual printing.

Recommended Answers

All 2 Replies

The example on this page shows how to print multiple pages.

Follow this tutorial in vb.net forum of Daniweb, which can give you a proper way of thinking.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.