How To…Merge Multiple Reports into one PDF

Question

“How do I merge multiple reports into a single PDF file?”

Solution

For ReportBuilder 11, new properties were added to the PDF Device allowing the developer to manually start and end the print job.  This allows you to easily merge multiple reports into a single PDF file.  Simply set the EndPrintJob property to false before printing the first report and set StartPrintJob to False befor printing the rest of the reports to be merged.  Before printing the final report, set EndPrintJob back to True to finalize the PDF file creation.

It is possible to preform this task in earlier versions of ReportBuilder as well by using a TppPDFDevice descendent class.  See the specified example below for more information.

Download: MultipleReportsAsOnePDF.zip
Download: MultipleReportsAsOnePDF_RB10.zip

Sample Delphi code:

var
  lPDFDevice: TppPDFDevice;
begin

  lPDFDevice := TppPDFDevice.Create(nil);

  //lPDFDevice.PDFSettings := ppReport1.PDFSettings;  // optionally assign PDFSettings from report
  lPDFDevice.PDFSettings.OpenPDFFile := True;
  lPDFDevice.FileName := TppFileUtils.GetApplicationFilePath + 'myReport.PDF';

  //Don't end the job after the first report.
  lPDFDevice.EndPrintJob := False;
  lPDFDevice.Publisher := ppReport1.Publisher;

  //Print the first report
  ppReport1.PrintToDevices;

  lPDFDevice.Reset;
  lPDFDevice.Publisher := ppReport2.Publisher;

  //No need to start a new print job.
  lPDFDevice.StartPrintJob := False;
  lPDFDevice.EndPrintJob := True;

  //Print the second report
  ppReport2.PrintToDevices;

  lPDFDevice.Free;

end;