OpenPageForm.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using FastReport.Design;
  2. using FastReport.Dialog;
  3. using FastReport.Utils;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Drawing;
  7. using System.Linq;
  8. namespace FastReport.Forms
  9. {
  10. /// <summary>
  11. /// Represents a form for opening and adding pages of another report to the main report.
  12. /// </summary>
  13. public partial class OpenPageForm : BaseDialogForm
  14. {
  15. /// <summary>
  16. /// Designer of the main report.
  17. /// </summary>
  18. public Designer Designer { get; }
  19. private List<PageBase> pages = new List<PageBase>();
  20. private List<int> selectedIndices = new List<int>();
  21. /// <summary>
  22. /// Creates a new instance of the form to open the pages of another report
  23. /// </summary>
  24. /// <param name="designer">Designer of the main report.</param>
  25. /// <param name="report">The report from which pages will be added to the main report</param>
  26. public OpenPageForm(Designer designer, Report report)
  27. {
  28. InitializeComponent();
  29. Localize();
  30. UIUtils.CheckRTL(this);
  31. UpdateDpiDependencies();
  32. Designer = designer;
  33. foreach (PageBase page in report.Pages)
  34. {
  35. pages.Add(page);
  36. }
  37. }
  38. /// <inheritdoc/>
  39. public override void Localize()
  40. {
  41. base.Localize();
  42. Text = Res.Get("Forms,OpenPage");
  43. }
  44. private void btnOk_Click(object sender, EventArgs e)
  45. {
  46. if (lbPages.SelectedItems.Count < 1)
  47. return;
  48. List<string> richNames = new List<string>();
  49. foreach (Base item in Designer.Report.AllObjects)
  50. {
  51. if (item is RichObject)
  52. richNames.Add(item.Name);
  53. }
  54. foreach (string page in lbPages.SelectedItems)
  55. {
  56. Designer.Report.Pages.Add(pages.Find(x => x.Name == page));
  57. }
  58. // select the newly added pages from the list of pages of the main report for renaming (if necessary)
  59. var addedPages = Designer.Report.Pages.Cast<PageBase>().Skip(Designer.Report.Pages.Count - lbPages.SelectedItems.Count);
  60. foreach (var page in addedPages)
  61. {
  62. if (Designer.Report.Pages.Cast<PageBase>().Where(c => c.Name == page.Name).Count() > 1)
  63. page.CreateUniqueName();
  64. foreach (Base item in page.AllObjects)
  65. {
  66. if (item is RichObject)
  67. {
  68. item.Report.Designer = Designer;
  69. string newName;
  70. int i = 1;
  71. do
  72. {
  73. newName = item.BaseName + i.ToString();
  74. i++;
  75. }
  76. while (richNames.Contains(newName));
  77. item.Name = newName;
  78. }
  79. if (Designer.Report.AllObjects.Cast<Base>().Where(c => c.Name == item.Name).Count() > 1)
  80. item.CreateUniqueName();
  81. }
  82. }
  83. Designer.SetModified(this, "AddPage");
  84. Close();
  85. }
  86. private void OpenPageForm_Load(object sender, EventArgs e)
  87. {
  88. foreach (var item in pages)
  89. {
  90. lbPages.Items.Add(item.Name);
  91. }
  92. if (lbPages.Items.Count > 0)
  93. lbPages.SelectedItem = lbPages.Items[0];
  94. }
  95. private string GetLastSelectedPageName()
  96. {
  97. foreach (int index in lbPages.SelectedIndices)
  98. if (!selectedIndices.Contains(index))
  99. selectedIndices.Add(index);
  100. foreach (int index in new List<int>(selectedIndices))
  101. if (!lbPages.SelectedIndices.Contains(index) && selectedIndices.Count > 1)
  102. selectedIndices.Remove(index);
  103. return lbPages.Items[selectedIndices.Last()].ToString();
  104. }
  105. private void lbPages_SelectedIndexChanged(object sender, EventArgs e)
  106. {
  107. float width, height, resolution = 1;
  108. Bitmap bitmap = null;
  109. //string lastPageName = GetLastSelectedPageName();
  110. string lastPageName = lbPages.SelectedItems.Cast<string>().Last().ToString();
  111. if (pages.Find(x => x.Name == lastPageName) is ReportPage reportPage)
  112. {
  113. // the original height and width of the report page
  114. float originalPaperHeight = reportPage.PaperHeight;
  115. float originalPaperWidth = reportPage.PaperWidth;
  116. // sum the height of all brands - this value will become the height of the page
  117. float heightBands = 0;
  118. foreach (var item in reportPage.AllObjects)
  119. {
  120. if (item is BandBase bandItem)
  121. heightBands += bandItem.Height;
  122. }
  123. reportPage.PaperHeight = heightBands / Units.Millimeters + (reportPage.TopMargin + reportPage.BottomMargin);
  124. height = reportPage.PaperHeight * Units.Millimeters;
  125. width = reportPage.PaperWidth * Units.Millimeters;
  126. if (reportPage.UnlimitedWidth)
  127. {
  128. // if the report objects go beyond the bounds of the page width specified in the properties,
  129. // the page width is set to the value of the right edge of the rightmost object
  130. foreach (var item in reportPage.AllObjects)
  131. {
  132. if (item is BandBase bandItem)
  133. {
  134. foreach (var itemBandItem in bandItem.AllObjects)
  135. {
  136. if (itemBandItem is ComponentBase reportObject)
  137. if (reportObject.Right > reportPage.PaperWidth * Units.Millimeters)
  138. reportPage.PaperWidth = reportObject.Right / Units.Millimeters;
  139. }
  140. }
  141. }
  142. reportPage.PaperWidth += (reportPage.LeftMargin + reportPage.RightMargin);
  143. width = reportPage.PaperWidth * Units.Millimeters;
  144. //if the page has the UnlimitedWidth = true,
  145. //the UnlimitedWidthValue is set to the page width value,
  146. //otherwise an exception is raised
  147. reportPage.UnlimitedWidthValue = width;
  148. }
  149. bitmap = new Bitmap((int)(width * resolution), (int)(height * resolution));
  150. List<RichObject> richObjectsWithConvertRichText = new List<RichObject>();
  151. foreach (Base obj in reportPage.AllObjects)
  152. {
  153. if (obj is RichObject)
  154. {
  155. RichObject rich = obj as RichObject;
  156. if (rich.ConvertRichText)
  157. {
  158. rich.ConvertRichText = false;
  159. richObjectsWithConvertRichText.Add(rich);
  160. }
  161. }
  162. }
  163. using (var cache = new GraphicCache())
  164. {
  165. using (var g = Graphics.FromImage(bitmap))
  166. {
  167. reportPage.Draw(new FRPaintEventArgs(g, resolution, resolution, cache));
  168. }
  169. }
  170. //returning the original page height and width of the report
  171. reportPage.PaperHeight = originalPaperHeight;
  172. reportPage.PaperWidth = originalPaperWidth;
  173. foreach (RichObject rich in richObjectsWithConvertRichText)
  174. {
  175. rich.ConvertRichText = true;
  176. }
  177. }
  178. if (pages.Find(x => x.Name == lastPageName) is DialogPage dialogPage)
  179. {
  180. height = dialogPage.Form.Height;
  181. width = dialogPage.Form.Width;
  182. bitmap = DrawUtils.DrawToBitmap(dialogPage.Form, true);
  183. using (var cache = new GraphicCache())
  184. {
  185. using (var g = Graphics.FromImage(bitmap))
  186. {
  187. foreach (DialogControl page in dialogPage.Controls)
  188. {
  189. page.Draw(new FRPaintEventArgs(g, resolution, resolution, cache));
  190. }
  191. }
  192. }
  193. dialogPage.Form.DrawToBitmap(bitmap, new Rectangle(0, 0, (int)width, (int)height));
  194. }
  195. picPreview.Image = bitmap;
  196. }
  197. }
  198. }