CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Printing RTFbox

  1. #1
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Printing RTFbox

    Anyone knows of a good routine to print the content of an richtextbox? The problem I'm encountring is handling different font sizes.
    Any examples/suggestions welcome.
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  2. #2
    Join Date
    Feb 2003
    Location
    UK
    Posts
    19

    Need to to this as well

    I cannot answer your question but I am also trying to do this. Did you find out the solution?

    Russ

  3. #3
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Printing RTFbox

    Originally posted by Cakkie
    Anyone knows of a good routine to print the content of an richtextbox? The problem I'm encountring is handling different font sizes.
    Any examples/suggestions welcome.
    Hi Tom.
    Here is the code how open file to RTB and then print it

    Code:
    'Open file to RTB and print RTB
    
    'To print multiple pages we have to create virtual page of text called PrintPage and then add text to it
    'until page is full
    'Controls: OpenFileDialog1, PrintDocument1, PrintDialog1
    
    ------------------------------------------------------------------------------------------------------
    Imports System.IO  'for FileStream class
    Imports System.Drawing.Printing
    
    Public Class Form1
        Inherits System.Windows.Forms.Form
    
    #Region " Windows Form Designer generated code "
    #End Region
    
        Private PrintPageSettings As New PageSettings()
        Private StringToPrint As String
        Private PrintFont As New Font("Arial", 10)
    
        Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
            Dim FilePath As String
            'Display Open dialog box and select text file
            OpenFileDialog1.Filter = "Text files (*.txt)|*.txt"
            OpenFileDialog1.ShowDialog()
            'If Cancel button not selected, load FilePath variable
            If OpenFileDialog1.FileName <> "" Then
                FilePath = OpenFileDialog1.FileName
                Try
                    'Read text file and load into RichTextBox1
                    Dim MyFileStream As New FileStream(FilePath, FileMode.Open)
                    RichTextBox1.LoadFile(MyFileStream, _
                      RichTextBoxStreamType.PlainText)
                    MyFileStream.Close()
                    'Initialize string to print
                    StringToPrint = RichTextBox1.Text
                    'Enable Print button
                    btnPrint.Enabled = True
                Catch ex As Exception
                    'display error messages if they appear
                    MessageBox.Show(ex.Message)
                End Try
            End If
        End Sub
    
        Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
            Try
                'Specify current page settings
                PrintDocument1.DefaultPageSettings = PrintPageSettings
                'Specify document for print dialog box and show
                StringToPrint = RichTextBox1.Text
                PrintDialog1.Document = PrintDocument1
                Dim result As DialogResult = PrintDialog1.ShowDialog()
                'If click OK, print document to printer
                If result = DialogResult.OK Then
                    PrintDocument1.Print()
                End If
            Catch ex As Exception
                'Display error message
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
        Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Dim numChars As Integer
            Dim numLines As Integer
            Dim stringForPage As String
            Dim strFormat As New StringFormat()
            'Based on page setup, define drawable rectangle on page
            Dim rectDraw As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, _
              e.MarginBounds.Width, e.MarginBounds.Height)
            'Define area to determine how much text can fit on a page
            'Make height one line shorter to ensure text doesn't clip
            Dim sizeMeasure As New SizeF(e.MarginBounds.Width, _
              e.MarginBounds.Height - PrintFont.GetHeight(e.Graphics))
    
            'When drawing long strings, break between words
            strFormat.Trimming = StringTrimming.Word
            'Compute how many chars and lines can fit based on sizeMeasure
            e.Graphics.MeasureString(StringToPrint, PrintFont, sizeMeasure, strFormat, numChars, numLines)
            'Compute string that will fit on a page
            stringForPage = StringToPrint.Substring(0, numChars)
            'Print string on current page
            e.Graphics.DrawString(stringForPage, PrintFont, Brushes.Black, rectDraw, strFormat)
            'If there is more text, indicate there are more pages
            If numChars < StringToPrint.Length Then
                'Subtract text from string that has been printed
                StringToPrint = StringToPrint.Substring(numChars)
                e.HasMorePages = True
            Else
                e.HasMorePages = False
                'All text has been printed, so restore string
                StringToPrint = RichTextBox1.Text
            End If
        End Sub
    End Class
    Iouri Boutchkine
    [email protected]

  4. #4
    Join Date
    Feb 2003
    Location
    UK
    Posts
    19

    That's not the solution were looking for...

    Iouri

    Thats just prints everything in 10pt Ariel. It doesn't handle the rich text correctly. As you know, the richtextbox can have text in multiple fonts and multiple sizes. This printing funtion just overrides all that formatting and makes in all the same. OK if you printing from a multiline textbox but not what you want from a richtextbox. The code below is what I'm currently using but it kind of makes using a rtb pointless.

    Russ


    Code:
        Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
            PrintDocument()
        End Sub
        Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreview.Click
            prnPreview.Document = prnDoc
            prnPreview.ShowDialog()
        End Sub
        Private Sub prnDoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles prnDoc.PrintPage
            e.Graphics.DrawString(rtbReport.Text, rtbReport.Font, Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top)
        End Sub 'prnDoc_PrintPage
        Private Sub PrintDocument()
            prnDoc.DocumentName = "data report"
            prnDialog.Document = prnDoc
    
            If prnDialog.ShowDialog = DialogResult.OK Then
                prnDoc.Print()
            End If
        End Sub   ' PrintDocument

  5. #5
    Join Date
    Jun 2003
    Location
    Gent
    Posts
    7
    I used this in VB6, don't know to what extend it works or needs modifications to work in .Net (does SelPrint still exist?)...

    Code:
           
        CommonDialog1.Flags = cdlPDReturnDC + cdlPDNoPageNums
        If RichTextBox1.SelLength = 0 Then
           CommonDialog1.Flags = CommonDialog1.Flags + cdlPDAllPages
        Else
           CommonDialog1.Flags = CommonDialog1.Flags + cdlPDSelection
        End If
        CommonDialog1.ShowPrinter
        Printer.Print ""
        RichTextBox1.SelPrint CommonDialog1.hDC
    De groeten

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

Featured