using System; using System.Collections.Generic; using System.Linq; using System.Text; using DevExpress.XtraReports.UI; using System.IO; namespace GZTW.AyaNova.BLL { /// /// Generate AyaNova reports from code /// public class ReportGenerator { /// /// Generates a pdf file of report and returns in memory stream /// /// AyaNova Report object /// Data source for report /// A pdf file in memory stream public static MemoryStream GetReportAsPDF(Report r, object DataSource) { ReportDataSet source = null; if (DataSource is ReportDataSet) source = (ReportDataSet)DataSource; else { ReportDataSetAdapter rdsa = new ReportDataSetAdapter(); source = new ReportDataSet(); rdsa.Fill(source, DataSource); } //test with data portal and remove this if not necessary if (AyaBizUtils.LocaleText == null) throw new System.ApplicationException("Generate report -> AyaBizUtils.LocaleText is null"); // source.LocaleTextTable = AyaBizUtils.LocaleText; XtraReport x = new XtraReport(); x.LoadLayout(r.GetReportContent()); LocalizeReport(x); if (source.Tables.Count == 1) { //It's a summary report so just bind to the report as normal x.DataSource = source; } else { //It's a detailed report made of subreports so remove the datasource //from the main report and set it on the detailereport band x.DataSource = null; DetailReportBand detailReport = x.Bands[BandKind.DetailReport] as DetailReportBand; if (detailReport != null) { detailReport.DataSource = source; } } //case 3646 removed // if (AyaBizUtils.Trial) WatermarkReport(x); MemoryStream ms = new MemoryStream(); x.ExportToPdf(ms); return ms; } static private void LocalizeReport(XtraReport x) { foreach (Band b in x.Bands) { LocalizeReportControl(b); } } /// /// Localize the text on the report if it starts with LT /// /// static private void LocalizeReportControl(XRControl c) { if (c.Text.StartsWith("LT")) c.Text = AyaBizUtils.LocaleText.GetLocalizedText(c.Text.Replace("LT:", "")); foreach (XRControl d in c.Controls) LocalizeReportControl(d); } static internal void WatermarkReport(XtraReport x) { x.Watermark.Text = "TRIAL SAMPLE"; x.Watermark.Font = new System.Drawing.Font("Tahoma", 72, System.Drawing.FontStyle.Bold); x.Watermark.TextTransparency = 128; x.Watermark.ShowBehind = false; } } }