Cakkie
May 24th, 2002, 06:24 AM
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.
Any examples/suggestions welcome.
|
Click to See Complete Forum and Search --> : Printing RTFbox Cakkie May 24th, 2002, 06:24 AM 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. russgreen February 26th, 2003, 11:56 AM I cannot answer your question but I am also trying to do this. Did you find out the solution? Russ Iouri February 26th, 2003, 06:59 PM 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 '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 russgreen February 26th, 2003, 07:21 PM 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 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 Mendel June 10th, 2003, 08:09 AM I used this in VB6, don't know to what extend it works or needs modifications to work in .Net (does SelPrint still exist?)... 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 :) codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |