ExportBase.DesignExt.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using FastReport.Preview;
  2. using FastReport.Utils;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Windows.Forms;
  8. namespace FastReport.Export
  9. {
  10. partial class ExportBase
  11. {
  12. private bool tabsIsExists = false;
  13. public bool TabsIsExists
  14. {
  15. get { return tabsIsExists; }
  16. set { tabsIsExists = value; }
  17. }
  18. #region Public Methods
  19. /// <summary>
  20. /// Exports the report to a file.
  21. /// </summary>
  22. /// <param name="report">Report to export.</param>
  23. /// <param name="pages">Pages of open tabs.</param>
  24. /// <returns><b>true</b> if report was succesfully exported.</returns>
  25. /// <remarks>
  26. /// This method displays an export options dialog, then prompts a file name using standard "Open file"
  27. /// dialog. If both dialogs were closed by OK button, exports the report and returns <b>true</b>.
  28. /// </remarks>
  29. public bool Export(Report report, List<PreparedPages> pages = null)
  30. {
  31. SetReport(report);
  32. if (AllowSaveSettings)
  33. RestoreSettings();
  34. tabsIsExists = pages != null && pages.Count > 1;
  35. if (ShowDialog())
  36. {
  37. if (AllowSaveSettings)
  38. SaveSettings();
  39. int indexReportInTabs = tabsIsExists ? pages.IndexOf(report.PreparedPages) : -1;
  40. int reportCountPage = report.PreparedPages.Count;
  41. int countSourcePage = 0;
  42. while (true)
  43. {
  44. using (SaveFileDialog dialog = new SaveFileDialog())
  45. {
  46. dialog.Filter = FileFilter;
  47. string defaultExt = dialog.Filter.Split('|')[1];
  48. dialog.DefaultExt = Path.GetExtension(defaultExt);
  49. dialog.FileName = GetFileName(report) + "." + dialog.DefaultExt;
  50. countSourcePage = 0;
  51. if (!string.IsNullOrEmpty(SaveInitialDirectory))
  52. dialog.InitialDirectory = SaveInitialDirectory;
  53. if (dialog.ShowDialog() == DialogResult.OK)
  54. {
  55. Config.DoEvent();
  56. try
  57. {
  58. if (exportTabs && pages != null && pages.Count > 1)
  59. {
  60. for (int i = 0; i < pages.Count; i++)
  61. {
  62. if (i != indexReportInTabs)
  63. {
  64. int count = report.PreparedPages.Count;
  65. for (int j = 0; j < pages[i].Count; j++)
  66. {
  67. report.PreparedPages.AddPage(new ReportPage());
  68. int pageIndex = report.PreparedPages.Count - 1;
  69. if (i < indexReportInTabs)
  70. {
  71. for (int k = pageIndex; k >= report.PreparedPages.Count - reportCountPage; k--)
  72. {
  73. report.PreparedPages.ModifyPage(k, report.PreparedPages.GetPage(k - 1));
  74. }
  75. pageIndex -= reportCountPage;
  76. }
  77. ReportPage sourcePage = pages[i].GetPage(j);
  78. countSourcePage++;
  79. report.PreparedPages.AddSourcePage(sourcePage);
  80. report.PreparedPages.ModifyPage(pageIndex, sourcePage);
  81. }
  82. }
  83. }
  84. }
  85. Export(report, dialog.FileName);
  86. return true;
  87. }
  88. catch (IOException ex)
  89. {
  90. if (MessageBox.Show(ex.Message + "\r\n\r\n" + Res.Get("Messages,SaveToAnotherFile"),
  91. Res.Get("Messages,Error"),
  92. MessageBoxButtons.YesNo, MessageBoxIcon.Error) == DialogResult.No)
  93. return false;
  94. }
  95. finally
  96. {
  97. if (exportTabs && pages != null && pages.Count > 1)
  98. {
  99. for (int i = pages.Count - 1; i >= 0; i--)
  100. {
  101. if (i != indexReportInTabs)
  102. {
  103. for (int j = 0; j < pages[i].Count; j++)
  104. {
  105. int pageIndex = report.PreparedPages.Count - 1;
  106. if (i < indexReportInTabs)
  107. {
  108. pageIndex -= reportCountPage;
  109. }
  110. report.PreparedPages.RemovePage(pageIndex);
  111. }
  112. }
  113. }
  114. for(int i = 0;i < countSourcePage; i++)
  115. {
  116. report.PreparedPages.SourcePages.RemoveLast();
  117. }
  118. }
  119. }
  120. }
  121. else
  122. {
  123. return false;
  124. }
  125. }
  126. }
  127. }
  128. return false;
  129. }
  130. /// <summary>
  131. /// Displays a dialog with export options.
  132. /// </summary>
  133. /// <returns><b>true</b> if dialog was closed with OK button.</returns>
  134. public virtual bool ShowDialog()
  135. {
  136. return true;
  137. }
  138. #endregion Public Methods
  139. #region Private Methods
  140. private void ShowPerformance(int exportTickCount)
  141. {
  142. if (Report.Preview != null && Config.ReportSettings.ShowPerformance)
  143. Report.Preview.ShowPerformance(String.Format(Res.Get("Export,Misc,Performance"), exportTickCount));
  144. }
  145. /// <summary>
  146. /// Gets a report page with OverlayBand if it is a Demo or Academic.
  147. /// </summary>
  148. /// <param name="page">The prepared report page</param>
  149. /// <returns>The prepared report page with OverlayBand.</returns>
  150. protected ReportPage GetOverlayPage(ReportPage page)
  151. {
  152. if (page != null)
  153. {
  154. #if Demo
  155. OverlayBand band = new OverlayBand();
  156. band.Parent = page;
  157. band.Bounds = new RectangleF(0, -page.TopMargin * Units.Millimeters, 200, 20);
  158. TextObject text = new TextObject();
  159. text.Parent = band;
  160. text.Bounds = new RectangleF(0, 0, Units.Millimeters * 50, 20);
  161. text.Text = typeof(Double).Name[0].ToString() + typeof(Exception).Name[0].ToString() +
  162. typeof(Math).Name[0].ToString() + typeof(Object).Name[0].ToString() + " " +
  163. typeof(ValueType).Name[0].ToString() + typeof(Exception).Name[0].ToString() +
  164. typeof(Rectangle).Name[0].ToString() + typeof(ShapeKind).Name[0].ToString() +
  165. typeof(ICloneable).Name[0].ToString() + typeof(Object).Name[0].ToString() +
  166. typeof(NonSerializedAttribute).Name[0].ToString();
  167. #endif
  168. #if Academic
  169. OverlayBand band = new OverlayBand();
  170. band.Parent = page;
  171. band.Bounds = new RectangleF(0, -page.TopMargin * Units.Millimeters, 200, 20);
  172. TextObject text = new TextObject();
  173. text.Parent = band;
  174. text.Bounds = new RectangleF(0, 0, Units.Millimeters * 50, 20);
  175. text.Text = typeof(Array).Name[0].ToString() + typeof(Char).Name[0].ToString() +
  176. typeof(Array).Name[0].ToString() + typeof(DateTime).Name[0].ToString() +
  177. typeof(Enum).Name[0].ToString() + typeof(Math).Name[0].ToString() +
  178. typeof(IComparable).Name[0].ToString() + typeof(Char).Name[0].ToString() +
  179. " " +
  180. typeof(LineObject).Name[0].ToString() + typeof(IComparable).Name[0].ToString() +
  181. typeof(Char).Name[0].ToString() + typeof(Enum).Name[0].ToString() +
  182. typeof(Nullable).Name[0].ToString() + typeof(ShapeObject).Name[0].ToString() +
  183. typeof(Enum).Name[0].ToString();
  184. #endif
  185. }
  186. return page;
  187. }
  188. private int GetPagesCount(List<int> pages)
  189. {
  190. #if Demo
  191. return 5;
  192. #else
  193. return pages.Count;
  194. #endif
  195. }
  196. #endregion Private Methods
  197. #if Demo || Academic
  198. internal const bool HAVE_TO_WORK_WITH_OVERLAY = true;
  199. #else
  200. internal const bool HAVE_TO_WORK_WITH_OVERLAY = false;
  201. #endif
  202. }
  203. }