Intelligent page breaks in reports (Delphi)
Overview
Intelligent page breaks are a FlexCel feature that lets you keep
together selected rows when printing.
In this demo we will see how to use them on a report.
Make sure to read the conceptual documentation
about Intelligent Page Breaks
to better understand what we are doing here.
Concepts
How to create Multi-level "keep together" ranges. In most reports
one level is normally enough, but in master detail cases or
situations like this one, you might need more than one.
Take a look at the "keeprows_x_" named ranges in the template.
Those are the ranges you use to tell FlexCel which rows to keep
together. To have an interesting data sample for our demo, we will
write a chart of the customers only if they are from Germany or
USA.
In this example, we want to keep full customers in the same page.
But if that isn't possible, we would like the chart not to split
in the middle of a page break. That is why we defined a
"keeprows_2_" inside "keeprows_1_" protecting the chart.
You can see an example of how this works at pages 43-44 when doing
a print preview. The chart is completely in a page 44, instead of
half in 43 and half in the 44. If you remove the "keeprows_2_"
range from the template you will see how that would be otherwise.
Files
UMainForm.pas
unit UMainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics,
FlexCel.VCLSupport, FlexCel.Core, FlexCel.XlsAdapter, FlexCel.Report, FlexCel.Render,
{$if CompilerVersion >= 23.0} System.UITypes, {$IFEND}
ShellApi,
Controls, Forms, Dialogs, StdCtrls, ExtCtrls;
type
TMainForm = class(TForm)
btnCancel: TButton;
btnGo: TButton;
SaveDialog: TSaveDialog;
Label1: TLabel;
procedure btnCancelClick(Sender: TObject);
procedure btnGoClick(Sender: TObject);
private
procedure RunReport;
function GetDataPath: string;
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
uses IOUtils, DemoCustomers;
{$R *.dfm}
procedure TMainForm.btnCancelClick(Sender: TObject);
begin
Close;
end;
procedure TMainForm.btnGoClick(Sender: TObject);
begin
RunReport;
end;
function TMainForm.GetDataPath: string;
begin
Result := TPath.Combine(TPath.GetDirectoryName(ParamStr(0)), '..\..');
end;
procedure TMainForm.RunReport;
var
Report: TFlexCelReport;
begin
if not SaveDialog.Execute then exit;
Report := TFlexCelReport.Create(true);
try
Report.AddTable(DemoTables);
Report.SetValue('Date', Now);
Report.Run(
TPath.Combine(GetDataPath, 'Intelligent Page Breaks In Reports.template' + TPath.GetExtension(SaveDialog.FileName)),
SaveDialog.FileName);
finally
Report.Free;
end;
if MessageDlg('Do you want to open the generated file?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then
begin
ShellExecute(0, 'open', PCHAR(SaveDialog.FileName), nil, nil, SW_SHOWNORMAL);
end;
end;
end.