How To…Create a Table of Contents

Question

“How do I create a table of contents sorted by page at the beginning of my report?”

Solution

Use a group to separate out each topic in the table of contents.  During the first pass of the report, keep track of the current page inside the OnGroupBreak event for each group, then build the table of contents based on these values for the second pass of the report.

Download:  TableOfContents.zip

Sample Delphi code:

procedure TForm1.ppGroup1AfterGroupBreak(Sender: TObject);
var
  lsEntry: String;
begin

  if ppReport1.FirstPass then
    begin
      //Create the CalculateContentsEntry retrieve and process the group info.
      //See the example for an implementation of this routine.
      lsEntry := CalculateContentsEntry(ppMemo1.Font, 4);

      ppMemo1.Lines.Add(lsEntry + IntToStr(ppReport1.AbsolutePageNo));
    end;
end;

procedure TForm1.ppReport1BeforePrint(Sender: TObject);
begin
  ppMemo1.Lines.Clear;
end;