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..
Alles anzeigen
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.