hi all,

I am writing some text to rich text box using different fonts for different lines.But i cannot be able to copy the same font to the print prieview page.When i take out the print out, all lines will be of same font.How how can i solve this problem. Can anybody suggest a code for this. I am new to C#.
Thanks in advance

Dani AI

Generated

As reported, the problem isn’t copying text — it’s that plain-text printing loses the RichTextBox’s RTF formatting (fonts, sizes, colors). As hinted, you need to render the control’s formatted RTF to the printer device context instead of using plain DrawString. The standard WinForms approach is to ask the RichEdit engine to format itself for a printer DC (EM_FORMATRANGE). The snippet below is a compact, working pattern to use from a PrintDocument.PrintPage handler; it preserves fonts and other RTF formatting across pages.

// Put these members in the Form class
const int WM_USER = 0x0400;
const int EM_FORMATRANGE = WM_USER + 57;

[StructLayout(LayoutKind.Sequential)]
struct CHARRANGE { public int cpMin; public int cpMax; }
[StructLayout(LayoutKind.Sequential)]
struct RECT { public int left; public int top; public int right; public int bottom; }
[StructLayout(LayoutKind.Sequential)]
struct FORMATRANGE {
  public IntPtr hdc; public IntPtr hdcTarget; public RECT rc; public RECT rcPage; public CHARRANGE chrg;
}

[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

int _printCharPos = 0;

int PrintRichText(RichTextBox rtb, PrintPageEventArgs e, ref int charFrom)
{
  var g = e.Graphics;
  IntPtr hdc = g.GetHdc();

  var fr = new FORMATRANGE();
  fr.hdc = hdc; fr.hdcTarget = hdc;
  float twipsPerHundredthInch = 14.4f; // 1440/100
  fr.rc.left = (int)(e.MarginBounds.Left * twipsPerHundredthInch);
  fr.rc.top = (int)(e.MarginBounds.Top * twipsPerHundredthInch);
  fr.rc.right = (int)(e.MarginBounds.Right * twipsPerHundredthInch);
  fr.rc.bottom = (int)(e.MarginBounds.Bottom * twipsPerHundredthInch);
  fr.rcPage.left = (int)(e.PageBounds.Left * twipsPerHundredthInch);
  fr.rcPage.top = (int)(e.PageBounds.Top * twipsPerHundredthInch);
  fr.rcPage.right = (int)(e.PageBounds.Right * twipsPerHundredthInch);
  fr.rcPage.bottom = (int)(e.PageBounds.Bottom * twipsPerHundredthInch);

  fr.chrg.cpMin = charFrom;
  fr.chrg.cpMax = rtb.TextLength;

  IntPtr lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fr));
  Marshal.StructureToPtr(fr, lParam, false);
  IntPtr res = SendMessage(rtb.Handle, EM_FORMATRANGE, (IntPtr)1, lParam);
  int lastChar = res.ToInt32();
  Marshal.FreeCoTaskMem(lParam);
  SendMessage(rtb.Handle, EM_FORMATRANGE, IntPtr.Zero, IntPtr.Zero);
  g.ReleaseHdc(hdc);

  charFrom = lastChar;
  return lastChar;
}

// In PrintDocument.PrintPage:
int last = PrintRichText(myRichTextBox, e, ref _printCharPos);
if (last < myRichTextBox.TextLength) { e.HasMorePages = true; }
else { e.HasMorePages = false; _printCharPos = 0; }

Troubleshooting notes: ensure the fonts used by the RTF are actually installed on the machine/printer; printer drivers may substitute missing fonts. If copy/paste between controls is needed, copy the control’s RTF markup (not plain text) so formatting is preserved before printing.

Recommended Answers

All 7 Replies

You want "SelectedRTF" not just selected, that would include the formatting

You want "SelectedRTF" not just selected, that would include the formatting

can u please suggest some code for this....
thanks

richbox1.selectedrtf=richbox2.selectedrtf

richbox1.selectedrtf=richbox2.selectedrtf

thank u very much for replying.. but when i use selectedrtf, the output is not coming.. Please suggest correctly since i am new to C#. thank u very much once again..

Well you said you were copying, the text would need to be selected in the example ive given..

Well you said you were copying, the text would need to be selected in the example ive given..

actually what i have to do is i have some text written in rich text box using some fonts for different lines. I have to take the contents of the rich text box print out. Also I can be able to get print out of the contents of rich text box. But the problem i am facing is when i take out the print out, the font will be same for all the lines. But I have to take the print out for the same font which i have stored in the rich text box. So can u please suggest me now how i solve this problem.If u tel me some code it wil be useful for me.

thank u.....

If you need all the rtf code, then use the rtf property it contains the rtf coding as well which would include the fonts/pictures/colors etc.

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.