#1 2013-03-09 12:05:38

dec
Member
Registered: 2013-03-04
Posts: 9

Limitation (in char length) when try to add a RichEdit

Hello,

I found certain "limitation" when try to add a RichEdit using "TGDIPages.AppendRichEdit()". This method work like a charm if the text contain 65536 chars length or less. If the length is greater, then the PDF exported contain the "RTF format code", but not the "formated RTF text". I will investigate the refered method of "TGDIPages", but, sincerely, I cannot found a solution.

In the method:

procedure TGDIPages.AppendRichEdit(RichEditHandle: HWnd);
var Range: TFormatRange;
    LogX, LogY, LastChar, MaxLen, OldMap: integer;
    TextLenEx: TGetTextLengthEx; // RichEdit 2.0 Window Class
begin
  if (Self<>nil) and (fCanvas<>nil) then
  with Range do begin
    LogX := GetDeviceCaps(fCanvas.Handle, LOGPIXELSX);
    LogY := GetDeviceCaps(fCanvas.Handle, LOGPIXELSY);
    rcPage.Left := (fPageMarginsPx.Left*1440) div LogX;
    rcPage.Right := ((fPhysicalSizePx.x-fPageMarginsPx.Right)*1440) div LogX;
    rcPage.Top := ((fPageMarginsPx.Top+fHeaderHeight)*1440) div LogY;
    rcPage.Bottom := ((fPhysicalSizePx.y-fPageMarginsPx.Bottom-fFooterHeight)*1440) div LogY;
    CheckHeaderDone;
    rc := rcPage;
    rc.Top := (fCurrentYPos*1440) div LogY;
    LastChar := 0;
    with TextLenEx do begin
      flags := GTL_DEFAULT;
      codepage := CP_ACP;
    end;
    MaxLen := SendMessage(RichEditHandle, EM_GETTEXTLENGTHEX, Integer(@TextLenEx), 0);
    chrg.cpMax := -1;
    OldMap := SetMapMode(hdc, MM_TEXT);
    try
      SendMessage(RichEditHandle, EM_FORMATRANGE, 0, 0);
      repeat
        chrg.cpMin := LastChar;
        hdc := fCanvas.Handle;
        hdcTarget := hdc;
        LastChar := SendMessage(RichEditHandle, EM_FORMATRANGE, 1, Integer(@Range));
        if cardinal(LastChar)>=cardinal(MaxLen) then
          break;
        NewPageInternal;
        DoHeader;
        rc := rcPage;
      until false;
      fCurrentYPos := (rc.Bottom*LogY) div 1440;
    finally
      SendMessage(RichEditHandle, EM_FORMATRANGE, 0, 0);
      SetMapMode(hdc, OldMap);
    end;
  end;
end;

"MaxLen" (returned by "SendMessage()") have the correct value if chars length is 65536 or less, but, in other case the length do not corresponde with the real RTF text length, and, therefore, the results are not the expected. I really do not know how to deal with this, but hope someone can found a possible solution. Or maybe I'm front a mistake of mine and here it's not limitation at all?

Thank you very  much.

Offline

#2 2013-03-09 12:12:59

dec
Member
Registered: 2013-03-04
Posts: 9

Re: Limitation (in char length) when try to add a RichEdit

OMG... just found that "RichEdit.MaxLength" property can help here. Follow this article on Stack Overflow I found this value to set the maximum length for the RichEdit:

RichEdit.MaxLength := System.MaxInt-2;

Then the PDF engine work like a charm. Maybe this can help to someone in the future. wink

Last edited by dec (2013-03-09 12:13:17)

Offline

#3 2013-03-10 09:42:35

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,242
Website

Re: Limitation (in char length) when try to add a RichEdit

I've added explicit information about need to use TRichEdit.MaxLength property to properly handle content > 64 KB.
See http://synopse.info/fossil/info/7addf05127

Thanks for the feedback!

Offline

#4 2013-03-10 12:34:22

dec
Member
Registered: 2013-03-04
Posts: 9

Re: Limitation (in char length) when try to add a RichEdit

You're welcome! wink

Offline

Board footer

Powered by FluxBB