Files
2018-08-22 21:59:07 +00:00

108 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraReports.UI;
using System.IO;
namespace GZTW.AyaNova.BLL
{
/// <summary>
/// Generate AyaNova reports from code
/// </summary>
public class ReportGenerator
{
/// <summary>
/// Generates a pdf file of report and returns in memory stream
/// </summary>
/// <param name="r">AyaNova Report object</param>
/// <param name="DataSource">Data source for report</param>
/// <returns>A pdf file in memory stream</returns>
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);
}
}
/// <summary>
/// Localize the text on the report if it starts with LT
/// </summary>
/// <param name="c"></param>
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;
}
}
}