Print Page | Close Window

Drawing RichText (RTF)

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: I need help - I can help
Forum Description: Problems and solutions while programming with the Debenu Quick PDF Library and Debenu PDF Viewer SDK
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=1940
Printed Date: 13 May 25 at 9:13AM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: Drawing RichText (RTF)
Posted By: waynefulcher
Subject: Drawing RichText (RTF)
Date 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



Replies:
Posted By: AndrewC
Date Posted: 01 Sep 11 at 4:55AM
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");
        }






Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk