123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- using FastReport.Design;
- using FastReport.Dialog;
- using FastReport.Utils;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- namespace FastReport.Forms
- {
- /// <summary>
- /// Represents a form for opening and adding pages of another report to the main report.
- /// </summary>
- public partial class OpenPageForm : BaseDialogForm
- {
- /// <summary>
- /// Designer of the main report.
- /// </summary>
- public Designer Designer { get; }
- private List<PageBase> pages = new List<PageBase>();
- private List<int> selectedIndices = new List<int>();
- /// <summary>
- /// Creates a new instance of the form to open the pages of another report
- /// </summary>
- /// <param name="designer">Designer of the main report.</param>
- /// <param name="report">The report from which pages will be added to the main report</param>
- public OpenPageForm(Designer designer, Report report)
- {
- InitializeComponent();
- Localize();
- UIUtils.CheckRTL(this);
- UpdateDpiDependencies();
- Designer = designer;
- foreach (PageBase page in report.Pages)
- {
- pages.Add(page);
- }
- }
- /// <inheritdoc/>
- public override void Localize()
- {
- base.Localize();
- Text = Res.Get("Forms,OpenPage");
- }
- private void btnOk_Click(object sender, EventArgs e)
- {
- if (lbPages.SelectedItems.Count < 1)
- return;
- List<string> richNames = new List<string>();
- foreach (Base item in Designer.Report.AllObjects)
- {
- if (item is RichObject)
- richNames.Add(item.Name);
- }
- foreach (string page in lbPages.SelectedItems)
- {
- Designer.Report.Pages.Add(pages.Find(x => x.Name == page));
- }
- // select the newly added pages from the list of pages of the main report for renaming (if necessary)
- var addedPages = Designer.Report.Pages.Cast<PageBase>().Skip(Designer.Report.Pages.Count - lbPages.SelectedItems.Count);
- foreach (var page in addedPages)
- {
- if (Designer.Report.Pages.Cast<PageBase>().Where(c => c.Name == page.Name).Count() > 1)
- page.CreateUniqueName();
- foreach (Base item in page.AllObjects)
- {
- if (item is RichObject)
- {
- item.Report.Designer = Designer;
- string newName;
- int i = 1;
- do
- {
- newName = item.BaseName + i.ToString();
- i++;
- }
- while (richNames.Contains(newName));
- item.Name = newName;
- }
- if (Designer.Report.AllObjects.Cast<Base>().Where(c => c.Name == item.Name).Count() > 1)
- item.CreateUniqueName();
- }
- }
- Designer.SetModified(this, "AddPage");
- Close();
- }
- private void OpenPageForm_Load(object sender, EventArgs e)
- {
- foreach (var item in pages)
- {
- lbPages.Items.Add(item.Name);
- }
- if (lbPages.Items.Count > 0)
- lbPages.SelectedItem = lbPages.Items[0];
- }
- private string GetLastSelectedPageName()
- {
- foreach (int index in lbPages.SelectedIndices)
- if (!selectedIndices.Contains(index))
- selectedIndices.Add(index);
- foreach (int index in new List<int>(selectedIndices))
- if (!lbPages.SelectedIndices.Contains(index) && selectedIndices.Count > 1)
- selectedIndices.Remove(index);
- return lbPages.Items[selectedIndices.Last()].ToString();
- }
- private void lbPages_SelectedIndexChanged(object sender, EventArgs e)
- {
- float width, height, resolution = 1;
- Bitmap bitmap = null;
- //string lastPageName = GetLastSelectedPageName();
- string lastPageName = lbPages.SelectedItems.Cast<string>().Last().ToString();
- if (pages.Find(x => x.Name == lastPageName) is ReportPage reportPage)
- {
- // the original height and width of the report page
- float originalPaperHeight = reportPage.PaperHeight;
- float originalPaperWidth = reportPage.PaperWidth;
- // sum the height of all brands - this value will become the height of the page
- float heightBands = 0;
- foreach (var item in reportPage.AllObjects)
- {
- if (item is BandBase bandItem)
- heightBands += bandItem.Height;
- }
- reportPage.PaperHeight = heightBands / Units.Millimeters + (reportPage.TopMargin + reportPage.BottomMargin);
- height = reportPage.PaperHeight * Units.Millimeters;
- width = reportPage.PaperWidth * Units.Millimeters;
- if (reportPage.UnlimitedWidth)
- {
- // if the report objects go beyond the bounds of the page width specified in the properties,
- // the page width is set to the value of the right edge of the rightmost object
- foreach (var item in reportPage.AllObjects)
- {
- if (item is BandBase bandItem)
- {
- foreach (var itemBandItem in bandItem.AllObjects)
- {
- if (itemBandItem is ComponentBase reportObject)
- if (reportObject.Right > reportPage.PaperWidth * Units.Millimeters)
- reportPage.PaperWidth = reportObject.Right / Units.Millimeters;
- }
- }
- }
- reportPage.PaperWidth += (reportPage.LeftMargin + reportPage.RightMargin);
- width = reportPage.PaperWidth * Units.Millimeters;
- //if the page has the UnlimitedWidth = true,
- //the UnlimitedWidthValue is set to the page width value,
- //otherwise an exception is raised
- reportPage.UnlimitedWidthValue = width;
- }
- bitmap = new Bitmap((int)(width * resolution), (int)(height * resolution));
- List<RichObject> richObjectsWithConvertRichText = new List<RichObject>();
- foreach (Base obj in reportPage.AllObjects)
- {
- if (obj is RichObject)
- {
- RichObject rich = obj as RichObject;
- if (rich.ConvertRichText)
- {
- rich.ConvertRichText = false;
- richObjectsWithConvertRichText.Add(rich);
- }
- }
- }
- using (var cache = new GraphicCache())
- {
- using (var g = Graphics.FromImage(bitmap))
- {
- reportPage.Draw(new FRPaintEventArgs(g, resolution, resolution, cache));
- }
- }
- //returning the original page height and width of the report
- reportPage.PaperHeight = originalPaperHeight;
- reportPage.PaperWidth = originalPaperWidth;
- foreach (RichObject rich in richObjectsWithConvertRichText)
- {
- rich.ConvertRichText = true;
- }
- }
- if (pages.Find(x => x.Name == lastPageName) is DialogPage dialogPage)
- {
- height = dialogPage.Form.Height;
- width = dialogPage.Form.Width;
- bitmap = DrawUtils.DrawToBitmap(dialogPage.Form, true);
- using (var cache = new GraphicCache())
- {
- using (var g = Graphics.FromImage(bitmap))
- {
- foreach (DialogControl page in dialogPage.Controls)
- {
- page.Draw(new FRPaintEventArgs(g, resolution, resolution, cache));
- }
- }
- }
- dialogPage.Form.DrawToBitmap(bitmap, new Rectangle(0, 0, (int)width, (int)height));
- }
- picPreview.Image = bitmap;
- }
- }
- }
|