Do you own a Debenu Quick PDF Library version 7, 8, 9, 10, 11, 12, 13 or iSEDQuickPDF license? Upgrade to Debenu Quick PDF Library 14 today!
![]() |
Drawing RichText (RTF) |
Post Reply ![]() |
Author | |
waynefulcher ![]() Senior Member ![]() ![]() Joined: 23 Jun 08 Location: United States Status: Offline Points: 99 |
![]() ![]() ![]() ![]() ![]() Posted: 30 Aug 11 at 3:37PM |
Does anyone know of any code that could parse richtext and convert that to drawing commands which could be used to call methods in the QuickPDF library to output the richtext to PDF?
I am using Delphi 7 but really if anyone has any code in pretty much any language I can convert it to delphi. What would really be nice is if QuickPDF had a method something like:
PDF.DrawRichText(top, left, richtext) ;
Any help or direction for me to look would be awesome. I just don't want to start from scratch by writing my own RTF parser if I don't have to.
Thanks
Wayne
|
|
![]() |
|
AndrewC ![]() Moderator Group ![]() ![]() Joined: 08 Dec 10 Location: Geelong, Aust Status: Offline Points: 841 |
![]() ![]() ![]() ![]() ![]() |
I have played with some code in C# and the results were reasonable but not perfect. I did also attempt to move the same code to Delphi but the results were far from perfect.
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"); } |
|
![]() |
Post Reply ![]() |
|
Tweet
|
Forum Jump | Forum Permissions ![]() You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |
Copyright © 2017 Debenu. Debenu Quick PDF Library is a PDF SDK. All rights reserved. About — Contact — Blog — Support — Online Store