WPCubed Product Support

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.

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:

// Header
    aWPRichText.WritingAttr.Clear;
    aWPRichText.WritingAttr.SetFontName('Verdana');
    aWPRichText.WritingAttr.SetFontSize(10);
    aWPRichText.WritingAttr.SetFontStyle([fsBold]);
    txtplain := aWPRichText.WritingAttr.CharAttr;

    par := aWPRichText.HeaderFooter.Get(wpHeader,wpraOnFirstPage).FirstPar;
    for n := 0 to aWPRichText.HeaderFooter.Items[0].Collection.Count do
    begin
      par := aWPRichText.HeaderFooter.Get(wpHeader,wpraOnFirstPage).FirstPar;
      par.DeleteParagraph;
    end;

    for n := 0 to aWPRichText.HeaderFooter.Items[0].Collection.Count do
    begin
      par := aWPRichText.HeaderFooter.Get(wpHeader,wpraOnAllPages).FirstPar;
      par.DeleteParagraph;
    end;

    // First page
    par := aWPRichText.HeaderFooter.Get(wpHeader,wpraOnFirstPage).FirstPar;
    par.Clear;
    par.ASet(WPAT_Alignment, Integer(paralLeft));

    Navn := trim(qBorger.FieldByName('navn_formel').AsString);
    if pos(', ',Navn) = 1 then // hackish
      System.Delete(Navn,1,2);
    if navn = '' then
      Navn := '<i.a>';

    par.Insert(0, CPRorID + #13#10, txtplain);

    // All pages
    par := aWPRichText.HeaderFooter.Get(wpHeader,wpraOnAllPages).FirstPar;
    par.Clear;
    par.ASet(WPAT_Alignment, Integer(paralLeft));
    par.Insert(0, CPRorID + #13#10, txtplain);

    // Footer
    aWPRichText.WritingAttr.Clear;
    aWPRichText.WritingAttr.SetFontName('Verdana');
    aWPRichText.WritingAttr.SetFontSize(10);
    aWPRichText.WritingAttr.SetFontStyle([fsBold]);
    txtplain := aWPRichText.WritingAttr.CharAttr;

    par := aWPRichText.HeaderFooter.Get(wpFooter,wpraOnFirstPage).FirstPar;
    for n := 0 to aWPRichText.HeaderFooter.Items[0].Collection.Count do
    begin
      par := aWPRichText.HeaderFooter.Get(wpFooter,wpraOnFirstPage).FirstPar;
      par.DeleteParagraph;
    end;

    ... goes on and on..

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.

#1 ♥ 0
6.9k Views 3 users read 3 min

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

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?

#2 ♥ 0

Is GetTextLen unsafe or need something be flushed before calling it? In this particular program it was used two times. Some older applications of ours use the call a lot.

#4 ♥ 0
support

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

#5 ♥ 0
This category is archived. No new posts are possible.