using FastReport.Design; using FastReport.Dialog; using FastReport.Utils; using System; using System.Collections.Generic; using System.Drawing; using System.Linq; namespace FastReport.Forms { /// /// Represents a form for opening and adding pages of another report to the main report. /// public partial class OpenPageForm : BaseDialogForm { /// /// Designer of the main report. /// public Designer Designer { get; } private List pages = new List(); private List selectedIndices = new List(); /// /// Creates a new instance of the form to open the pages of another report /// /// Designer of the main report. /// The report from which pages will be added to the main report public OpenPageForm(Designer designer, Report report) { InitializeComponent(); Localize(); UIUtils.CheckRTL(this); UpdateDpiDependencies(); Designer = designer; foreach (PageBase page in report.Pages) { pages.Add(page); } } /// 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 richNames = new List(); 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().Skip(Designer.Report.Pages.Count - lbPages.SelectedItems.Count); foreach (var page in addedPages) { if (Designer.Report.Pages.Cast().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().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(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().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 richObjectsWithConvertRichText = new List(); 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; } } }