RichEdit

Top  Previous  Next

Procedure to export the text in any RichEdit object using wPDF to PDF.

 

It only requires the Handle of the RichEdit control (not the control!) the desired page size + margins in CM or INCH and a valid instance of a TWPDFPrinter class. In this class please set the filename as minimum. (The procedure can be used without linking in the unit "richedit" - it includes all type definitions it needs)

 

 Example:

    WPPDFPrinter1.FileName := 'c:\richedit.pdf';

    RichEditPDFPrint(RichEdit1.Handle,

        21, 29.7,       // Page Size in CM

        2,2,2,2,       // Margins in CM

         true,           // Use CM - otherwise INCH are used

         WPPDFPrinter1); // A valid TWPDFPrinter Instace

 

If you need to export different styles of tab-stops, header and footer please check out WPTools to render the RTF text. This also eliminates the dependency on the RichEdit DLL

 

 

procedure RichEditPDFPrint(RichEditHandle : Cardinal;

 PageWidth, PageHeight, LeftMargin, TopMargin, RightMargin, BottomMargin : Extended;

 UseCM : Boolean; PDFPrinter : TWPCustomPDFExport);

const

 EM_FORMATRANGE = WM_USER + 57;

type

 TRichCharRange = record

   cpMin: Longint;

   cpMax: LongInt;

end;

 TRichFormatRange = record

   hdc: HDC;

   hdcTarget: HDC;

   rc: TRect;

   rcPage: TRect;

   chrg: TRichCharRange;

end;

var

 Range: TRichFormatRange;

 LastChar, MaxLen, LogX, LogY, OldMap, PageW, PageH: Integer;

 PageRect: TRect;

 DC : HDC;

begin

 FillChar(Range, SizeOf(TRichFormatRange), 0);

 DC := GetDC(0);

 LogX := GetDeviceCaps(DC, LOGPIXELSX);

 LogY := GetDeviceCaps(DC, LOGPIXELSY);

try

   PDFPrinter.BeginDoc;

  // Initialize Page and output parameter

  if UseCM then

  begin

    PageW := Round( PageWidth * LogX / 2.54);

    PageH := Round( PageHeight * LogY / 2.54);

    PageRect.Left := Round( LeftMargin * 1440 / 2.54);

    PageRect.Right := Round( (PageWidth-RightMargin) * 1440 / 2.54);

    PageRect.Top := Round( TopMargin * 1440 / 2.54);

    PageRect.Bottom := Round( (PageHeight-BottomMargin) * 1440 / 2.54);

  end else

  begin

    PageW := Round( PageWidth * LogX);

    PageH := Round( PageHeight * LogY);

    PageRect.Left := Round( LeftMargin * 1440);

    PageRect.Right := Round( (PageWidth-RightMargin) * 1440);

    PageRect.Top := Round( TopMargin * 1440);

    PageRect.Bottom := Round( (PageHeight-BottomMargin) * 1440);

  end;

  // Initilaize Format Parameters

   LastChar := 0;

   MaxLen := SendMessage(RichEditHandle, WM_GETTEXTLENGTH, 0, 0);

   Range.rcPage := PageRect;

   Range.chrg.cpMax := -1;

   Range.hdcTarget := DC;

   Range.hdc := DC;

   SendMessage(RichEditHandle, EM_FORMATRANGE, 0, 0);  

  try

    repeat

       PDFPrinter.StartPage(PageW,PageH,LogX, LogY, 0);

       Range.rc := PageRect;

       Range.chrg.cpMin := LastChar;

       Range.hdcTarget := PDFPrinter.Canvas.Handle;

       Range.hdc := PDFPrinter.Canvas.Handle;

       LastChar := SendMessage(RichEditHandle, EM_FORMATRANGE, 1, Longint(@Range));

       PDFPrinter.EndPage;

    until (LastChar >= MaxLen) or (LastChar = -1);

     PDFPrinter.EndDoc;

  finally

     SendMessage(RichEditHandle, EM_FORMATRANGE, 0, 0); // flush buffer

  end;

finally

   ReleaseDC(0,DC);

end;

end;