How To…Add a PDF button

Question

“How can I add a PDF button to the Report Explorer toolbar?”

Solution

The following example shows create and register a custom ReportExplorer form descendant.

Download: ReportExplorerAddPDFButton.zip

Sample Delphi code:

uses
  ppForms,
  ppExpFrm,
  ppTBX,
  ppToolResources,
  ppTypes;

type

  TmyReportExplorerForm = class(TppReportExplorerForm)
  private
    FPDFButton: TppTBXItem;
  protected
    procedure ehPDFButton_Click(Sender: TObject); virtual;
  public
    procedure Initialize; override;
    property PDFButton: TppTBXItem read FPDFButton;
  end;

implementation

uses
  ppFileUtils,
  ppReport,
  ppRptExp;


{------------------------------------------------------------------------------}
{ TppReportExplorerForm.ehPDFButton_Click }

procedure TmyReportExplorerForm.ehPDFButton_Click(Sender: TObject);
var
  lReportExplorer: TppReportExplorer;
  lReport: TppReport;
begin

  lReportExplorer := TppReportExplorer(ReportExplorer);

  lReport := lReportExplorer.Designer.Report;

  if lReportExplorer.IsReport(ListView.ItemName, ListView.FolderId)then
    begin
      lReportExplorer.LoadReport(ListView.ItemName, ListView.FolderId);

      lReport.DeviceType := dtPDF;
      lReport.AllowPrintToFile := True;
      lReport.PDFSettings.OpenPDFFile := True; // optional
//      lReport.ShowPrintDialog := False; // optional

      lReport.TextFileName := TppFileUtils.GetApplicationFilePath + ListView.ItemName + '.pdf';  // default file name
      lReport.Print;
    end;

end;

{------------------------------------------------------------------------------}
{ TppReportExplorerForm.Initialize }

procedure TmyReportExplorerForm.Initialize;
var
  liIndex: Integer;
begin

  inherited;

  liIndex := Toolbar.Items.IndexOf(Toolbar.Email);

  FPDFButton := TppTBXItem.Create(nil);
  Toolbar.Items.Insert(liIndex, FPDFButton);

  FPDFButton.Hint := 'PDF';
  FPDFButton.OnClick := ehPDFButton_Click;

  //TODO: create image list and load PDF icon
//  FPDFButton.Images :=  FImageList;
//  FPDFButton.ImageIndex :=

  // temp code - need an icon for the button to appear
  FPDFButton.ImageIndex := ToolImageList.AddIcon('PPEMAIL');


end; {procedure, Initialize}


{******************************************************************************
 *
 ** I N I T I A L I Z A T I O N   /   F I N A L I Z A T I O N
 *
{******************************************************************************}

initialization

  ppRegisterForm(TppCustomReportExplorer, TmyReportExplorerForm);

finalization

  ppUnRegisterForm(TppCustomReportExplorer);

end.