Create text with multiple letters

Top  Previous  Next

 

Sometimes you need to create a longer text which contains copies of the same template filled with different data.

 

You can use a second TWPRichText object "AllRTFText" to receive the text of all the single letters.

 

Then You can use code like this to loop through the complete database, merge each record and append the result to AllRTFText. The first time the text is copied using "AsString" to make sure the page format and the header and footers are copied too. Later records use FastAppendText. 

 

   var i : Integer;

 

   Table1.DisableControls;

   try

   Table1.First;

   i := 0;

   AllRTFText.BeginUpdate;

   AllRTFText.Clear;

   while not Table1.EOF do

   begin

      WPRichText1.MergeText;

 

      if i=0 then // FIRST RUN

      begin

         AllRTFText.AsString := WPRichText1.AsString;

         AllRTFText.CPPosition := MaxInt; // to end

      end

      else if i>0 then // SUBSEQUENT RUNS

      begin

         // Append a new paragraph IF  the last line is not empty

         if AllRTFText.ActivePosInPar>0 then

             AllRTFText.InputString(#13);

         // Need page break

         AllRTFText.FastSetPageBreak(true, true);

         // and append the text

         AllRTFText.FastAppendText(WPRichText1,false);

      end;

      Application.ProcessMessages;

      Table1.Next;

      inc(i);

   end;

   finally

     AllRTFText.EndUpdate;

     AllRTFText.ReformatAll(true, true);

     Table1.EnableControls;

   end;

 

The above code simply copies the text to the destination. It will be one large text without sections.

To create sections please add the marked lines in red. We use the section property wpsec_ResetOutlineNums to make sure each section uses its own outline numbering.

 

  var Section : TWPRTFSectionProps;

 

  try

  Table1.First;

...

        // Need page break

        AllRTFText.FastSetPageBreak(true, true);

        // and append the text

        Section := AllRTFText.FastAppendText(WPRichText1,true);

        Section.Select := [wpsec_ResetOutlineNums];

     end;

...

  end;

  finally

...

  end;

 

 

When you save the resulting text you can use the following writer options in the format string to create better RTF code:

-nonumberprops:        Write the numbers as text

-nomergefields:        Do not save the fields, only the contents