This is a migrated thread and some comments may be shown as answers.

Printable panel doesn't resize when it's larger then pagesize

3 Answers 78 Views
Panel
This is a migrated thread and some comments may be shown as answers.
n/a
Top achievements
Rank 1
n/a asked on 05 Apr 2018, 02:10 PM
I have created next custom panel.

 

public class PrintablePanel : RadPanel, IPrintable
    {
        public PrintablePanel()
        {
        }
        public int BeginPrint(RadPrintDocument sender, PrintEventArgs args)
        {
            return 1;
        }
        public bool EndPrint(RadPrintDocument sender, PrintEventArgs args)
        {
            return true;
        }
        public Form GetSettingsDialog(RadPrintDocument document)
        {
            return new PrintSettingsDialog(document);
        }
        public bool PrintPage(int pageNumber, RadPrintDocument sender, PrintPageEventArgs args)
        {
            Bitmap bmp = new Bitmap(this.Width, this.Height);
            bool printLandscape = Width > Height;
            Size printSize = this.Size;
            if (Size.Width > Convert.ToInt32(args.PageSettings.PrintableArea.Width) ||
                Size.Height > Convert.ToInt32(args.PageSettings.PrintableArea.Height))
            {
                printSize = new Size(Convert.ToInt32(args.PageSettings.PrintableArea.Width),     Convert.ToInt32(args.PageSettings.PrintableArea.Height));
                bmp = Imageworker.ResizeImage(bmp, printSize.Width, printSize.Height);
            }
            args.PageSettings.Landscape = printLandscape;
            this.DrawToBitmap(bmp, new Rectangle(new Point(0, 0), printSize));
            args.Graphics.DrawImage(bmp, Point.Empty);
            sender.
            return false;
        }
        public void Print()
        {
            RadPrintDocument doc = this.CreatePrintDocument();
            doc.Print();
        }
        public void PrintPreview()
        {
            RadPrintDocument doc = this.CreatePrintDocument();
            RadPrintPreviewDialog dialog = new RadPrintPreviewDialog(doc);
            dialog.ThemeName = this.ThemeName;
            dialog.ShowDialog();
        }
        private RadPrintDocument CreatePrintDocument()
        {
            RadPrintDocument doc = new RadPrintDocument();
            doc.AssociatedObject = this;
            return doc;
        }
    }

 

The problem I have is that when I want to print a panel that's larger then the papersize, it won't resize.

Setting the page to landscape also doesn't work.

See my code in the following method:

public bool PrintPage(int pageNumber, RadPrintDocument sender, PrintPageEventArgs args)

3 Answers, 1 is accepted

Sort by
0
n/a
Top achievements
Rank 1
answered on 05 Apr 2018, 02:15 PM

Ignore the following line in public bool PrintPage(int pageNumber, RadPrintDocument sender, PrintPageEventArgs args)

'sender.'

0
n/a
Top achievements
Rank 1
answered on 06 Apr 2018, 09:12 AM

I have found a solution.

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Windows.Forms;
using Telerik.WinControls.UI;
 
namespace AudiQC.Objects
{
    public class PrintablePanel : RadPanel, IPrintable
    {
        public int BeginPrint(RadPrintDocument sender, PrintEventArgs args)
        {
            return 1;
        }
 
        public bool EndPrint(RadPrintDocument sender, PrintEventArgs args)
        {
            return true;
        }
 
        public Form GetSettingsDialog(RadPrintDocument document)
        {
            return new PrintSettingsDialog(document);
        }
 
        public bool PrintPage(int pageNumber, RadPrintDocument sender, PrintPageEventArgs args)
        {
            float scale = 1.0f;
 
            if (!args.PageSettings.Landscape)
            {
                scale = Math.Min((float)(args.PageSettings.PrintableArea.Width - (args.PageSettings.HardMarginX * 3)) / this.Size.Width, (float)(args.PageSettings.PrintableArea.Height - (args.PageSettings.HardMarginY * 3)) / this.Size.Height);
            }
            else
            {
                scale = Math.Min((float)(args.PageSettings.PrintableArea.Height - (args.PageSettings.HardMarginX * 3)) / this.Size.Width, (float)(args.PageSettings.PrintableArea.Width - (args.PageSettings.HardMarginY * 3)) / this.Size.Height);
            }
 
            Bitmap panelBmp = new Bitmap(this.Width, this.Height);
            this.DrawToBitmap(panelBmp, this.Bounds);
 
            Matrix saveMatrix = args.Graphics.Transform;
            args.Graphics.ScaleTransform(scale, scale);
            args.Graphics.DrawImage(panelBmp, new PointF(0, 0));
            args.Graphics.Transform = saveMatrix;
 
            return false;
        }
 
        public void PrintPreview()
        {
            RadPrintDocument doc = this.CreatePrintDocument();
            RadPrintPreviewDialog dialog = new RadPrintPreviewDialog(doc);
 
            dialog.ThemeName = this.ThemeName;
            dialog.ShowDialog();
        }
 
        private RadPrintDocument CreatePrintDocument()
        {
            RadPrintDocument doc = new RadPrintDocument();
            doc.AssociatedObject = this;
            doc.Landscape = this.Width > this.Height;
            return doc;
        }
    }
}
0
Hristo
Telerik team
answered on 09 Apr 2018, 02:00 PM
Hello Sven,

Thank you for writing.

Indeed the panel will not size automatically according to the paper and you will need to apply an additional scaling. The solution you shared appears to be valid. Thank you also for sharing it with the community. 

In case you would need further assistance, please let me know.

Regards,
Hristo
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Panel
Asked by
n/a
Top achievements
Rank 1
Answers by
n/a
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or