Merging RTF's with title line

  • The task is to create a document from other documents. Before inserting a document I must add a bold line with the creation date, creator and the title.

    Code
    procedure TDokumentData.BuildRtfDocument(aWPRichText: TWPRichText);var  st: string;  MemoryStream: TMemoryStream;begin  MemoryStream := TMemoryStream.Create;  try    if not Tomt then      if not LoadToStream(MemoryStream) then        raise Exception.CreateFmt('Kunne ikke indlæse %d: %s',[DokumentRef,Overskrift]);    aWPRichText.SelStart := aWPRichText.GetTextLen;    aWPRichText.WritingAttr.Clear('Verdana', 10);    aWPRichText.WritingAttr.SetFontStyle([fsBold]);    st := #13#10#13#10#13#10 + DateToStr(Oprettet) + '    ' + BrugerInit + '    ' + Overskrift + #13#10#13#10;    aWPRichText.InputString(st);    aWPRichText.WritingAttr.SetFontStyle([]);    MemoryStream.Seek(0,soFromBeginning);    aWPRichText.LoadSelectionFromStream(MemoryStream);    aWPRichText.ReformatAll(false);  finally    MemoryStream.Free;  end;end;

    Now, sometimes a document is empty but the line needs to be printed anyway. The stream is empty so the aWPRichText.LoadSelectionFromStream dows nothing. What happens then is that the caption is hacked up and printed several lines below. A line would look like this: <date> <creator> <tit \n\n\n le>.

    The snag is that after the documents are inserted a header and footer is constructed. Here is the first part of that method:

    I have tried a lot but with little success. If anyone could point out the what is wrong or if anything could be done smarter (e.g. using the rather undocumented InputParagraph), please do.

    Thanks in advance.

  • In the bottom of the method adding the headers, footers and first page info, I found these two lines.

    Code
    aWPRichText.SelStart := aWPRichText.GetTextLen;
    aWPRichText.InputString(#13#10#13#10#13#10);

    Apparently they had the SelStart placed in the middle of the title-line. The lines were deleted and the report worked.

    Can anyone tell me the correct way to move the current position to the bottom or why the above failed?

    • Offizieller Beitrag

    Hi,

    GetTextLen is used in combination with GetTextBuf - it is NOT the length of the text in the editor. It just reprots the amount of bytes GetTextBuf would fill. I woukd consider it unsave in this aspect. Using CPPosition := MaxInt is sufficient and optimized for speed.

    Your code looks good as far as I can see.

    Julian