Hi, I have vb.net application
In the form there are 2 RichtextBox.
To print the data in richtextbox I am using e.graphics method.
This is my code to print them....
The size of printing Richtextbox are fixed..
If Richtextbox1 has more data than the print area , then print does show more data.
So if possible that if RichTextBox1 has more data then it automatically shift down the RichTextBox2.OR as long as the user enter the data , the RichtextBox2 shifts automatically down....
Please help....

 Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

        Dim font1 As Font = New Drawing.Font("Arial", 11, FontStyle.Bold)
        Dim font2 As Font = New Drawing.Font("Microsoft Sans Serif", 10)

        Dim formatter As StringFormat = New StringFormat()
        formatter.LineAlignment = StringAlignment.Near
        formatter.Alignment = StringAlignment.Near

          Dim rectangle4 As RectangleF = New RectangleF(150, 340, 600, 60)
        e.Graphics.DrawString(Me.RichTextBox1.Text, font1, Brushes.Black, rectangle4, formatter)

          Dim rectangle5 As RectangleF = New RectangleF(150, 740, 600, 60)
        e.Graphics.DrawString(Me.RichTextBox2.Text, font1, Brushes.Black, rectangle5, formatter)

        End sub

Recommended Answers

All 5 Replies

Dear Shark 1,

what is the meaning of
"sr = New StreamReader("D:\My Documents\History of the Amiga.txt")"

in the btnPrint event of the tutorial?

I want RichTextBox data to print. If RichTextBox1 has less data then RichTextBox should automatically adjust its area of printing.
Can this possible in e.graphics.

In that tutorial it reads a text file and prints the content of that file.
I want say that the conseption to read and print text from a TextBox or RichTextBox into a PrintDocument are almost same.
I just give you an most common example how you can print a long plain text into a printdocument

Private Sub PD PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PD.PrintPage
    Static currentChar As Integer
    Static currentLine As Integer
    Dim txtFont As Font = TextBox1.Font
    Dim txtH, txtW As Integer
    Dim LMargin, TMargin As Integer

    ' Calculate the dimensions of the printable area of the page
    With PD.DefaultPageSettings
        txtH = .PaperSize.Height - .Margins.Top - .Margins.Bottom
        txtW = .PaperSize.Width - .Margins.Left - .Margins.Right
        LMargin = PD.DefaultPageSettings.Margins.Left
        TMargin = PD.DefaultPageSettings.Margins.Top
    End With

    New Rectangle(LMargin, TMargin, txtW, txtH))
    ' If the text is printed sideways, swap the printable area’s
    ' width and height
    If PD.DefaultPageSettings.Landscape Then
        Dim tmp As Integer
        tmp = txtH
        txtH = txtW
        txtW = tmp
    End If

    ' Calculate the number of lines per page
    Dim linesperpage As Integer = CInt(Math.Round(txtH / txtFont.Height))

    ' R is the rectangle in which the text should fit
    Dim R As New RectangleF(LMargin, TMargin, txtW, txtH)
    Dim fmt As StringFormat
    If Not TextBox1.WordWrap Then
        fmt = New StringFormat(StringFormatFlags.NoWrap)
        fmt.Trimming = StringTrimming.EllipsisWord
        Dim i As Integer
        For i = currentLine To Math.Min(currentLine + linesperpage, TextBox1.Lines.Length - 1)
            e.Graphics.DrawString(TextBox1.Lines(i), txtFont, Brushes.Black, New RectangleF(LMargin,TMargin + txtFont.Height * (i - currentLine),txtW, txtFont.Height), fmt)
        currentLine += linesperpage
        If currentLine >= TextBox1.Lines.Length Then
            e.HasMorePages = False
            currentLine = 0
        e.HasMorePages = True
        End If
        Exit Sub
    End If
    fmt = New StringFormat(StringFormatFlags.LineLimit)
    Dim lines, chars As Integer
    e.Graphics.MeasureString(Mid(TextBox1.Text, currentChar + 1), txtFont, New SizeF(txtW, txtH), fmt, chars, lines)
    If currentChar + chars < TextBox1.Text.Length Then
        If TextBox1.Text.Substring(currentChar + chars, 1) <> ” ” And TextBox1.Text.Substring(currentChar + chars, 1) <> vbLf Then
            While chars > 0 AndAlso TextBox1.Text.Substring (currentChar + chars, 1)<> ” AndAlso TextBox1.Text.Substring(currentChar + chars, 1) <> vbLf
                chars -= 1
            End While
            chars += 1
        End If
    End If
    e.Graphics.DrawString(TextBox1.Text.Substring(currentChar, chars),
    txtFont, Brushes.Black, R, fmt)
    currentChar = currentChar + chars
    If currentChar < TextBox1.Text.Length Then
        e.HasMorePages = True
        e.HasMorePages = False
        currentChar = 0
    End If
End Sub

Hope it can help you.

Let me try on this.

I am not getting from above code.....Please

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.