How To…Change the Report Color

Question

“How do I change the color of objects inside the report from the preview window?”

Solution

Create a preview plugin that adds a new button to the toolbar.  Once this button is clicked, loop through every object in the report changing its color as you go.  Then regenerate the report to the previewer.

Download: ChangeReportColor.zip

Sample Delphi code: 

procedure TmyChangeReportColorPreview.UpdatePageColor;
var
  lPage: TppPage;
  lDrawText: TppDrawText;
  liIndex: Integer;
begin

  if (FColor = -1) then Exit;

  lPage := Viewer.CurrentPage;

  for liIndex := 0 to lPage.DrawCommandCount - 1 do
    begin

      if (lPage.DrawCommands[liIndex] is TppDrawText)  then
        begin
          lDrawText := TppDrawText(lPage.DrawCommands[liIndex]);

          lDrawText.Font.Color := FColor;
        end;

    end;

  Viewer.ScreenDevice.RenderPage;
  Viewer.RefreshPage;

end;

procedure TmyChangeReportColorPreview.UpdateReportColor;
var
  liBand: Integer;
  liObject: Integer;
  lObject: TppComponent;
  lReport: TppReport;
begin

  if (FColor = -1) then Exit;

  lReport := TppReport(Viewer.Report);

  for liBand := 0 to lReport.BandCount-1 do
    begin

      for liObject := 0 to lReport.Bands[liBand].ObjectCount-1 do
        begin
          lObject := lReport.Bands[liBand].Objects[liObject];

          if lObject.HasFont then
            lObject.Font.Color := FColor;

        end;

    end;

end;