ReoGrid Forum

Fast and powerful .NET Spreadsheet Component

You are not logged in.

Announcement

This forum has been archived and no longer accepts new user registrations. Please report your questions, problems, and feedback to the issue page of ReoGrid on GitHub. Thank you for your cooperation.

https://github.com/unvell/ReoGrid/issues

#1 2016-12-26 05:52:09

sirmfletcher
Member
Registered: 2016-12-10
Posts: 8

How to Center Printouts Horizontally, Vertically or Both

I've added some code to enable the ability to print out content centered on a sheet
horizontally (i need this) and/or vertically (i don't need this).

in the file print.cs around line 625-626

           RGFloat scaling = this.printSettings == null ? 1f : (this.printSettings.PageScaling);
	    Rectangle paperBounds = this.GetPaperPrintBounds();
            
            // added by M. Fletcher 12/25/16 
            if (PrintSettings.CenterHorizontally || PrintSettings.CenterVertically)
                paperBounds = PrintContentCentered(printContentBounds);  // new function to calculate margins for centering

       
			RGFloat paperWidth = paperBounds.Width / scaling;
			RGFloat paperHeight = paperBounds.Height / scaling;
  
            // added by M. Fletcher 12/25/16 
            if (PrintSettings.CenterHorizontally || PrintSettings.CenterVertically)
                paperBounds = PrintContentCentered(printContentBounds);

new function added in print.cs

   /// <summary>
        /// Center the printout horizontally and/or vertically if user set CenterHorizontally or CenterVertically in printsettings
        /// This will readjust the margins so that printout if its less than the dimension of the page will look centered in the axis specified.
        /// </summary>
        /// <param name="printContentBounds"></param>
        /// <returns></returns>
        internal Rectangle PrintContentCentered(Rectangle printContentBounds)
        {
            RGFloat pWidth;
            RGFloat pHeight;

            
            // get true page dimensions to work with in our calculations
            if (this.PrintSettings.Landscape)
            {
                pWidth = this.PrintSettings.PaperHeight * 100;
                pHeight = this.PrintSettings.PaperWidth * 100;
            }
            else // portrait orientation
            {
                pWidth = this.PrintSettings.PaperWidth * 100;
                pHeight = this.PrintSettings.PaperHeight * 100;
            }

            // see if we need to center Horizontally
            if (this.PrintSettings.CenterHorizontally)
            {
              
                // center our width if possible
                if (printContentBounds.Width < pWidth)
                {
                    float leftmargin = (float)Math.Round(((pWidth - printContentBounds.Width) / 200), 2);

                    // since we can't always get exact same, let right be more or less than left
                    float rightmargin = ((pWidth - printContentBounds.Width) / 100 - leftmargin);

                    PageMargins pm = this.PrintSettings.Margins;

                    if (leftmargin != pm.Left || rightmargin != pm.Right)
                    {

                        pm.Left = leftmargin;
                        pm.Right = rightmargin;
                        // update our printsettings
                        this.PrintSettings.Margins = pm;
                    }
                }
            } // end of if CenterHorizontally

            if (this.PrintSettings.CenterVertically)
            {
                // center our height if possible
                if (printContentBounds.Height < pHeight)
                {
                    float topmargin = (float)Math.Round(((pHeight - printContentBounds.Height) / 200), 2);
                    float bottommargin = ((pHeight - printContentBounds.Height) / 100 - topmargin);

                    PageMargins pm = this.PrintSettings.Margins;
                    if (topmargin != pm.Top || bottommargin != pm.Bottom)
                    {
                        pm.Top = topmargin;
                        pm.Bottom = bottommargin;
                        // update our print settings
                        this.PrintSettings.Margins = pm;
                    }
                }

            } // end of if CenterVertically
            
            return this.GetPaperPrintBounds();

        } // end of PrintContentCentered()

in the PrintSettings.cs  file  2 properties added

	/// <summary>
		/// Get or set the paper margins.
		/// </summary>
		public PageMargins Margins { get; set; }

// around line 93 added 
        //--- Added by M.Fletcher 12/25/16  CenterHorizontally and CenterVertically

        /// <summary>
        /// Get or set whether printout should be centered accross the page horizontally (ignores margin left/right settings)
        /// </summary>
        public bool CenterHorizontally { get; set; }

        /// <summary>
        /// Get or set whether printout should be centered down the page vertically (ignores margin top/bottom settings)
        /// </summary>
        public bool CenterVertically { get; set; }

        //--- end Added by M.Fletcher 12/25/16  CenterHorizontally and CenterVertically

        /// <summary>
        /// Construct print settings instance.
        /// </summary>
        public PrintSettings()
		this.PageScaling = 1f;
			this.PageOrder = PrintPageOrder.DownThenOver;
			//this.PaperSize = PaperSize.Letter;
			this.PaperName = Print.PaperSize.Letter.ToString();
			this.PaperWidth = 8.5f;
			this.PaperHeight = 11f;
			this.Landscape = false;
            this.CenterHorizontally = false;      // added by M. Fletcher default to false
            this.CenterVertically = false;        // added by M. Fletcher   default to false
			this.Margins = new PageMargins(1f);
		}
// and added to Clone function/method
public object Clone()
		{
			return new PrintSettings()
			{
				PrinterName = this.PrinterName,
				PageOrder = this.PageOrder,
				PageScaling = this.PageScaling,
				ShowMargins = this.ShowMargins,
				ShowGridLines = this.ShowGridLines,
				//PaperSize = this.PaperSize,
				PaperName = PaperName,
				PaperWidth = this.PaperWidth,
				PaperHeight = this.PaperHeight,
				Landscape = this.Landscape,
                                CenterHorizontally = this.CenterHorizontally,    // added by M. Fletcher 12/25/16
                                CenterVertically = this.CenterVertically,        //added by M. Fletcher 12/25/16
				Margins = this.Margins,
			};
		}

updated printsettingsdialog.cs in reogrid Editor Demo
reogridupdatedprintdlg.png?ssl=1&w=450
4 new lines of code (3 new controls as pictured above, a groupbox, and 2 checkboxes)

in protected overrid void OnLoad(EventArgs e)
...
// around line #75 after chckPrintGridLines.Checked = ps.ShowGridLines  

            chkCenterHorizontally.Checked = ps.CenterHorizontally; // added by M.Fletcher 12/25/16
            chkCenterVertically.Checked = ps.CenterVertically;   // added by M.Fletcher 12/25/16
...
and
private void btnOK_Click(object sender, EventArgs e)
...
// around line 126 after ps.ShowGridLines = chkPrintGridLines.Checked;
          ps.CenterHorizontally = chkCenterHorizontally.Checked; //added by M.Fletcher 12/25/16
            ps.CenterVertically = chkCenterVertically.Checked; // added by M.Fletcher 12/25/16
	....
 

Results
Horizontally Centered only..
reogridpreviewcenterhorz.png?ssl=1&w=450
Vertically and Horizontally Centered
reogridpreviewcenteredhorzvert.png?ssl=1&w=450

I noticed that some of my test sheets had an extra blank column in the print area by default
causing the print preview to be off-center, until i adjusted the print area.

Now if only I could zoom above 100% like in excel... hummm

Fletch

Offline

#2 2016-12-26 07:10:05

Jingwood
Moderator
From: jing at reogrid.net
Registered: 2014-06-03
Posts: 615

Re: How to Center Printouts Horizontally, Vertically or Both

Hi Fletch, thank you for sharing the nice improvements like this! Do you use GitHub? if so we can merge your code more directly and quickly.

Offline

#3 2016-12-26 16:52:29

sirmfletcher
Member
Registered: 2016-12-10
Posts: 8

Re: How to Center Printouts Horizontally, Vertically or Both

Jing,

Yes I do have github installed on my computer.
I've made additional improvements this morning, to take into account scaling both below and above 100%.

now print content can be enlarged or reduced with scaling factor to fit more of the paper
changes to PrintContentCentered, added new parameter from calling function , the scaling factor

   internal Rectangle PrintContentCentered(Rectangle printContentBounds, RGFloat scaling)
        {
            RGFloat pWidth;
            RGFloat pHeight;
            RGFloat pcWidth;
            RGFloat pcHeight;

            // get true page dimensions to work with in our calculations
            if (this.PrintSettings.Landscape)
            {
                pWidth = this.PrintSettings.PaperHeight * 100;
                pHeight = this.PrintSettings.PaperWidth * 100;
            }
            else // portrait orientation
            {
                pWidth = this.PrintSettings.PaperWidth * 100;
                pHeight = this.PrintSettings.PaperHeight * 100;
            }

            // get width of content scaled at whatever factor we chose
            pcWidth = (int)Math.Round(printContentBounds.Width * scaling);
            
            // get height of content scaled at whatever factor we chose
            pcHeight = (int)Math.Round(printContentBounds.Height * scaling);
          

            // see if we need to center Horizontally
            if (this.PrintSettings.CenterHorizontally)
            {
              
                // center our width if possible
                if (printContentBounds.Width < pWidth)
                {
                    float leftmargin = (float)Math.Round(((pWidth - pcWidth) / 200), 2); //printContentBounds.Width

                    // since we can't always get exact same, let right be more or less than left
                    float rightmargin = ((pWidth - pcWidth) / 100 - leftmargin); //printContentBounds.Width

                    PageMargins pm = this.PrintSettings.Margins;

                    if (leftmargin != pm.Left || rightmargin != pm.Right)
                    {

                        pm.Left = leftmargin;
                        pm.Right = rightmargin;
                        // update our printsettings
                        this.PrintSettings.Margins = pm;
                    }
                }
            } // end of if CenterHorizontally

            if (this.PrintSettings.CenterVertically)
            {
                // center our height if possible
                if (printContentBounds.Height < pHeight)
                {
                    float topmargin = (float)Math.Round(((pHeight -pcHeight) / 200), 2);

                    // since we can't always get exact same, let bottom be more or less than top
                    float bottommargin = ((pHeight - pcHeight) / 100 - topmargin);

                    PageMargins pm = this.PrintSettings.Margins;
                    if (topmargin != pm.Top || bottommargin != pm.Bottom)
                    {
                        pm.Top = topmargin;
                        pm.Bottom = bottommargin;
                        // update our print settings
                        this.PrintSettings.Margins = pm;
                    }
                }

            } // end of if CenterVertically
            
            return this.GetPaperPrintBounds();

        } // end of PrintContentCentered()

and in the calling function

  // added by M. Fletcher 12/25/16 
            if (PrintSettings.CenterHorizontally || PrintSettings.CenterVertically)
                paperBounds = PrintContentCentered(printContentBounds, scaling);
       

screenshots of the results
Page Centered Horizontally and Vertically at 75% Scale
reogridpreviewcenteredscaled75.png?ssl=1&w=450

Page Centered Horizontally with Top Margin 1" at 75% Scale
reogridpreviewcenteredhorizscaled75.png?ssl=1&w=450

Page Centered Horizontally and Vertically at 155% Scale
reogridpreviewcenteredscaled155.png?ssl=1&w=450

Page Centered Horizontally with 0.5" Top Margin at 155% Scale
reogridpreviewcenteredhorzscaled155tophalfinchmargin.png?ssl=1&w=450

I also adjusted the limits on the NumericUpDown control on the printsettingsdialog.cs to allow values upto 500(%) for testing the scaling up and down.

The only other issue i've noticed, is adjusting the print bounding rect causes scaling factor to revert back to 100% instead of staying above 100%

Getting closer to what I need in my application which will print and display reports using your control!
Thanks,

Fletch

Offline

Board footer

Powered by FluxBB