PrintDocument clipping / wrapping text problem
-
I have the following code which prints three RichTextBox controls to a page. The format is Avery 5388 which is the perforated index card stock paper (8.5x11). The print preview shows the text in each box but the lines don't wrap. Any way to fix the wrapping problem?
private void fcPrintDocument\_PrintPage( object sender, PrintPageEventArgs e ) { cardDataTabPage.Focus(); DrawPage( e.Graphics ); } private void DrawPage( Graphics graphics ) { Graphics g = graphics; Pen boxPen = new Pen( Brushes.Black ); int pageWidth = fcPrintDocument.DefaultPageSettings.PaperSize.Width; int pageHeight = fcPrintDocument.DefaultPageSettings.PaperSize.Height; RectangleF srcRect = cardDataTabPage.ClientRectangle; RectangleF destRect = new Rectangle( 0, 0, pageWidth, pageHeight ); float scaleX = destRect.Width / 850; float scaleY = destRect.Height / 1100; for ( int i = 0; i < cardDataTabPage.Controls.Count; i++ ) { if ( cardDataTabPage.Controls\[i\].GetType() == richTextBox1.GetType() ) { RichTextBox rich = (RichTextBox)cardDataTabPage.Controls\[i\]; g.DrawRectangle( boxPen, rich.Bounds ); // draws 3x5 box where card should be g.DrawString( rich.Text, rich.Font, Brushes.Black, rich.Bounds.Left \* scaleX, rich.Bounds.Top \* scaleY, new StringFormat() ); } } }
Thanks.
-
I have the following code which prints three RichTextBox controls to a page. The format is Avery 5388 which is the perforated index card stock paper (8.5x11). The print preview shows the text in each box but the lines don't wrap. Any way to fix the wrapping problem?
private void fcPrintDocument\_PrintPage( object sender, PrintPageEventArgs e ) { cardDataTabPage.Focus(); DrawPage( e.Graphics ); } private void DrawPage( Graphics graphics ) { Graphics g = graphics; Pen boxPen = new Pen( Brushes.Black ); int pageWidth = fcPrintDocument.DefaultPageSettings.PaperSize.Width; int pageHeight = fcPrintDocument.DefaultPageSettings.PaperSize.Height; RectangleF srcRect = cardDataTabPage.ClientRectangle; RectangleF destRect = new Rectangle( 0, 0, pageWidth, pageHeight ); float scaleX = destRect.Width / 850; float scaleY = destRect.Height / 1100; for ( int i = 0; i < cardDataTabPage.Controls.Count; i++ ) { if ( cardDataTabPage.Controls\[i\].GetType() == richTextBox1.GetType() ) { RichTextBox rich = (RichTextBox)cardDataTabPage.Controls\[i\]; g.DrawRectangle( boxPen, rich.Bounds ); // draws 3x5 box where card should be g.DrawString( rich.Text, rich.Font, Brushes.Black, rich.Bounds.Left \* scaleX, rich.Bounds.Top \* scaleY, new StringFormat() ); } } }
Thanks.
Hi, you're not printing a RichTextBox at all, you are simply painting its textual content, in a single font, style, and color, at a specific point without taking care of width and height. There are DrawString overloads that take a Rectangle and perform a word wrap for you, keeping everything in the allotted width. If you really want to print an RTB, you need to call some Win32 functions, using P/Invoke. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Getting an article published on CodeProject should be easier and faster.
-
Hi, you're not printing a RichTextBox at all, you are simply painting its textual content, in a single font, style, and color, at a specific point without taking care of width and height. There are DrawString overloads that take a Rectangle and perform a word wrap for you, keeping everything in the allotted width. If you really want to print an RTB, you need to call some Win32 functions, using P/Invoke. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Getting an article published on CodeProject should be easier and faster.
I figured it out after I found a reference that states: when passing a RectangleF as one of the parameters, DrawString automatically wraps the text inside the destination's bounding rectangle. So I changed by method call to:
RectangleF cardBox = new RectangleF( rich.Bounds.X + 5, rich.Bounds.Y + 5, rich.Bounds.Width - 5, rich.Bounds.Height - 5 ); g.DrawString( rich.Text, rich.Font, Brushes.Black, cardBox, frmt );
The 'cardBox' rectangle is deflated by 5 pixels for padding. I also included a StringFormat to center the text on the card.
Mark
-
I figured it out after I found a reference that states: when passing a RectangleF as one of the parameters, DrawString automatically wraps the text inside the destination's bounding rectangle. So I changed by method call to:
RectangleF cardBox = new RectangleF( rich.Bounds.X + 5, rich.Bounds.Y + 5, rich.Bounds.Width - 5, rich.Bounds.Height - 5 ); g.DrawString( rich.Text, rich.Font, Brushes.Black, cardBox, frmt );
The 'cardBox' rectangle is deflated by 5 pixels for padding. I also included a StringFormat to center the text on the card.
Mark
you want -10 to center, not -5, and even that is just an approximation as it will paint flush left. to really center, you need to use Graphics.MeasureString to find actual width, then adjust Left (and maybe Top) while keeping the original Width and Height. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Getting an article published on CodeProject should be easier and faster.