Click here to Skip to main content
15,889,876 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Currently i can print lines of text from a file using the PrintDocument object. However the problem I am facing is that if the text on a line is wider than the width of the page, the text is cut off.

here is the code in my PrintPage event:
Private Sub objprintDocument_Printpage(ByVal sender As Object, _
                ByVal e As System.Drawing.Printing.PrintPageEventArgs)

        'declare variables
        Dim sngLinesPerPage As Single = 0
        Dim sngVerticalPosition As Single = 0
        Dim intLineCount As Integer = 0
        Dim sngLeftMargin As Single = e.MarginBounds.Left
        Dim sngTopMargin As Single = e.MarginBounds.Top
        Dim strLine As String

        'workout the number of lines per page
       'use margin bounds on the event to do this
        sngLinesPerPage = e.MarginBounds.Height / objPrintFont.GetHeight(e.Graphics)

        'now iterate through the file printing out each line
        'This assumes that a single line is not wider than the page
        'width. Check intLineCount first so that we donot read a line
       'we don't want to print
        strLine = objStreamToPrint.ReadLine()

        While (intLineCount < sngLinesPerPage And Not (strLine Is Nothing))
            'calculate the vertical position on the page
            sngVerticalPosition = sngTopMargin + (intLineCount * objPrintFont.GetHeight(e.Graphics))

            'pass a stringFormat to drawstring for the print preview control
            e.Graphics.DrawString(strLine, objPrintFont, Brushes.Black, _
            sngLeftMargin, sngVerticalPosition, New StringFormat)

            'increment line count
            intLineCount += 1

            'if the line count is less than number of lines
            'per page then read another line
            If intLineCount < sngLinesPerPage Then
                strLine = objStreamToPrint.ReadLine
            End If
        End While

        'if we have more lines then print another page
        'If we have more lines then print another page
        If (strLine <> Nothing) Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
        End If
    End Sub


Thanks for your help
Posted
Updated 28-Aug-13 5:20am
v2

1 solution

Check for a line longer than the page width and print the remaining text on the next line(s). There is no automatic text wrapping to the next line.


This example has not been tested.
Note: This example does not handle the case of an extremely long line that would overflow the end of the page and should cause a new page.
VB
    Private Sub objprintDocument_Printpage(ByVal sender As Object, _
            ByVal e As System.Drawing.Printing.PrintPageEventArgs)

    'declare variables
    Dim sngLinesPerPage As Single = 0
    Dim sngVerticalPosition As Single = 0
    Dim intLineCount As Integer = 0
    Dim sngLeftMargin As Single = e.MarginBounds.Left
    Dim sngTopMargin As Single = e.MarginBounds.Top
    Dim strLine As String

    'workout the number of lines per page
    'use margin bounds on the event to do this
    sngLinesPerPage = e.MarginBounds.Height / objPrintFont.GetHeight(e.Graphics)

    'now iterate through the file printing out each line
    'This assumes that a single line is not wider than the page
    'width. Check intLineCount first so that we donot read a line
    'we don't want to print
    strLine = objStreamToPrint.ReadLine()

    While (intLineCount < sngLinesPerPage And Not (strLine Is Nothing))
        'calculate the vertical position on the page
        sngVerticalPosition = sngTopMargin + (intLineCount * objPrintFont.GetHeight(e.Graphics))

        'pass a stringFormat to drawstring for the print preview control
        Const intLineWidth As Integer = 90
        While strLine.Length > 0 ' Assuming 90 characters fit on a line
            e.Graphics.DrawString( _
                strLine.Substring(0, Math.Min(strLine.Length, intLineWidth)), _
                objPrintFont, Brushes.Black, _
                sngLeftMargin, sngVerticalPosition, New StringFormat)
            'increment line count
            intLineCount += 1
            If strLine.Length = intLineWidth Then
                strLine = ""
            Else
                ' Remainder of line that is not yet printed
                strLine = strLine.Substring(intLineWidth)
             End If
        End While

        'if the line count is less than number of lines
        'per page then read another line
        If intLineCount < sngLinesPerPage Then
            strLine = objStreamToPrint.ReadLine
        End If
    End While

    'if we have more lines then print another page
    'If we have more lines then print another page
    If (strLine <> Nothing) Then
        e.HasMorePages = True
    Else
        e.HasMorePages = False
    End If
End Sub
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900