ReportUtils.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. //using Ghostscript.NET;
  2. //using Ghostscript.NET.Processor;
  3. using System.Data;
  4. using System.Drawing.Printing;
  5. using System.IO;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Forms.Integration;
  9. using FastReport;
  10. using FastReport.Data;
  11. using FastReport.Design;
  12. using FastReport.Design.StandardDesigner;
  13. using FastReport.Export.Pdf;
  14. using FastReport.Utils;
  15. using InABox.Clients;
  16. using InABox.Core;
  17. using InABox.Reports.Common;
  18. using InABox.Scripting;
  19. using InABox.WPF;
  20. using MessageBox = System.Windows.Forms.MessageBox;
  21. using SaveFileDialog = Microsoft.Win32.SaveFileDialog;
  22. using ScriptEditor = InABox.DynamicGrid.ScriptEditor;
  23. using XmlDocument = System.Xml.XmlDocument;
  24. namespace InABox.Reports
  25. {
  26. public enum ReportExportType
  27. {
  28. PDF,
  29. HTML,
  30. RTF,
  31. Excel,
  32. Text,
  33. Image
  34. }
  35. public class ReportExportDefinition
  36. {
  37. public ReportExportDefinition(string caption, Bitmap image, ReportExportType type, Action<DataModel, byte[]> action)
  38. {
  39. Caption = caption;
  40. Image = image;
  41. Type = type;
  42. Action = action;
  43. }
  44. public string Caption { get; }
  45. public Bitmap Image { get; }
  46. public ReportExportType Type { get; }
  47. public Action<DataModel, byte[]> Action { get; }
  48. }
  49. [LibraryInitializer]
  50. public static class ReportUtils
  51. {
  52. public static List<ReportExportDefinition> ExportDefinitions { get; } = new();
  53. public static void RegisterClasses()
  54. {
  55. CoreUtils.RegisterClasses(typeof(ReportTemplate).Assembly, typeof(ReportUtils).Assembly);
  56. foreach (string printer in PrinterSettings.InstalledPrinters)
  57. ReportPrinters.Register(printer);
  58. RegisteredObjects.Add(typeof(MultiImageObject), "ReportPage", Properties.Resources.multi_image, "Multi-Image");
  59. RegisteredObjects.Add(typeof(MultiSignatureObject), "ReportPage", Properties.Resources.signature, "Multi-Signature");
  60. }
  61. public static void PreviewReport(ReportTemplate template, DataModel data, bool printandclose = false, bool allowdesigner = false)
  62. {
  63. var isrdl = template != null && template.IsRDL;
  64. if (isrdl)
  65. {
  66. MessageBox.Show("RDL Reports are no longer supported!");
  67. }
  68. else
  69. {
  70. PreviewWindow window = new(template, data)
  71. {
  72. AllowDesign = allowdesigner,
  73. Title = string.Format("Report Preview - {0}", template.Name),
  74. Height = 800,
  75. Width = 1200,
  76. WindowState = WindowState.Maximized,
  77. };
  78. window.Show();
  79. }
  80. }
  81. public static Report SetupReport(ReportTemplate template, DataModel data, bool repopulate)
  82. {
  83. var templaterdl = template != null ? string.IsNullOrWhiteSpace(template.RDL) ? "" : template.RDL : "";
  84. var tables = new List<string>();
  85. if (!string.IsNullOrWhiteSpace(templaterdl))
  86. {
  87. var xml = new XmlDocument();
  88. xml.LoadString(templaterdl);
  89. var datasources = xml.GetElementsByTagName("TableDataSource");
  90. for (var i = 0; i < datasources.Count; i++)
  91. {
  92. var datasource = datasources[i];
  93. tables.Add(datasource.Attributes.GetNamedItem("Name").Value);
  94. }
  95. }
  96. else
  97. {
  98. foreach(var table in data.DefaultTables)
  99. {
  100. tables.Add(table.TableName);
  101. }
  102. }
  103. var report = new Report();
  104. report.LoadFromString(templaterdl);
  105. report.FileName = template.Name;
  106. foreach(var tableName in data.TableNames)
  107. {
  108. var dataSource = report.GetDataSource(tableName);
  109. if (dataSource != null)
  110. {
  111. var modelTable = data.GetDataModelTable(tableName);
  112. var columnNames = CoreUtils.GetColumnNames(modelTable.Type, x => true);
  113. foreach (var column in dataSource.Columns)
  114. {
  115. if(column is FastReport.Data.Column col && !col.Enabled)
  116. {
  117. //columns.Add(col.Name.Replace('_','.'));
  118. columnNames.Remove(col.Name.Replace('_', '.'));
  119. }
  120. /*if (column is FastReport.Data.Column col && col.DataType != null && col.DataType.IsAssignableTo(typeof(IEnumerable<byte[]>)))
  121. {
  122. col.BindableControl = ColumnBindableControl.Custom;
  123. col.CustomBindableControl = "MultiImageObject";
  124. }*/
  125. }
  126. var columns = Columns.Create(modelTable.Type);
  127. foreach(var column in columnNames)
  128. {
  129. columns.Add(column);
  130. }
  131. modelTable.Columns = columns;
  132. }
  133. }
  134. var script = new ScriptDocument(template.Script);
  135. var ScriptOK = false;
  136. if (!string.IsNullOrWhiteSpace(template.Script))
  137. if (script.Compile())
  138. {
  139. script.SetValue("Model", data);
  140. ScriptOK = script.Execute("Report", "Init");
  141. }
  142. if (repopulate)
  143. data.LoadModel(tables);
  144. if (ScriptOK)
  145. {
  146. script.SetValue("RequireTables", tables);
  147. script.Execute("Report", "Populate");
  148. }
  149. var ds = data.AsDataSet();
  150. report.RegisterData(ds);
  151. foreach (var tableName in data.TableNames)
  152. {
  153. var columns = data.GetColumns(tableName)?.AsDictionary() ?? new Dictionary<string, Type>();
  154. var dataSource = report.GetDataSource(tableName);
  155. if(dataSource != null)
  156. {
  157. foreach (var column in dataSource.Columns)
  158. {
  159. if (column is FastReport.Data.Column col)
  160. {
  161. if (col.DataType != null && col.DataType.IsAssignableTo(typeof(IEnumerable<byte[]>)))
  162. {
  163. col.BindableControl = ColumnBindableControl.Custom;
  164. col.CustomBindableControl = "MultiImageObject";
  165. }
  166. col.Enabled = columns.ContainsKey(col.Name.Replace('_', '.'));
  167. }
  168. }
  169. }
  170. }
  171. if (string.IsNullOrWhiteSpace(templaterdl))
  172. {
  173. foreach (var table in data.DefaultTables)
  174. {
  175. var dataSource = report.GetDataSource(table.TableName);
  176. dataSource.Enabled = true;
  177. }
  178. foreach (Relation relation in report.Dictionary.Relations)
  179. if (data.DefaultTables.Any(x => x.TableName.Equals(relation.ParentDataSource.Alias)) &&
  180. data.DefaultTables.Any(x => x.TableName.Equals(relation.ChildDataSource.Alias)))
  181. relation.Enabled = true;
  182. }
  183. return report;
  184. }
  185. public static void DesignReport(ReportTemplate template, DataModel data, bool populate = false)
  186. {
  187. var isrdl = template != null && template.IsRDL;
  188. var templaterdl = template != null ? string.IsNullOrWhiteSpace(template.RDL) ? "" : template.RDL : "";
  189. if (isrdl)
  190. MessageBox.Show("RDL Reports are not supported!");
  191. else
  192. {
  193. PreviewWindow window = new(template, data)
  194. {
  195. AllowDesign = true,
  196. Title = string.Format("Report Designer - {0}", template.Name),
  197. Height = 800,
  198. Width = 1200,
  199. WindowState = WindowState.Maximized,
  200. IsPreview = false,
  201. ShouldPopulate = populate
  202. };
  203. window.Show();
  204. }
  205. }
  206. public static byte[] ReportToPDF(ReportTemplate template, DataModel data, bool repopulate = true)
  207. {
  208. byte[] result = null;
  209. if (template.IsRDL)
  210. MessageBox.Show("RDL Reports are not supported!");
  211. // using (var pdf = new MemoryStream())
  212. // {
  213. // using (var ms = ReportUtils.SetupReportToStream(template.RDL, data.AsDictionary))
  214. // {
  215. // using (ReportWriter reportWriter = new ReportWriter(ms))
  216. // {
  217. // reportWriter.ReportProcessingMode = Syncfusion.ReportWriter.ProcessingMode.Local;
  218. // reportWriter.Save(pdf, WriterFormat.PDF);
  219. //
  220. // //String filename = Path.Combine(GetPath(), "report.pdf");
  221. // //reportWriter.Save(filename, WriterFormat.PDF);
  222. // //PdfLoadedDocument rpt = new PdfLoadedDocument(filename);
  223. //
  224. // PdfLoadedDocument rpt = new PdfLoadedDocument(pdf);
  225. // PdfDocument doc = new PdfDocument(PdfConformanceLevel.Pdf_A1B);
  226. // PdfDocument.Merge(doc, rpt);
  227. // var msFinal = new MemoryStream();
  228. // doc.Save(msFinal);
  229. // result = msFinal.GetBuffer();
  230. // }
  231. // }
  232. // //result = GhostScriptIt(pdf.GetBuffer());
  233. // //result = pdf.GetBuffer();
  234. // }
  235. else
  236. using (var report = SetupReport(template, data, repopulate))
  237. {
  238. report.Prepare();
  239. var ms = new MemoryStream();
  240. report.Export(new PDFExport(), ms);
  241. result = ms.GetBuffer();
  242. }
  243. return result;
  244. }
  245. public static IEnumerable<ReportTemplate> LoadReports(string sectionName, DataModel model)
  246. {
  247. return new Client<ReportTemplate>().Query(
  248. new Filter<ReportTemplate>(x => x.DataModel).IsEqualTo(model.Name)
  249. .And(x => x.Section).IsEqualTo(sectionName),
  250. null,
  251. new SortOrder<ReportTemplate>(x => x.Name)).Rows.Select(x => x.ToObject<ReportTemplate>());
  252. }
  253. private static void PopulateMenu(ItemsControl menu, string sectionName, DataModel model, bool allowdesign, bool populate = false)
  254. {
  255. var reports = new Client<ReportTemplate>().Query(
  256. new Filter<ReportTemplate>(x => x.DataModel).IsEqualTo(model.Name)
  257. .And(x => x.Section).IsEqualTo(sectionName),
  258. null,
  259. new SortOrder<ReportTemplate>(x => x.Name)
  260. );
  261. foreach (var row in reports.Rows)
  262. {
  263. var print = new MenuItem();
  264. print.Header = row.Get<ReportTemplate, string>(x => x.Name);
  265. print.Tag = row.ToObject<ReportTemplate>();
  266. print.Click += (sender, args) =>
  267. {
  268. var template = (sender as MenuItem)!.Tag as ReportTemplate;
  269. if (template != null)
  270. PreviewReport(template, model, false, allowdesign);
  271. };
  272. menu.Items.Add(print);
  273. }
  274. if (allowdesign)
  275. {
  276. if (reports.Rows.Any())
  277. menu.Items.Add(new Separator());
  278. var manage = new MenuItem();
  279. manage.Header = "Manage Reports";
  280. manage.Click += (sender, args) =>
  281. {
  282. var manager = new ReportManager()
  283. {
  284. DataModel = model,
  285. Section = sectionName,
  286. Populate = populate
  287. };
  288. manager.ShowDialog();
  289. };
  290. menu.Items.Add(manage);
  291. }
  292. }
  293. public static void PopulateMenu(MenuItem menu, string sectionName, DataModel model, bool allowdesign, bool populate = false) =>
  294. PopulateMenu(menu as ItemsControl, sectionName, model, allowdesign, populate);
  295. public static void PopulateMenu(ContextMenu menu, string sectionName, DataModel model, bool allowdesign, bool populate = false) =>
  296. PopulateMenu(menu as ItemsControl, sectionName, model, allowdesign, populate);
  297. public static void PrintMenu(FrameworkElement? element, string sectionName, DataModel model, bool allowdesign, bool populate = false)
  298. {
  299. var menu = new ContextMenu();
  300. PopulateMenu(menu, sectionName, model, allowdesign, populate);
  301. if (menu.Items.Count > 0)
  302. {
  303. menu.PlacementTarget = element;
  304. menu.IsOpen = true;
  305. }
  306. }
  307. public static void PrintMenu<TType>(FrameworkElement? element, string sectionName, DataModel<TType> model, bool allowdesign, bool populate = false)
  308. where TType : Entity, IRemotable, IPersistent, new()
  309. {
  310. PrintMenu(element, sectionName, model, allowdesign, populate);
  311. }
  312. #region Old RDL Stuff (Deprecated)
  313. // private static ReportDefinition SetupReportDefinition(String rdl, Dictionary<System.Type, CoreTable> dataenvironment = null)
  314. // {
  315. // ReportDefinition report = null;
  316. // if (String.IsNullOrWhiteSpace(rdl))
  317. // report = CreateReport();
  318. // else
  319. // report = DeserialiseReport(rdl);
  320. //
  321. // if (dataenvironment != null)
  322. // SetupReportData(report, dataenvironment);
  323. // return report;
  324. // }
  325. // public static String SetupReport(String rdl, Dictionary<System.Type, CoreTable> dataenvironment = null)
  326. // {
  327. // var defn = SetupReportDefinition(rdl, dataenvironment);
  328. // return SerialiseReport(defn);
  329. // }
  330. //
  331. // public static MemoryStream SetupReportToStream(String rdl, Dictionary<System.Type, CoreTable> dataenvironment = null)
  332. // {
  333. // var defn = SetupReportDefinition(rdl, dataenvironment);
  334. // return SerialiseReportToStream(defn);
  335. // }
  336. // public static String CleanupReport(String rdl)
  337. // {
  338. // ReportDefinition report = null;
  339. // if (String.IsNullOrWhiteSpace(rdl))
  340. // report = CreateReport();
  341. // else
  342. // report = DeserialiseReport(rdl);
  343. //
  344. // CleanupReportData(report);
  345. //
  346. // return SerialiseReport(report);
  347. //
  348. // }
  349. // private static ReportDefinition CreateReport()
  350. // {
  351. // ReportDefinition rd = new ReportDefinition();
  352. // rd.DataSources = new DataSources();
  353. // rd.DataSets = new DataSets();
  354. //
  355. // rd.ReportSections = new ReportSections();
  356. // rd.ReportSections.Add(
  357. // new ReportSection()
  358. // {
  359. // Width = new Syncfusion.RDL.DOM.Size("7.5in"),
  360. // Body = new Body()
  361. // {
  362. // Style = new Syncfusion.RDL.DOM.Style() { BackgroundColor = "White", Border = new Syncfusion.RDL.DOM.Border() },
  363. // Height = new Syncfusion.RDL.DOM.Size("10in"),
  364. //
  365. // },
  366. // Page = new Syncfusion.RDL.DOM.Page()
  367. // {
  368. // PageHeight = new Syncfusion.RDL.DOM.Size("11in"),
  369. // PageWidth = new Syncfusion.RDL.DOM.Size("8.5in"),
  370. // Style = new Syncfusion.RDL.DOM.Style() { BackgroundColor = "White", Border = new Syncfusion.RDL.DOM.Border() },
  371. // }
  372. // }
  373. // );
  374. // rd.EmbeddedImages = new EmbeddedImages();
  375. //
  376. // rd.RDLType = RDLType.RDL2010;
  377. // rd.CodeModules = new CodeModules();
  378. // rd.CodeModules.Add(new CodeModule() { Value = "System.Drawing, Version = 2.0.0.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a" });
  379. // rd.CodeModules.Add(new CodeModule() { Value = "Syncfusion.Pdf.Base, Version = 13.3350.0.7, Culture = neutral, PublicKeyToken = 3d67ed1f87d44c89" });
  380. // rd.CodeModules.Add(new CodeModule() { Value = "Syncfusion.Linq.Base, Version = 13.3350.0.7, Culture = neutral, PublicKeyToken = 3d67ed1f87d44c89" });
  381. //
  382. // rd.Classes = new Classes();
  383. // rd.Classes.Add(new Class() { ClassName = "Syncfusion.Pdf.Barcode.PDFCode39Barcode", InstanceName = "Code39" });
  384. // rd.Classes.Add(new Class() { ClassName = "Syncfusion.Pdf.Barcode.PdfQRBarcode", InstanceName = "QRCode" });
  385. //
  386. // rd.Code = @"
  387. // Public Function Code39BarCode(Text As String) As String
  388. // Dim _msg as String
  389. // try
  390. // Code39.BarHeight = 100
  391. // Code39.Text = Text
  392. // Code39.EnableCheckDigit = True
  393. // Code39.EncodeStartStopSymbols = True
  394. // Code39.ShowCheckDigit = False
  395. // Code39.TextDisplayLocation = 0
  396. // Dim _img As System.Drawing.Image = Code39.ToImage()
  397. // Dim ms As New System.IO.MemoryStream()
  398. // _img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
  399. // Dim _bytes = ms.ToArray()
  400. // Return System.Convert.ToBase64String(_bytes, 0, _bytes.Length)
  401. // Catch ex As Exception
  402. // _msg = ex.toString()
  403. // End Try
  404. // return Nothing
  405. // End Function
  406. //
  407. // Public Function QRBarCode(Text As String) As String
  408. // Dim _msg as String
  409. // try
  410. // QRCode.Text = Text
  411. // QRCode.XDimension = 4
  412. // Dim _img As System.Drawing.Image = QRCode.ToImage()
  413. // Dim ms As New System.IO.MemoryStream()
  414. // _img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp)
  415. // Dim _bytes = ms.ToArray()
  416. // Return System.Convert.ToBase64String(_bytes, 0, _bytes.Length)
  417. // Catch ex As Exception
  418. // _msg = ex.toString()
  419. // End Try
  420. // return Nothing
  421. // End Function
  422. //
  423. // ";
  424. // return rd;
  425. //
  426. // //MemoryStream memoryStream = new MemoryStream();
  427. // //TextWriter ws2 = new StreamWriter(memoryStream);
  428. // //xs2.Serialize(ws2, rd, serialize);
  429. // //memoryStream.Position = 0;
  430. // ////To save to a file.
  431. // //StringWriter ws3 = new StringWriter
  432. // //TextWriter ws3 = new StreamWriter(_tempfile);
  433. // //xs2.Serialize(ws3, rd, serialize);
  434. // //ws2.Close();
  435. // //ws3.Close();
  436. // //memoryStream.Close();
  437. //
  438. // }
  439. private static string DataTableToXML(Type type, CoreTable table)
  440. {
  441. var doc = new XmlDocument();
  442. var root = doc.CreateElement(string.Empty, type.Name.ToLower() + "s", string.Empty);
  443. doc.AppendChild(root);
  444. //var properties = CoreUtils.PropertyList(type, x => true, true);
  445. if (table != null)
  446. {
  447. if (table.Rows.Any())
  448. {
  449. foreach (var datarow in table.Rows)
  450. {
  451. var row = doc.CreateElement(string.Empty, type.Name.ToLower(), string.Empty);
  452. foreach (var column in table.Columns)
  453. {
  454. var field = doc.CreateElement(string.Empty, column.ColumnName.Replace('.', '_'), string.Empty);
  455. var oVal = datarow[column.ColumnName];
  456. var value = doc.CreateTextNode(oVal != null ? oVal.ToString() : "");
  457. field.AppendChild(value);
  458. row.AppendChild(field);
  459. }
  460. root.AppendChild(row);
  461. }
  462. }
  463. else
  464. {
  465. var row = doc.CreateElement(string.Empty, type.Name.ToLower(), string.Empty);
  466. foreach (var column in table.Columns)
  467. {
  468. var field = doc.CreateElement(string.Empty, column.ColumnName.Replace('.', '_'), string.Empty);
  469. var value = doc.CreateTextNode("");
  470. field.AppendChild(value);
  471. row.AppendChild(field);
  472. }
  473. root.AppendChild(row);
  474. }
  475. }
  476. var xmlString = "";
  477. using (var wr = new StringWriter())
  478. {
  479. doc.Save(wr);
  480. xmlString = wr.ToString();
  481. }
  482. return xmlString.Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n", "");
  483. }
  484. public static DataSet CreateReportDataSource(Dictionary<Type, CoreTable> dataenvironment)
  485. {
  486. var ds = new DataSet();
  487. foreach (var key in dataenvironment.Keys)
  488. {
  489. var table = dataenvironment[key];
  490. var dt = new DataTable(key.EntityName().Split('.').Last());
  491. foreach (var column in table.Columns)
  492. dt.Columns.Add(column.ColumnName);
  493. foreach (var row in table.Rows)
  494. dt.Rows.Add(row.Values.ToArray());
  495. ds.Tables.Add(dt);
  496. }
  497. return ds;
  498. }
  499. // private static void SetupReportData(ReportDefinition report, Dictionary<System.Type,CoreTable> dataenvironment)
  500. // {
  501. //
  502. // report.DataSets.Clear();
  503. // report.DataSources.Clear();
  504. // foreach (System.Type type in dataenvironment.Keys)
  505. // {
  506. // String _tempfile = Path.GetTempFileName();
  507. // String xml = DataTableToXML(type, dataenvironment[type]);
  508. // File.WriteAllText(_tempfile, xml);
  509. // // Create DataSource(s)
  510. // DataSource datasource = new DataSource()
  511. // {
  512. // Name = type.Name,
  513. // ConnectionProperties = new ConnectionProperties()
  514. // {
  515. // DataProvider = "XML",
  516. // IntegratedSecurity = true,
  517. // ConnectString = _tempfile
  518. // },
  519. // SecurityType = SecurityType.None,
  520. // };
  521. // report.DataSources.Add(datasource);
  522. //
  523. // Syncfusion.RDL.DOM.DataSet dataset = new Syncfusion.RDL.DOM.DataSet() { Name = type.Name };
  524. // dataset.Fields = new Fields();
  525. //
  526. // CoreTable table = dataenvironment[type];
  527. // foreach (var column in table.Columns)
  528. // {
  529. // dataset.Fields.Add(
  530. // new Field()
  531. // {
  532. // Name = column.ColumnName.Replace('.', '_'),
  533. // DataField = column.ColumnName.Replace('.', '_'),
  534. // TypeName = column.DataType.Name
  535. // }
  536. // );
  537. // }
  538. //
  539. // //var properties = CoreUtils.PropertyList(type, x => true, true);
  540. //
  541. // //// Create DataSet(s)
  542. // //foreach (String key in properties.Keys)
  543. // //{
  544. // // dataset.Fields.Add(
  545. // // new Field()
  546. // // {
  547. // // Name = key.Replace('.', '_'),
  548. // // DataField = key.Replace('.', '_'),
  549. // // TypeName = properties[key].Name
  550. // // }
  551. // // );
  552. // //}
  553. // dataset.Query = new Query()
  554. // {
  555. // DataSourceName = type.Name,
  556. // CommandType = Syncfusion.RDL.DOM.CommandType.Text,
  557. // CommandText = String.Format("{0}s/{0}", type.Name.ToLower())
  558. // };
  559. // report.DataSets.Add(dataset);
  560. // }
  561. // }
  562. //
  563. // private static void CleanupReportData(ReportDefinition report)
  564. // {
  565. // foreach (var ds in report.DataSources)
  566. // {
  567. // String _tempfile = ds.ConnectionProperties.ConnectString;
  568. // if (File.Exists(_tempfile))
  569. // File.Delete(_tempfile);
  570. // ds.ConnectionProperties.ConnectString = "";
  571. // }
  572. // report.DataSources[0].ConnectionProperties.ConnectString = "";
  573. // }
  574. //
  575. // private static ReportDefinition DeserialiseReport(String rdl)
  576. // {
  577. // String data = rdl;
  578. // try
  579. // {
  580. // byte[] debase64 = Convert.FromBase64String(rdl);
  581. // data = Encoding.UTF8.GetString(debase64);
  582. // }
  583. // catch (Exception e)
  584. // {
  585. // Logger.Send(LogType.Error, "", String.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  586. // }
  587. // ReportDefinition report = null;
  588. // XmlSerializer xs = new XmlSerializer(typeof(ReportDefinition), "http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition");
  589. // using (StringReader stringreader = new StringReader(data)) // root.ToString()))
  590. // {
  591. // report = (ReportDefinition)xs.Deserialize(stringreader);
  592. // }
  593. // return report;
  594. // }
  595. //
  596. // [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
  597. // private static String SerialiseReport(ReportDefinition report)
  598. // {
  599. // var stream = SerialiseReportToStream(report);
  600. // return Encoding.UTF8.GetString(stream.ToArray());
  601. //
  602. // }
  603. //
  604. // [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
  605. // private static MemoryStream SerialiseReportToStream(ReportDefinition report)
  606. // {
  607. //
  608. // string nameSpace = "http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition";
  609. //
  610. // if (report.RDLType == RDLType.RDL2010)
  611. // nameSpace = "http://schemas.microsoft.com/sqlserver/reporting/2010/01/reportdefinition";
  612. //
  613. // XmlSerializerNamespaces serialize = new XmlSerializerNamespaces();
  614. // serialize.Add("rd", "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner");
  615. // XmlSerializer xs2 = new XmlSerializer(typeof(Syncfusion.RDL.DOM.ReportDefinition), nameSpace);
  616. // MemoryStream memoryStream = new MemoryStream();
  617. // TextWriter ws2 = new StreamWriter(memoryStream);
  618. // xs2.Serialize(ws2, report, serialize);
  619. // memoryStream.Position = 0;
  620. // return memoryStream;
  621. // //return Encoding.UTF8.GetString(memoryStream.ToArray());
  622. //
  623. // }
  624. // private static byte[] GhostScriptIt(byte[] pdf)
  625. // {
  626. // String input = Path.GetTempFileName();
  627. // File.WriteAllBytes(input, pdf);
  628. // byte[] result = new byte[] { };
  629. // GhostscriptVersionInfo gv = new GhostscriptVersionInfo(
  630. // new Version(0, 0, 0),
  631. // "gsdll32.dll",
  632. // string.Empty,
  633. // GhostscriptLicense.GPL
  634. // );
  635. // GhostscriptPipedOutput gsPipedOutput = new GhostscriptPipedOutput();
  636. // // pipe handle format: %handle%hexvalue
  637. // string outputPipeHandle = "%handle%" + int.Parse(gsPipedOutput.ClientHandle).ToString("X2");
  638. // using (GhostscriptProcessor processor = new GhostscriptProcessor(gv, true))
  639. // {
  640. // List<string> switches = new List<string>();
  641. // switches.Add("-empty");
  642. // switches.Add("-dQUIET");
  643. // switches.Add("-dSAFER");
  644. // switches.Add("-dBATCH");
  645. // switches.Add("-dNOPAUSE");
  646. // switches.Add("-dNOPROMPT");
  647. // switches.Add("-dCompatibilityLevel=1.4");
  648. // switches.Add("-sDEVICE=pdfwrite");
  649. // switches.Add("-o" + outputPipeHandle);
  650. // switches.Add("-q");
  651. // switches.Add("-f");
  652. // switches.Add(input);
  653. // try
  654. // {
  655. // processor.StartProcessing(switches.ToArray(), null);
  656. // result = gsPipedOutput.Data;
  657. // }
  658. // catch (Exception ex)
  659. // {
  660. // Console.WriteLine(ex.Message);
  661. // }
  662. // finally
  663. // {
  664. // gsPipedOutput.Dispose();
  665. // gsPipedOutput = null;
  666. // }
  667. // }
  668. // File.Delete(input);
  669. // return result;
  670. // }
  671. #endregion
  672. }
  673. }