delphifastreport

How to show the PrintDialog just after showing the preview in FastReport?


I'm using Delphi to develop a desktop database application, and I've an invoice report made with FastReport, I know that I can use InvoiceReport.ShowReport to show it's preview. So I need to know how I can show the print dialog automatically just after showing the preview. The user then can print it or cancel the dialog


Solution

  • To show print dialog in Fast Report you simply call Print with PrintOptions.ShowDialog:=True. By default the preview form is shown modal, so the easiest solution is to change it and call Print:

    InvoiceReport.PreviewOptions.Modal:=False;
    InvoiceReport.PrintOptions.ShowDialog:=True;
    InvoiceReport.ShowReport;
    InvoiceReport.Print;
    

    If you need to keep the preview form modal, the other option is to handle OnPreview event and call Print, but you have to postpone the procedure, so for example:

    const 
      UM_PRINT_DIALOG = WM_USER+1;
    
    ...
    
    procedure ShowPrintDialog(var Message: TMessage); message UM_PRINT_DIALOG;
    
    ...
    
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      InvoiceReport.ShowReport;
      //if you want to show the report once it is fully rendered use:
      //InvoiceReport.PrepareReport;
      //InvoiceReport.ShowPreparedReport;
    end;
    
    procedure TForm1.InvoiceReportPreview(Sender: TObject);
    begin
      PostMessage(Handle,UM_PRINT_DIALOG,0,0);
    end;
    
    procedure TForm1.ShowPrintDialog(var Message: TMessage);
    begin
      InvoiceReport.PrintOptions.ShowDialog:=True;
      InvoiceReport.Print;
    end;