using FastReport; using FastReport.Data; using FastReport.Format; using FastReport.Table; using FastReport.Utils; using System; using System.Data; using System.Drawing; using System.IO; using System.Windows; namespace ReportFromCode { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { private DataSet dataSet; public MainWindow() { InitializeComponent(); LoadDataSet(); } private void LoadDataSet() { // load nwind database dataSet = new DataSet(); dataSet.ReadXml(GetReportsFolder() + "nwind.xml"); } private string GetReportsFolder() { string thisFolder = Config.ApplicationFolder; for (int i = 0; i < 7; i++) { if (Directory.Exists(thisFolder + @"Demos\Reports")) { return Path.GetFullPath(thisFolder + @"Demos\Reports\"); } thisFolder += @"..\"; } throw new Exception("Could not locate the Reports folder."); } private void SimpleList_Click(object sender, RoutedEventArgs e) { Report report = new Report(); // register all data tables and relations report.RegisterData(dataSet); // enable the "Employees" table to use it in the report report.GetDataSource("Employees").Enabled = true; // add report page var page = new ReportPage(); report.Pages.Add(page); // always give names to objects you create. You can use CreateUniqueName method to do this; // call it after the object is added to a report. page.CreateUniqueName(); // create title band page.ReportTitle = new ReportTitleBand(); // native FastReport unit is screen pixel, use conversion page.ReportTitle.Height = Units.Centimeters * 1; page.ReportTitle.CreateUniqueName(); // create title text var titleText = new TextObject(); titleText.Parent = page.ReportTitle; titleText.CreateUniqueName(); titleText.Bounds = new RectangleF(Units.Centimeters * 5, 0, Units.Centimeters * 10, Units.Centimeters * 1); titleText.Font = new Font("Arial", 14, System.Drawing.FontStyle.Bold); titleText.Text = "Employees"; titleText.HorzAlign = HorzAlign.Center; // create data band var dataBand = new DataBand(); page.Bands.Add(dataBand); dataBand.CreateUniqueName(); dataBand.DataSource = report.GetDataSource("Employees"); dataBand.Height = Units.Centimeters * 0.5f; // create two text objects with employee's name and birth date var empNameText = new TextObject(); empNameText.Parent = dataBand; empNameText.CreateUniqueName(); empNameText.Bounds = new RectangleF(0, 0, Units.Centimeters * 5, Units.Centimeters * 0.5f); empNameText.Text = "[Employees.FirstName] [Employees.LastName]"; var empBirthDateText = new TextObject(); empBirthDateText.Parent = dataBand; empBirthDateText.CreateUniqueName(); empBirthDateText.Bounds = new RectangleF(Units.Centimeters * 5.5f, 0, Units.Centimeters * 3, Units.Centimeters * 0.5f); empBirthDateText.Text = "[Employees.BirthDate]"; // format value as date empBirthDateText.Format = new DateFormat() { Format = "MM/dd/yyyy" }; // run report designer report.Design(); } private void MasterDetail_Click(object sender, RoutedEventArgs e) { Report report = new Report(); // register all data tables and relations report.RegisterData(dataSet); // enable the "Categories" and "Products" tables to use it in the report report.GetDataSource("Categories").Enabled = true; report.GetDataSource("Products").Enabled = true; // enable relation between two tables report.Dictionary.UpdateRelations(); // add report page var page = new ReportPage(); report.Pages.Add(page); // always give names to objects you create. You can use CreateUniqueName method to do this; // call it after the object is added to a report. page.CreateUniqueName(); // create master data band var masterDataBand = new DataBand(); page.Bands.Add(masterDataBand); masterDataBand.CreateUniqueName(); masterDataBand.DataSource = report.GetDataSource("Categories"); masterDataBand.Height = Units.Centimeters * 0.5f; // create category name text var categoryText = new TextObject(); categoryText.Parent = masterDataBand; categoryText.CreateUniqueName(); categoryText.Bounds = new RectangleF(0, 0, Units.Centimeters * 5, Units.Centimeters * 0.5f); categoryText.Font = new Font("Arial", 10, System.Drawing.FontStyle.Bold); categoryText.Text = "[Categories.CategoryName]"; // create detail data band var detailDataBand = new DataBand(); masterDataBand.Bands.Add(detailDataBand); detailDataBand.CreateUniqueName(); detailDataBand.DataSource = report.GetDataSource("Products"); detailDataBand.Height = Units.Centimeters * 0.5f; // set sort by product name detailDataBand.Sort.Add(new Sort("[Products.ProductName]")); // create product name text var productText = new TextObject(); productText.Parent = detailDataBand; productText.CreateUniqueName(); productText.Bounds = new RectangleF(Units.Centimeters * 1, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f); productText.Text = "[Products.ProductName]"; // run report designer report.Design(); } private void Group_Click(object sender, RoutedEventArgs e) { Report report = new Report(); // register all data tables and relations report.RegisterData(dataSet); // enable the "Products" table to use it in the report report.GetDataSource("Products").Enabled = true; // add report page var page = new ReportPage(); report.Pages.Add(page); // always give names to objects you create. You can use CreateUniqueName method to do this; // call it after the object is added to a report. page.CreateUniqueName(); // create group header var groupHeaderBand = new GroupHeaderBand(); page.Bands.Add(groupHeaderBand); groupHeaderBand.Height = Units.Centimeters * 1; groupHeaderBand.Condition = "[Products.ProductName].Substring(0,1)"; groupHeaderBand.SortOrder = FastReport.SortOrder.Ascending; // create group text var groupText = new TextObject(); groupText.Parent = groupHeaderBand; groupText.CreateUniqueName(); groupText.Bounds = new RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 1); groupText.Font = new Font("Arial", 14, System.Drawing.FontStyle.Bold); groupText.Text = "[[Products.ProductName].Substring(0,1)]"; groupText.VertAlign = VertAlign.Center; groupText.Fill = new LinearGradientFill(Color.OldLace, Color.Moccasin, 90, 0.5f, 1); // create data band var dataBand = new DataBand(); groupHeaderBand.Data = dataBand; dataBand.CreateUniqueName(); dataBand.DataSource = report.GetDataSource("Products"); dataBand.Height = Units.Centimeters * 0.5f; // create product name text var productText = new TextObject(); productText.Parent = dataBand; productText.CreateUniqueName(); productText.Bounds = new RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f); productText.Text = "[Products.ProductName]"; // create group footer groupHeaderBand.GroupFooter = new GroupFooterBand(); groupHeaderBand.GroupFooter.CreateUniqueName(); groupHeaderBand.GroupFooter.Height = Units.Centimeters * 1; // create total var groupTotal = new Total(); groupTotal.Name = "TotalRows"; groupTotal.TotalType = TotalType.Count; groupTotal.Evaluator = dataBand; groupTotal.PrintOn = groupHeaderBand.GroupFooter; report.Dictionary.Totals.Add(groupTotal); // show total in the group footer var totalText = new TextObject(); totalText.Parent = groupHeaderBand.GroupFooter; totalText.CreateUniqueName(); totalText.Bounds = new RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f); totalText.Text = "Rows: [TotalRows]"; totalText.HorzAlign = HorzAlign.Right; totalText.Border.Lines = BorderLines.Top; // run report designer report.Design(); } private void NestedGroups_Click(object sender, RoutedEventArgs e) { Report report = new Report(); // register all data tables and relations report.RegisterData(dataSet); // enable the "Products" table to use it in the report report.GetDataSource("Products").Enabled = true; // add report page var page = new ReportPage(); report.Pages.Add(page); // always give names to objects you create. You can use CreateUniqueName method to do this; // call it after the object is added to a report. page.CreateUniqueName(); // create group header var groupHeaderBand = new GroupHeaderBand(); page.Bands.Add(groupHeaderBand); groupHeaderBand.Height = Units.Centimeters * 1; groupHeaderBand.Condition = "[Products.ProductName].Substring(0,1)"; // create group text var groupText = new TextObject(); groupText.Parent = groupHeaderBand; groupText.CreateUniqueName(); groupText.Bounds = new RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 1); groupText.Font = new Font("Arial", 14, System.Drawing.FontStyle.Bold); groupText.Text = "[[Products.ProductName].Substring(0,1)]"; groupText.VertAlign = VertAlign.Center; groupText.Fill = new LinearGradientFill(Color.OldLace, Color.Moccasin, 90, 0.5f, 1); // create nested group header var nestedGroupBand = new GroupHeaderBand(); groupHeaderBand.NestedGroup = nestedGroupBand; nestedGroupBand.Height = Units.Centimeters * 0.5f; nestedGroupBand.Condition = "[Products.ProductName].Substring(0,2)"; // create nested group text var nestedText = new TextObject(); nestedText.Parent = nestedGroupBand; nestedText.CreateUniqueName(); nestedText.Bounds = new RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f); nestedText.Font = new Font("Arial", 10, System.Drawing.FontStyle.Bold); nestedText.Text = "[[Products.ProductName].Substring(0,2)]"; // create data band var dataBand = new DataBand(); // connect it to inner group nestedGroupBand.Data = dataBand; dataBand.CreateUniqueName(); dataBand.DataSource = report.GetDataSource("Products"); dataBand.Height = Units.Centimeters * 0.5f; // set sort by product name dataBand.Sort.Add(new Sort("[Products.ProductName]")); // create product name text var productText = new TextObject(); productText.Parent = dataBand; productText.CreateUniqueName(); productText.Bounds = new RectangleF(Units.Centimeters * 0.5f, 0, Units.Centimeters * 9.5f, Units.Centimeters * 0.5f); productText.Text = "[Products.ProductName]"; // create group footer for outer group groupHeaderBand.GroupFooter = new GroupFooterBand(); groupHeaderBand.GroupFooter.CreateUniqueName(); groupHeaderBand.GroupFooter.Height = Units.Centimeters * 1; // create total var groupTotal = new Total(); groupTotal.Name = "TotalRows"; groupTotal.TotalType = TotalType.Count; groupTotal.Evaluator = dataBand; groupTotal.PrintOn = groupHeaderBand.GroupFooter; report.Dictionary.Totals.Add(groupTotal); // show total in the group footer var totalText = new TextObject(); totalText.Parent = groupHeaderBand.GroupFooter; totalText.CreateUniqueName(); totalText.Bounds = new RectangleF(0, 0, Units.Centimeters * 10, Units.Centimeters * 0.5f); totalText.Text = "Rows: [TotalRows]"; totalText.HorzAlign = HorzAlign.Right; totalText.Border.Lines = BorderLines.Top; // run report designer report.Design(); } private void Subreport_Click(object sender, RoutedEventArgs e) { Report report = new Report(); // register all data tables and relations report.RegisterData(dataSet); // enable the "Products" and "Suppliers" tables to use it in the report report.GetDataSource("Products").Enabled = true; report.GetDataSource("Suppliers").Enabled = true; // add report page var page = new ReportPage(); report.Pages.Add(page); // always give names to objects you create. You can use CreateUniqueName method to do this; // call it after the object is added to a report. page.CreateUniqueName(); // create title band page.ReportTitle = new ReportTitleBand(); // native FastReport unit is screen pixel, use conversion page.ReportTitle.Height = Units.Centimeters * 1; page.ReportTitle.CreateUniqueName(); // create two title text objects var titleText1 = new TextObject(); titleText1.Parent = page.ReportTitle; titleText1.CreateUniqueName(); titleText1.Bounds = new RectangleF(0, 0, Units.Centimeters * 8, Units.Centimeters * 1); titleText1.Font = new Font("Arial", 14, System.Drawing.FontStyle.Bold); titleText1.Text = "Products"; titleText1.HorzAlign = HorzAlign.Center; var titleText2 = new TextObject(); titleText2.Parent = page.ReportTitle; titleText2.CreateUniqueName(); titleText2.Bounds = new RectangleF(Units.Centimeters * 9, 0, Units.Centimeters * 8, Units.Centimeters * 1); titleText2.Font = new Font("Arial", 14, System.Drawing.FontStyle.Bold); titleText2.Text = "Suppliers"; titleText2.HorzAlign = HorzAlign.Center; // create report title's child band that will contain subreports var childBand = new ChildBand(); page.ReportTitle.Child = childBand; childBand.CreateUniqueName(); childBand.Height = Units.Centimeters * 0.5f; // create the first subreport var subreport1 = new SubreportObject(); subreport1.Parent = childBand; subreport1.CreateUniqueName(); subreport1.Bounds = new RectangleF(0, 0, Units.Centimeters * 8, Units.Centimeters * 0.5f); // create subreport's page var subreportPage1 = new ReportPage(); report.Pages.Add(subreportPage1); // connect subreport to page subreport1.ReportPage = subreportPage1; // create report on the subreport's page var dataBand = new DataBand(); subreportPage1.Bands.Add(dataBand); dataBand.CreateUniqueName(); dataBand.DataSource = report.GetDataSource("Products"); dataBand.Height = Units.Centimeters * 0.5f; var productText = new TextObject(); productText.Parent = dataBand; productText.CreateUniqueName(); productText.Bounds = new RectangleF(0, 0, Units.Centimeters * 8, Units.Centimeters * 0.5f); productText.Text = "[Products.ProductName]"; // create the second subreport var subreport2 = new SubreportObject(); subreport2.Parent = childBand; subreport2.CreateUniqueName(); subreport2.Bounds = new RectangleF(Units.Centimeters * 9, 0, Units.Centimeters * 8, Units.Centimeters * 0.5f); // create subreport's page var subreportPage2 = new ReportPage(); report.Pages.Add(subreportPage2); // connect subreport to page subreport2.ReportPage = subreportPage2; // create report on the subreport's page var dataBand2 = new DataBand(); subreportPage2.Bands.Add(dataBand2); dataBand2.CreateUniqueName(); dataBand2.DataSource = report.GetDataSource("Suppliers"); dataBand2.Height = Units.Centimeters * 0.5f; // create supplier name text var supplierText = new TextObject(); supplierText.Parent = dataBand2; supplierText.CreateUniqueName(); supplierText.Bounds = new RectangleF(0, 0, Units.Centimeters * 8, Units.Centimeters * 0.5f); supplierText.Text = "[Suppliers.CompanyName]"; // run report designer report.Design(); } private void TableObject_Click(object sender, RoutedEventArgs e) { Report report = new Report(); // add report page var page = new ReportPage(); page.Name = "Page1"; report.Pages.Add(page); // create data band var dataBand = new DataBand(); dataBand.Name = "DataBand"; page.Bands.Add(dataBand); // create Table object with 10 rows and 10 columns var table = new TableObject(); table.Name = "Table1"; table.RowCount = 10; table.ColumnCount = 10; for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) { table[j, i].Text = (10 * i + j + 1).ToString(); table[j, i].Border.Lines = BorderLines.All; } dataBand.Objects.Add(table); table.CreateUniqueNames(); dataBand.Height = table.CalcHeight(); // run report designer report.Design(); } } }