Click to See Complete Forum and Search --> : Display page break in RichTextBox, WYSIWYG


jaado
March 30th, 2010, 07:08 AM
Hi everyone,

After googling the web and trying different approaches to this problem I now turn to you.

My problem is this: I am in the early stage of developing a specialized word processor and I am using RichTextBox as editor. I would like to display the RichTextBox as wide as writable area of paper - that is, paperwidth excluding margins. And I would like to display a dotted line at the position where the writable area of the paper ends. And this is where my problems start.

I've tried to set the WYSIWIG mode as described i numerous articles but when I calculate number of lines that fit in my RichTextBox the result will never be the same as displayed in print preview.

To calculate numer of lines that fit in RichTextBox I fetch graphics object from default printer settings and divide it's printable area with GetHeight(printerGraphics) of current font.

When comparing layout of text in RichTextBox with text rendered by RichtTextBox in print preview it looks like line spacing in print preview is lower than line spacing displayed in RichTextBox.

If anybody has a solution for this, an implementation of their own or other hints that will point me in right direction I would be so happy.
/
Jan Adolfsson

jaado
March 30th, 2010, 02:42 PM
This is how my code in the richtextbox looks like:

float widthInPixels = float.MinValue;
float heightInPixels = float.MinValue;

float printAreaWidth = printerSettings.DefaultPageSettings.Bounds.Width - printerSettings.DefaultPageSettings.Margins.Left - printerSettings.DefaultPageSettings.Margins.Right;
float printAreaHeight = printerSettings.DefaultPageSettings.Bounds.Height - printerSettings.DefaultPageSettings.Margins.Top - printerSettings.DefaultPageSettings.Margins.Bottom;

using (Graphics rtbGraphics = CreateGraphics())
{
widthInPixels = (printAreaWidth / 100.0f ) * rtbGraphics.DpiX;
heightInPixels = (printAreaHeight / 100.0f) * rtbGraphics.DpiY;
}
Graphics printerGraphics = printerSettings.CreateMeasurementGraphics();

float fontHeight = Font.GetHeight(printerGraphics);
_nrOfRowsOnPage = (int)(printAreaHeight / fontHeight);
_printerDC = printerGraphics.GetHdc();

printAreaWidth = printAreaWidth * 14.4f;
Win32API.SendMessage(Handle, Win32API.EM_SETTARGETDEVICE, _printerDC.ToInt32(), new IntPtr((int)printAreaWidth));

Width = (int)widthInPixels;

jaado
April 8th, 2010, 06:13 AM
I finally found a solution to this problem.

First a fill the richtextbox with lines. Then I use the formatrange method to get character index of last character that fit on the page and recalculate this to number of lines. Then I multiply number of lines with font height and I think I have the right position for the page break line.

If you find a better solution please let me know.
/jan Adolfsson

nhshah
May 6th, 2010, 03:50 AM
Hi,

Can you please show me that code?
I really need it , its urgent.

Regards,
Nidhi

nhshah
May 7th, 2010, 11:41 PM
I finally found a solution to this problem.

First a fill the richtextbox with lines. Then I use the formatrange method to get character index of last character that fit on the page and recalculate this to number of lines. Then I multiply number of lines with font height and I think I have the right position for the page break line.

If you find a better solution please let me know.
/jan Adolfsson

Can you please send me code? I really need it for my project
Thanks for your help

carlos.mx
December 29th, 2011, 02:03 PM
Me too.. please..
Can you please send me code? I really need it for my project
Thanks for your help