Using the CreatePage event in FastReport .NET

2024-04-08

The new event CreatePage occurs when creating a new page in the FastReport .NET engine. At this stage, you can change the properties of the page. Other events like StartPage, FinishPage, and ManualBuild are triggered before the beginning/end of building the page template.

For example, you can alternate between landscape and portrait page orientations. Alternatively, with the help of this event, you can create a separate data source that will contain information about ExportAlias or other page properties. During the event execution, you can set parameters, and at the beginning of the next page, iterate through the data source.

The modified demo report using the CreatePage event to define ExportAlias is described in the following example.

<?xml version="1.0" encoding="utf-8"?>
<Report ScriptLanguage="CSharp" TextQuality="Regular" ReportInfo.Description="Demonstrates a group report. To create it:&#13;&#10;- go to "Report|Configure Bands..." menu;&#13;&#10;- press "Add" button and add a group header (this will add a data band and a group footer as well);&#13;&#10;- return to the report page, doubleclick the group header to show its editor." ReportInfo.Created="01/17/2008 04:31:41" ReportInfo.Modified="03/13/2024 17:32:59" ReportInfo.CreatorVersion="1.0.0.0">
 <ScriptText>
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using FastReport;
using FastReport.Data;
using FastReport.Dialog;
using FastReport.Barcode;
using FastReport.Table;
using FastReport.Utils;
 
namespace FastReport
{
 public class ReportScript
 {
 private void Page1_CreatePage(object sender, EventArgs e)
 {
 if(Data1.DataSource.RowCount == 0)
 Data1.InitDataSource();
 Page1.ExportAlias = Data1.DataSource["ProductName"].ToString().Substring(0,1);
 }
 }
}
</ScriptText>
 <Styles>
 <Style Name="EvenRows" Fill.Color="OldLace" Font="Arial, 10pt"/>
 </Styles>
 <Dictionary>
 <TableDataSource Name="Products" ReferenceName="NorthWind.Products" DataType="System.Int32" Enabled="true">
 <Column Name="ProductID" DataType="System.Int32"/>
 <Column Name="ProductName" DataType="System.String"/>
 <Column Name="SupplierID" DataType="System.Int32"/>
 <Column Name="CategoryID" DataType="System.Int32"/>
 <Column Name="QuantityPerUnit" DataType="System.String"/>
 <Column Name="UnitPrice" DataType="System.Decimal"/>
 <Column Name="UnitsInStock" DataType="System.Int16"/>
 <Column Name="UnitsOnOrder" DataType="System.Int16"/>
 <Column Name="ReorderLevel" DataType="System.Int16"/>
 <Column Name="Discontinued" DataType="System.Boolean" BindableControl="CheckBox"/>
 <Column Name="EAN13" DataType="System.String"/>
 </TableDataSource>
 <Total Name="TotalProducts" TotalType="Count" Evaluator="Data1" PrintOn="GroupFooter1"/>
 </Dictionary>
 <ReportPage Name="Page1" Watermark.Font="Arial, 60pt" CreatePageEvent="Page1_CreatePage">
 <ReportTitleBand Name="ReportTitle1" Width="718.2" Height="103.95" CanGrow="true">
 <TextObject Name="Text1" Top="56.7" Width="718.2" Height="47.25" Text="ALPHABETICAL PRODUCT LIST" HorzAlign="Center" VertAlign="Center" Font="Tahoma, 14pt, style=Bold"/>
 <TextObject Name="Text11" Top="28.35" Width="718.2" Height="28.35" Anchor="Top, Left, Right" Fill.Color="WhiteSmoke" CanGrow="true" CanShrink="true" Text="[Report.ReportInfo.Description]&#13;&#10;" Padding="4, 4, 4, 4" Font="Tahoma, 8pt"/>
 <TextObject Name="Text18" Width="718.2" Height="28.35" Anchor="Top, Left, Right" Fill.Color="WhiteSmoke" Cursor="Hand" Hyperlink.Value="http://fast.report/cda2a" Hyperlink.OpenLinkInNewTab="true" Text="Learn how to build this report on the Fast Reports Academy channel" VertAlign="Center" Font="Tahoma, 10pt, style=Bold, Underline" TextFill.Color="Blue"/>
 </ReportTitleBand>
 <GroupHeaderBand Name="GroupHeader1" Top="107.15" Width="718.2" Height="37.8" StartNewPage="true" KeepWithData="true" Condition="[Products.ProductName].Substring(0,1)" SortOrder="None">
 <TextObject Name="Text7" Left="9.45" Width="699.3" Height="37.8" Border.Lines="All" Border.Color="LightSkyBlue" Fill="LinearGradient" Fill.StartColor="LightSkyBlue" Fill.EndColor="AliceBlue" Fill.Angle="90" Fill.Focus="0.42" Fill.Contrast="1" Text="[[Products.ProductName].Substring(0,1)]" Padding="5, 0, 0, 0" VertAlign="Center" Font="Tahoma, 12pt, style=Bold"/>
 <DataBand Name="Data1" Top="148.15" Width="718.2" Height="18.9" DataSource="Products" KeepTogether="true">
 <TextObject Name="Text2" Left="9.45" Width="604.8" Height="18.9" Border.Lines="Left" Border.Color="LightSkyBlue" Text="[Products.ProductName]" VertAlign="Center" Font="Tahoma, 8pt">
 <Highlight>
 <Condition Expression="[Row#] % 2 == 0" Fill.Color="AliceBlue" TextFill.Color="Black" Font="Arial, 10pt" ApplyFill="true" ApplyTextFill="false"/>
 </Highlight>
 </TextObject>
 <TextObject Name="Text4" Left="614.25" Width="94.5" Height="18.9" Border.Lines="Right" Border.Color="LightSkyBlue" Text="[Products.UnitPrice]" Format="Currency" Format.UseLocale="true" Format.DecimalDigits="2" HorzAlign="Right" VertAlign="Center" Font="Tahoma, 8pt">
 <Highlight>
 <Condition Expression="[Row#] % 2 == 0" Fill.Color="AliceBlue" TextFill.Color="Black" Font="Arial, 10pt" ApplyFill="true" ApplyTextFill="false"/>
 </Highlight>
 </TextObject>
 <Sort>
 <Sort Expression="[Products.ProductName]"/>
 </Sort>
 </DataBand>
 <GroupFooterBand Name="GroupFooter1" Top="170.25" Width="718.2" Height="47.25" KeepWithData="true">
 <TextObject Name="Text8" Left="9.45" Width="699.3" Height="18.9" Border.Lines="Left, Right, Bottom" Border.Color="LightSkyBlue" Text="Total products: [TotalProducts]" HorzAlign="Right" VertAlign="Center" Font="Tahoma, 8pt, style=Bold"/>
 </GroupFooterBand>
 </GroupHeaderBand>
 <PageFooterBand Name="PageFooter1" Top="220.7" Width="718.2" Height="28.35" Fill.Color="WhiteSmoke">
 <TextObject Name="Text9" Left="614.25" Width="94.5" Height="28.35" Text="[PageN]" HorzAlign="Right" VertAlign="Center" Font="Tahoma, 8pt"/>
 <TextObject Name="Text12" Left="9.45" Width="217.35" Height="28.35" Cursor="Hand" Hyperlink.Value="https://www.fast-report.com/en/product/fast-report-net/" Text="Generated by FastReport .NET" VertAlign="Center" Font="Tahoma, 8pt, style=Underline" TextFill.Color="Blue"/>
 </PageFooterBand>
 </ReportPage>
</Report>

The result of exporting the report from the example to Excel 2007:

Example of exporting a finished report

Note 1. Engine.CurPage increases after calling a new event, and since CurPage is the index of the page, its value will be 0 when creating the first two pages. Therefore, we recommend using a separate page counter in the report script when using the new event.

Note 2. When changing the Visible property to false, the further building of the page stops.

Thanks to the new property, you can move to a new data record line and modify the page properties as new pages are added.

November 26, 2024

Installing FastReport on .NET 8.0 and Creating a Simple Report

The purpose of this article is to explain step by step how to install FastReport on .NET 8.0 and how to create a simple report. Taking the reporting process from the beginning, it will show how to connect, design and view reports.
November 20, 2024

Localization and Language Switching in FastReport VCL

FastReport VCL supports 40 languages for interface localization and allows you to change the language on the fly through menus or code, without recompilation.
November 01, 2024

New Features of the FastReport VCL Editor

We are considering new features of the report editor: extension lines, highlighting of intersecting objects, updated report and data trees.
Fast Reports
  • 800-985-8986 (English, US)
  • +4930568373928 (German)
  • +55 19 98147-8148 (Portuguese)
  • info@fast-report.com
  • 66 Canal Center Plaza, Ste 505, Alexandria, VA 22314

© 1998-2024 Fast Reports Inc.