Here is the C# code which should help you get started. Some Googling will help also.
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[StructLayout(LayoutKind.Sequential)]
private struct CHARRANGE
{
public int cpMin; //First character of range (0 for start of doc)
public int cpMax; //Last character of range (-1 for end of doc)
}
[StructLayout(LayoutKind.Sequential)]
private struct FORMATRANGE
{
public IntPtr hdc; //Actual DC to draw on
public IntPtr hdcTarget; //Target DC for determining text formatting
public RECT rc; //Region of the DC to draw to (in twips)
public RECT rcPage; //Region of the whole DC (page size) (in twips)
public CHARRANGE chrg; //Range of text to draw (see earlier declaration)
}
private const int WM_USER = 0x0400;
private const int EM_FORMATRANGE = WM_USER + 57;
[DllImport("USER32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
// Size RichTextBox1 to A4 format
public void RenderRTFtoPDF(string rtfFilename)
{
IntPtr dc;
int pageNumber = 1;
int lastChar = 0;
int docId;
int mainDocId = 0;
int ret;
// Create a new RichTextBox to use for rendering.
// RichTextBox richTextBox1 = new RichTextBox();
// richTextBox1.Visible = false;
richTextBox1.Left = richTextBox1.Top = 0;
richTextBox1.Width = (int)(8.268 * 96) - 50;
richTextBox1.Height = (int)(11.693 * 96) - 600;
richTextBox1.LoadFile(rtfFilename);
do
{
QP.SetMeasurementUnits(1);
// get a virtual device context size at A4
//dc = (IntPtr)QP.GetCanvasDC(richTextBox1.Width * 96, richTextBox1.Height * 96); // 1 point = 20 twips
//dc = (IntPtr)QP.GetCanvasDC((int)(595 * 1.37), (int)(842 * 1.37)); // 1 point = 20 twips
dc = (IntPtr)QP.GetCanvasDC((int)(8.27 * 96), (int)(11.70 * 96)); // 1 point = 20 twips
// print the rtfbox
lastChar = RenderRTF(dc, richTextBox1, lastChar);
//generate a new document
ret = QP.LoadFromCanvasDC(96, 3);
ret = docId = QP.SelectedDocument();
if (pageNumber == 1)
mainDocId = docId;
else
{
ret = QP.SelectDocument(mainDocId);
ret = QP.MergeDocument(docId);
}
pageNumber++;
}
while (lastChar != 0 && lastChar != -1);
}
int RenderRTF(IntPtr dc, RichTextBox rtf, int FirstChar)
{
RECT RcDrawTo;
RECT RcPage;
FORMATRANGE fr;
int scale = 36;
int nextCharPosition;
RcPage.Left = 0;
RcPage.Right = (rtf.Left + rtf.Width) * scale;
RcPage.Top = 0;
RcPage.Bottom = (rtf.Top + rtf.Height) * scale;
//RcPage.Right /= 8;
//RcPage.Bottom /= 8;
RcDrawTo.Left = (rtf.Left) * scale;
RcDrawTo.Top = (rtf.Top) * scale;
RcDrawTo.Right = (rtf.Left + rtf.Width) * scale;
RcDrawTo.Bottom = (rtf.Top + rtf.Height) * scale;
fr.hdc = dc;
fr.hdcTarget = dc;
fr.rc = RcDrawTo;
fr.rcPage = RcPage;
fr.chrg.cpMin = FirstChar;
fr.chrg.cpMax = -1;
IntPtr res = IntPtr.Zero;
IntPtr wparam = IntPtr.Zero;
wparam = new IntPtr(1);
//Get the pointer to the FORMATRANGE structure in memory
IntPtr lparam = IntPtr.Zero;
lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fr));
Marshal.StructureToPtr(fr, lparam, false);
res = SendMessage(rtf.Handle, EM_FORMATRANGE, wparam, lparam);
nextCharPosition = res.ToInt32();
if (nextCharPosition < rtf.TextLength)
return nextCharPosition;
else
return 0;
}
private void button23_Click(object sender, EventArgs e)
{
RenderRTFtoPDF("Overview.rtf");
QP.SaveToFile("Overview.pdf");
}