FastReport ProgrammerManual
FastReport ProgrammerManual
Programmer's
manual
Edition 1.01
Table of Contents
TfrxReport
This component is the main one. One TfrxReport component contains one report.
In design-time, double-clicking the component calls the report designer. The component
has all necessary properties and methods for report loading and saving, design and
viewing. Let us examine the TfrxReport methods:
procedure Clear;
Clears a report.
procedure DesignReport;
Calls the report designer. The designer should be included into your project (to perform
this, it is enough to either use the “TfrxDesigner” component, or include the “frxDesgn”
unit into the “Uses” list).
procedure ShowPreparedReport;
Displays the report, which was previously built via the “PrepareReport” call.
FastReport – Programmer’s manual 5
procedure Print;
Prints a report.
Since the following methods are service ones, in most cases you would not need
to use them. They may be useful for FastReport functionality enhancement, for example,
when writing custom report components.
TfrxEngineOptions = class(TPersistent)
published
property ConvertNulls: Boolean default True;
Converts the “Null” value of the DB field into “0,” “False,” or empty string, depending
on the field type.
TfrxPreviewOptions = class(TPersistent)
published
property AllowEdit: Boolean default True;
Enables or disables a finished report editing.
pbPrint - printing
pbLoad – loading from a file
pbSave – saving into a file
pbExport - export
pbZoom - zooming
pbFind - search
pbOutline – report outline enabling
pbPageSetup – page properties
pbTools - tools
pbEdit - editor
pbNavigator - navigation
end;
TfrxPrintOptions = class(TPersistent)
published
property Copies: Integer default 1;
A number of the printable copies by default.
end;
TfrxReportOptions = class(TPersistent)
published
property Author: String;
Report author.
When starting a report. Occurs when an unknown variable is met. An event handler must
return the value of this variable.
TfrxDBDataset
TfrxUserDataset
Components for data access. The FastReport uses these components for
navigation and reference to the data set fields. Both of the components have a general
“TfrxDataSet” parent, from which they inherit most part of the functionality.
TfrxDesigner
TfrxPreview
procedure AddPage;
Adds a blank page to the end of the report.
procedure DeletePage;
Deletes the current page.
procedure Print;
Prints a report.
procedure LoadFromFile;
Displays the file loading dialogue.
procedure SaveToFile;
Displays the file saving dialogue.
procedure Edit;
Loads the current page for editing to the designer.
procedure First;
Moves to the first page.
procedure Next;
Moves to the next page.
procedure Prior;
Moves to the previous page.
procedure Last;
Moves to the last page.
procedure PageSetupDlg;
Displays the page setting dialogue.
procedure Find;
FastReport – Programmer’s manual 15
procedure FindNext;
Continues searching the text.
procedure Cancel;
Aborts a report constructing.
procedure Clear;
Clears a report.
TfrxBarcodeObject
TfrxOLEObject
TfrxChartObject
TfrxRichObject
TfrxCrossObject
TfrxCheckBoxObject
TfrxGradientObject
Add-in objects, which can be used in the report. These components themselves do
not do anything; they are necessary for automatic adding the corresponding component's
unit to the “Uses” list. If you attempt to open a report, where the connectable components
are used, without including the given objects into the project, an error message will
appear.
TfrxDialogContols
A set of add-in objects, which can be used in the dialogue forms inside the report.
It contains the following components: button, edit box, list box, etc. The component itself
does not do anything; it is necessary for automatic adding the “frxDCtrl” unit to the
“Uses” list.
TfrxBDEComponents
TfrxADOComponents
TfrxIBXComponents
A set of add-in objects, which can be used in the dialogue forms inside the report.
It contains the following components: “database”, “table” and “query”. These
components themselves do not do anything; they are necessary for automatic adding the
corresponding component's unit to the “Uses” list.
FastReport – Programmer’s manual 17
By default, a report form is stored together with the project form, i.e. in a DFM
file. In most cases, no more operations required, and you thus would not need to take
special measures to load a report. If you decided to store a report form in a file or in the
DB BLOb-field (this provides great flexibility, i.e. you can modify a report without
recompiling the program), you would have to use the “TfrxReport” methods for report
loading and saving:
Examples:
frxReport1.LoadFromFile('c:\1.fr3');
frxReport1.SaveToFile('c:\2.fr3');
Designing a report
Example:
frxReport1.DesignReport;
Running a report
FastReport – Programmer’s manual 18
In most cases, it is more convenient to use the first method. It displays the preview
window right away, while a report continues to be constructed.
The “ClearLastReport” parameter is convenient to use in case when it is necessary to add
another report to the previously constructed one (such technique is used for batch report
printing).
Example:
frxReport1.ShowReport;
Previewing a report
Example:
if frxReport1.PrepareReport then
frxReport1.ShowPreparedReport;
In this case, report construction is finished first, and after that it is displayed in the
preview window. Construction of a large report can take a lot of time, and that is why it is
better to use the “ShowReport anisochronous” method, than the
“PrepareReport/ShowPreparedReport” one. One can assign preview settings by default
via the “TfrxReport.PreviewOptions” property.
Printing a report
In most cases, you will print a report from the preview window. To print a report
manually, you should use the “TfrxReport.Print” method, for example:
FastReport – Programmer’s manual 19
frxReport1.Print;
At the same time, the dialogue, in which printing parameters can be set, will
appear. You can assign settings by default, and disable a printing dialogue with the help
of the “TfrxReport.PrintOptions” property.
It can be executed from the preview window. This also can be performed
manually with the help of the “TfrxReport.PreviewPages” methods:
Example:
frxReport1.PreviewPages.LoadFromFile('c:\1.fp3');
frxReport1.ShowPreparedReport;
Note, that after finished report loading is completed, its previewing is executed
via the “ShowPreparedReport” method!
Exporting a report
It can be performed from a preview window. The operation can also be executed
manually, via the “TfrxReport.Export” method. In the parameter of this method you
should specify the export filter you want to use:
frxReport1.Export(frxHTMLExport1);
The export filter component must be available (you must put it on the form of
your project) and be adjusted correctly.
FastReport displays reports in the standard preview window. If it does not suit you
for some reason, a custom preview form may be created. For this purpose, the
“TfrxPreview” component from the FastReport component palette was designed. To
FastReport – Programmer’s manual 20
There is two typical problems when using TfrxPreview component. It does not
handle keys (arrows, PgUp, PgDown etc) and mouse wheel (if any). To make
TfrxPreview working with keys, pass the focus to it (it can be done, for example, in the
OnShow event handler of a form):
frxPreview.SetFocus;
frxReport1.LoadFromFile('1.fr3');
frxReport1.PrepareReport;
frxReport1.LoadFromFile('2.fr3');
frxReport1.PrepareReport(False);
frxReport1.ShowPreparedReport;
We load the first report and build it without displaying. Then we load the second
one into the same «TfrxReport» object and build it with the «ClearLastReport» parameter,
equal to «False». This allows the second report to be added to the one previously built.
After that, we display a finished report in the preview window.
You can use the «Page,» «Page#,» «TotalPages,» and «TotalPages#» system
variables for displaying a page number or a total number of pages. In composite reports,
these variables work in the following way:
FastReport – Programmer’s manual 21
Interactive reports
In interactive reports, one can define a reaction for mouse-click on any of the
report objects in a preview window. For example, a user can click on the data line, and
thus run a new report with detailed data of the selected line.
Any report can become interactive. To perform this, you only need to create a
TfrxReport.OnClickObject event handler. Here is a code example of this handler below:
In this example, clicking on the object with the «Memo1» name results in
displaying a message with the contents of this object. When clicking on the «Memo2,» a
dialogue is displayed, where the contents of this object can be modified. Setting of the
«Modified» flag to «True» allows holding and displaying alterations.
In the same way, a different reaction for a click can be defined; it may, for
example, run a new report. It is necessary to NOTE the following. In the FastReport 3
FastReport – Programmer’s manual 22
version, one TfrxReport component can display only one report in the preview window
(unlike the FastReport 2.x version). That is why one should run a report either in a
separate TfrxReport object, or in the same one, but the current report must be erased.
To give a prompting indication about clickable objects to the end user, we can
modify the mouse cursor when it passes over a clickable object in the preview window.
To do this, select the desired object in the report designer and set it’s cursor property to
something other than crDefault.
One more detail concerns the defining clickable objects. In simple reports, this can
be defined either in the object’s name, or in its contents. However, this cannot always be
performed in more complicated cases. For example, a detailed report should be created in
a selected data line. A user clicked on the «Memo1» object with the '12' contents. What
data line does this object refer to? That is why you should know the primary key, which
identifies this line unambiguously. FastReport enables to assign a string, containing any
data (in our case the data of the primary key), to every report’s object. This string is stored
in the «TagStr» property.
During a report’s building, the «TagStr» property’s contents are calculated in the
same way, as contents of text objects are calculated; this means that the variables’ values
are substituted in place of all variables. A variable in this particular case is what is
enclosed into the square brackets. That is why the lines of the '1005', '2112', etc. types will
be contained in the «TagStr» property of the objects lying on the Master Data after report
building. A simple conversion from a string into an integer will give us a value of the
primary key, with which a required record can be found.
If the primary key is composite (i.e. it contains several fields) the «TagStr»
property’s contents can be the following:
[Table1."Field1"];[Table1."Field2"]
After constructing a report, the «TagStr» property contains values of the '1000;1'
type, from which it is rather not difficult to get values of a key as well.
FastReport’ s objects (such as report page, band, memo-object) are not directly
FastReport – Programmer’s manual 23
accessible from your code. This means that you cannot address the object by its name, as,
for example, when you addressing to a button on your form. To address an object, it
should be found with the help of the «TfrxReport.FindObject» method:
var
Memo1: TfrxMemoView;
after that, one can address the object’s properties and methods. You can address the
report’s pages using the «TfrxReport.Pages» property:
var
Page1: TfrxReportPage;
As a rule, you will create most reports using the designer. Nevertheless, in some
cases (for example, when the report’s form is unknown) it is necessary to create a report
manually, from code.
To create a report manually, one should perform the following steps in order:
- clear the report component
- add data sources
- add report’s page
- add bands on a page
- set bands’ properties, and then connect them to the data
- add objects on each band
- set objects’ properties, and then connect them to the data
Let us examine creation of a simple report of the «list» type. Assume that we have
the following components: frxReport1: TfrxReport and frxDBDataSet1: TfrxDBDataSet
(the last one is connected to data from the DBDEMOS, the «Customer.db» table). Our
report will contain one page with the «Report Title» and «Master Data» bands. On the
«Report Title» band there will be an object with the "Hello FastReport!" text, and the
«Master Data» one will contain an object with a link to the "CustNo" field.
var
Page: TfrxReportPage;
Band: TfrxBand;
DataBand: TfrxMasterData;
Memo: TfrxMemoView;
{ clear a report }
frxReport1.Clear;
{ add a page }
Page := TfrxReportPage.Create(frxReport1);
{ create a unique name }
Page.CreateUniqueName;
{ set sizes of fields, paper and orientation by default }
Page.SetDefaults;
{ modify paper’s orientation }
Page.Orientation := poLandscape;
All the data sources, which are to be used in the report, must be added to the list
of data sources. In our case, this is performed using the «frxReport1.DataSets.Add
(frxDBDataSet1)» line. Otherwise, a report will not work.
The call for Page.SetDefaults is not necessary, since in this case a page will have the А4
format and margins of 0 mm. SetDefaults sets 10mm margins and takes page size and
alignment, which a printers have by default.
FastReport – Programmer’s manual 25
While adding bands to a page, you should make sure they do not overlap each
other. To perform this, it is sufficient to set the «Top» and «Height» coordinates. There is
no point in modifying the «Left» and «Width» coordinates, since a band always has the
width of the page, on which it is located (in case of vertical bands it's not true – you
should set Left and Width properties and don't care about Top and Height). One should
note, that the order of bands’ location on a page is of great importance. Always locate
bands in the same way you would do it in the designer.
Objects’ coordinates and sizes are set in pixels. Since the «Left,» «Top,» «Width,»
and «Height» properties of all objects have the «Extended» type, you can point out non-
integer values. The following constants are defined for converting pixels into centimeters
and inches:
fr01cm = 3.77953;
fr1cm = 37.7953;
fr01in = 9.6;
fr1in = 96;
Band.Height := fr01cm * 5;
Band.Height := fr1cm * 0.5;
As we know, a report can contain dialogue forms. The following example shows
how to create a dialogue form, with an «OK» button:
{ for working with dialogue objects the following unit should be used }
uses frxDCtrl;
var
Page: TfrxDialogPage;
Button: TfrxButtonControl;
{ add a page }
Page := TfrxDialogPage.Create(frxReport1);
{ create a unique name }
Page.CreateUniqueName;
{ set sizes }
Page.Width := 200;
Page.Height := 200;
{ set a position }
Page.Position := poScreenCenter;
{ add a button }
Button := TfrxButtonControl.Create(Page);
Button.CreateUniqueName;
Button.Caption := 'OK';
Button.ModalResult := mrOk;
Button.SetBounds(60, 140, 75, 25);
FastReport – Programmer’s manual 26
{ show a report }
frxReport1.ShowReport;
The «PaperSize» property sets paper format. This is one of the standard values,
defined in the Windows.pas (for example, DMPAPER_A4). If a value to this property is
assigned, FastReport fills the «PaperWidth» and «PaperHeight» properties automatically
(paper size in millimeters). Setting the DMPAPER_USER (or 256) value as a format,
would mean that custom paper size is set. In this case, the «PaperWidth» and
«PaperHeight» properties should be filled manually.
The following example shows, how to modify parameters of the first page (it is
assumed that we already have a report):
var
Page: TfrxReportPage;
FastReport engine:
- report’s preparation (script, data sources initialization, bands’ tree forming)
- all calculations (aggregate functions, event handlers)
FastReport – Programmer’s manual 27
Handler:
- bands’ presentation in a certain order
procedure NewColumn;
Creates a new column. If a column is the last one, it creates a new page.
procedure NewPage;
Creates a new page.
Let us give an example of a simple handler. There is two «Master Data» bands in
a report, which are not connected to data. The handler presents these bands in an
interlaced order, six times each one. After six bands, a small gap is made.
var
i: Integer;
Band1, Band2: TfrxMasterData;
for i := 1 to 6 do
begin
{ lead/deduce bands one after another }
frxReport1.Engine.ShowBand(Band1);
frxReport1.Engine.ShowBand(Band2);
{ make a small gap }
if i = 3 then
frxReport1.Engine.CurY := frxReport1.Engine.CurY + 10;
end;
The next example shows two groups of bands alongside each other.
var
i, j: Integer;
Band1, Band2: TfrxMasterData;
SaveY: Extended;
SaveY := frxReport1.Engine.CurY;
for j := 1 to 2 do
begin
for i := 1 to 6 do
begin
frxReport1.Engine.ShowBand(Band1);
frxReport1.Engine.ShowBand(Band2);
if i = 3 then
FastReport – Programmer’s manual 29
Printing an array
To print an array, we use a report with one «Master Data» band, which will be
presented as many times, as there are elements in the array. To do this, place a
«TfrxUserDataSet» component on the form, and then set it’s properties (it is possible to
do it in a code, as shown in our example):
RangeEnd := reCount
RangeEndCount := a number of elements in an array
Printing a TStringList
Printing a file
For printing, you should use a report with a «Master Data» band, which will be
printed once (to perform this, it should be connected to a data source, which contains one
record; select a source named "Single row" from the list). Stretching («Stretch») and
splitting («Allow Split») are enabled in the band. This means, that the way the band is
stretched allows finding room for all objects located in it. However, if a band does not
find room in a page, it will be presented partially in separate pages.
File contents are presented via the «Text» object, which contains the [file]
variable. This variable, as in the previous examples, is filled in the
«TfrxReport.OnGetValue» event. Stretching is also enabled in the object (the «Stretch»
item from the contextual menu or the «StretchMode» property = smActualHeight).
FastReport – Programmer’s manual 30
Printing a TStringGrid
The «TStringGrid» component represents a table with several rows and columns.
That means that a report stretches not only by height, but by width as well. To print such
component, let us use the «Cross-tab» object (it becomes available when the
«TfrxCrossObject» component is added to the project). This object is responsible only for
printing table data with a number of rows and columns unknown beforehand. The object
has two versions: «TfrxCrossView» for user’s data printing, and «TfrxDBCrossView» for
printing the specially prepared data from the DB table.
Let us use the TfrxCrossView. The object should be preliminarily set. To perform
this, let us enter report’s designer and call the object editor by double-clicking on it. We
must set the number of the rows and columns’ titles nesting, and the number of values in
the table cells. In our case, all these values must be equal to «1». In our example, the rows
and columns’ titles and the total values of lines and columns are disabled as well.
It is necessary to fill the object with values from the StringGrid in the
«TfrxReport.OnBeforePrint» event. A value is added via the «TfrxCrossView.AddValue»
method. Its parameters are the following: composite index of a line, a column and the
cell’s value (which is composite as well, since an object can contain several values in a
cell).
The notion of variables was minutely explained in the corresponding chapter. Let
us briefly call to mind the main points.
TfrxVariable = class(TCollectionItem)
published
property Name: String;
Name of a variable
TfrxVariables = class(TCollection)
public
function Add: TfrxVariable;
Adds a variable to the end of the list
end;
Customer name
Account number
in total
total vat
Properties
Customer name
Account number
Totals
In total
total vat
frxReport1.Variables.Clear;
Adding a category
FastReport – Programmer’s manual 33
It is required to create at least one category. Categories and variables are stored in
one list. The category differs from a variable by the “space,” which is the first symbol of
the name. All variables located in the list after the category, are considered belonging to
this category.
or
var
Category: TfrxVariable;
Category := frxReport1.Variables.Add;
Category.Name := ' ' + 'My category 1';
Adding a variable
Variables can be added only after a category is already added. All the variables
located in the list after the category, are considered belonging to this category. Variables’
names must be unique within the whole list, and not within a category
this way adds a variable (if it does not exist already) or modifies a value of the existing
variable.
var
Variable: TfrxVariable;
Variable := frxReport1.Variables.Add;
Variable.Name := 'My Variable 1';
Variable.Value := 10;
Both of the ways add a variable to the end of the list, therefore, it would be added
to the last category. If a variable is supposed to be added to a specified position of the list,
use the “Insert” method:
var
Variable: TfrxVariable;
Variable := frxReport1.Variables.Insert(1);
Variable.Name := 'My Variable 1';
Variable.Value := 10;
Deleting a variable
Deleting a category
To delete a category with all its variables, use the following code:
or
var
Index: Integer;
Variable: TfrxVariable;
It should be noted, that when accessing a report variable its value is calculated if it
is of string type. That means the variable which value is 'Table1."Field1"' will return a
value of a DB field, but not the 'Table1."Field1"' string. You should be careful when
assigning a string-type values to report variables. For example, the next code will raise
exception "unknown variable 'test'" when running a report:
because FastReport trying to calculate a value of such variable. The right way to pass a
string values is:
In this case the variable value - string 'test' will be shown without errors. But keep
FastReport – Programmer’s manual 35
in mind that:
- string should not contain single quotes. All single quotes must be doubled;
- string should not contain #13#10 symbols.
Script variables
Instead of report variables, script variables are in the TfrxReport.Script. You can
define them using FastScript methods. Let's look at some differences between report and
script variables::
Working with script variables is easy. Just assign value to the variable this way:
In this case FastReport will create a variable if it is not exists, or assign a value to
it. There is no need to use extra quotes when assigning a string to that variable.
Let's look at example of using that way. Let's create the report and lay the "Text"
object to it. Type the following text in this object:
[My Variable]
FastReport – Programmer’s manual 36
Run the report and we will see that variable is shown correctly. The
TfrxReport.OnGetValue event handler is called each time when FastReport finds
unknown variable. The event handler should return a value of that variable.
The detailed description of styles can be found in the corresponding chapter. First
of all, let us call to mind, what “style”, “set of styles” and “library of styles” are.
Style is an element, which possesses a name and properties, and determines design
attributes, i.e. color, font and frame. The style determines the way a report object should
be designed. The objects such as TfrxMemoView have the Style property, which is a
property intended to set the style name. When applying a value to this property, the style
design attributes are copied to the object.
A set of styles consists of several styles, which refer to a report. The “TfrxReport”
component has the “Styles” property, which refers to the object of the “TfrxStyles” type.
The set of styles also possesses a name. The set of styles determines design of a whole
report.
TfrxStyleItem = class(TCollectionItem)
public
property Name: String;
Style name.
The set of styles is represented by the TfrxStyles class. It comprises methods for
performing such set operations as reading, saving, adding, deleting, as well as searching
for a style. The set of styles file has FS3 extension by default.
TfrxStyles = class(TCollection)
public
constructor Create(AReport: TfrxReport);
Creates the styles set. One can specify “nil” instead of “AReport,” however in this case a
user would be unable to use the “Apply” method.
procedure Apply;
Applies a set to a report.
end;
TfrxStyleSheet = class(TObject)
public
constructor Create;
Constructs a library.
FastReport – Programmer’s manual 38
procedure Clear;
Clears a library.
end;
The following code demonstrates processes of creation of styles set, and addition
of two styles to a set. After these operations are completed, the styles are applied to the
report.
var
Style: TfrxStyleItem;
Styles: TfrxStyles;
Styles := TfrxStyles.Create(nil);
var
Style: TfrxStyleItem;
Styles: TfrxStyles;
Styles := frxReport1.Styles;
Styles.Clear;
Modifying/adding/deleting a style
var
Style: TfrxStyleItem;
Styles: TfrxStyles;
Styles := frxReport1.Styles;
var
Style: TfrxStyleItem;
Styles: TfrxStyles;
Styles := frxReport1.Styles;
FastReport – Programmer’s manual 40
{ add }
Style := Styles.Add;
Style.Name := 'Style3';
var
Style: TfrxStyleItem;
Styles: TfrxStyles;
Styles := frxReport1.Styles;
{ delete }
Style := Styles.Find('Style3');
Style.Free;
After modifications are accomplished, you should call the “Apply” method:
{ use modifications }
frxReport1.Styles.Apply;
Saving/restoring a set
frxReport1.Styles.SaveToFile('c:\1.fs3');
frxReport1.Styles.LoadFromFile('c:\1.fs3');
frxReport1.Styles.Clear;
or
frxReport1.Styles := nil;
The following example illustrates how to create a library and add two sets of
styles to it.
var
Styles: TfrxStyles;
StyleSheet: TfrxStyleSheet;
StyleSheet := TfrxStyleSheet.Create;
Style libraries are frequently used for displaying accessible style sets in such
controls as “ComboBox” or “ListBox.” After that, the set, selected by a user, is applied to
a report.
StyleSheet.GetList(ComboBox1.Items);
frxReport1.Styles := StyleSheet.Items[ComboBox1.ItemIndex];
or
frxReport1.Styles := StyleSheet.Find[ComboBox1.Text];
var
Styles: TfrxStyles;
StyleSheet: TfrxStyleSheet;
{ modify a style with the Style1 name from the found set }
with Styles.Find('Style1') do
Font.Name := 'Arial Black';
var
Styles: TfrxStyles;
StyleSheet: TfrxStyleSheet;
var
FastReport – Programmer’s manual 42
i: Integer;
StyleSheet: TfrxStyleSheet;
var
StyleSheet: TfrxStyleSheet;
StyleSheet.SaveToFile('c:\1.fss');
StyleSheet.LoadFromFile('c:\1.fss');