Spreadsheet's PrintInfo settings not having any effect on PrintDocument

Posted by: wlin-centerprise-com on 8 September 2017, 1:40 pm EST

  • Posted 8 September 2017, 1:40 pm EST

    Hi there,

    I posted earlier about having problems getting a PrintDocument for the FarPoint spreadsheet working correctly. As per my last post, I was able to get it functioning. However, now I am finding that the PrintInfo settings I apply to each spreadsheet are being ignored. So the print preview being shown doesn't show the correct orientation, margins, etc. Below is the code for my PrintDocument subclass. When the OnPrintPage() method is called for each page e.PageSettings shows a default margin, orientation, etc., completely unrelated to the PrintInfo settings I'm setting for each sheet in the FarPoint spreadsheet (see the PrintSettings property).

    Anyone know why my print settings are being ignored and how I might change that?

    Regards,

    William W. Lin

     

        public class FPointPrintDocument : PrintDocument
        {
            #region Private members
            private FarPoint.Win.Spread.FpSpread  _fpSpreadsheet;
            private int _intTotalPagesToPrint = -1;
            private bool _bCurrentlyPrinting = false;
            private int _intCurrentSheetPagePrinting = -1;
            private int _intCurrentSheetPrinting = -1;
            #endregion Private members

            #region Public properties
            public int NumPages
            {
                get { return _intTotalPagesToPrint; }
            }

            public PrintInfo PrintSettings
            {
                get
                {
                    // Note: assumes a single PrintInfo is used for all sheets
                    return _fpSpreadsheet.Sheets[0].PrintInfo;
                }

                set
                {
                    // Note: assumes a single PrintInfo is used for all sheets
                    // *** Set the print settings for each sheet in the spreadsheet
                    foreach (SheetView sheet in _fpSpreadsheet.Sheets)
                    {
                        sheet.PrintInfo = value;
                    }
                }
            }

            public bool PrintActiveSheetOnly { get; set; }
            #endregion Public properties

            public FPointPrintDocument(FpSpread fpSpreadsheet, PrintInfo printSettings) : base()
            {
                _fpSpreadsheet = fpSpreadsheet;
                PrintSettings = printSettings;
                PrintActiveSheetOnly = true;
            }

            protected override void OnPrintPage(PrintPageEventArgs e)
            {
                base.OnPrintPage(e);

                Rectangle rect = new Rectangle(e.PageBounds.X, e.PageBounds.Y, e.PageBounds.Width, e.PageBounds.Height);

                // *** If we've just begun to print, set up the initial vars
                if (!_bCurrentlyPrinting)
                {
                    if (PrintActiveSheetOnly)
                    {
                        _intTotalPagesToPrint = _fpSpreadsheet.GetOwnerPrintPageCount(e.Graphics, rect,
                                                                                       _fpSpreadsheet.ActiveSheetIndex);
                        _intCurrentSheetPrinting = _fpSpreadsheet.ActiveSheetIndex;
                    }
                    else
                    {
                        _intTotalPagesToPrint = 0;
                        for (int i = 0; i < _fpSpreadsheet.Sheets.Count; i++)
                        {
                            _intTotalPagesToPrint += _fpSpreadsheet.GetOwnerPrintPageCount(e.Graphics, rect, i);
                        }
                        _intCurrentSheetPrinting = 0;   // 0-based
                    }

                    _intCurrentSheetPagePrinting = 1;    // 1-based
                    _bCurrentlyPrinting = true;
                }

                // *** Print the current page and increment the page counter
                _fpSpreadsheet.OwnerPrintDraw(e.Graphics, rect, _intCurrentSheetPrinting, _intCurrentSheetPagePrinting);
                _intCurrentSheetPagePrinting++;

                // *** If we've printed out all the pages for this sheet, go on to the next sheet
                if (_intCurrentSheetPagePrinting > _fpSpreadsheet.GetOwnerPrintPageCount(e.Graphics, rect, _intCurrentSheetPrinting))
                {
                    _intCurrentSheetPrinting++;
                    _intCurrentSheetPagePrinting = 1;
                }

                // *** Signal whether there are any more sheets/pages left to print
                if ( ( !PrintActiveSheetOnly && (_intCurrentSheetPrinting >= _fpSpreadsheet.Sheets.Count))
                    || ( PrintActiveSheetOnly && ( _intCurrentSheetPrinting != _fpSpreadsheet.ActiveSheetIndex )))
                {
                    e.HasMorePages = false;
                    _bCurrentlyPrinting = false;
                }
                else
                {
                    e.HasMorePages = true;
                }
            }   
        }

  • Replied 8 September 2017, 1:40 pm EST

    To distill my question to its essence:

    How can I display a print preview of my spreadsheet, taking into account all the print settings such as margin, orientation, etc.? I can print properly using FpSpread.Print(), but there doesn't appear to be any way to preview what will be printed, unless I'm missing something.

  • Replied 8 September 2017, 1:40 pm EST

    William,


    If you are setting the PrintInfo object into the SheetView correctly, then this should work. Could you post a small zipped project reproducing this issue for us to debug?

  • Replied 8 September 2017, 1:40 pm EST

    Making modifications to the PrintInfo object will not effect the PageSettings you receive as a parameter in your OnPrintPage handler. Since you created the PrintDocument you would need to make the appropriate changes to its properties in order for the PageSettings in the OnPrintPage handler to reflect them. 


     Within Spread when the PrintSheet method is invoked a PrintDocument is created and its properties (margins, landscape, etc) are modified based on all the PrintInfo property settings. Then the Print method is called to begin the printing. When the OnPrintPage handler for our internally created PrintDocument is now called the PageSettings parameter will reflect those changes.


    In your case the PrintInfo settings are never being transfered to the PrintDocument and its sub-objects such as DefaultPageSettings, PrinterSettings ,etc.

  • Replied 8 September 2017, 1:40 pm EST

    As Kevin wrote, I needed to transfer the appropriate PrintInfo settings to the PrintDocument.DefaultPageSettings member. To wit, here is the method I wrote in my FarPoint subclass of PrintDocument:

            private FarPoint.Win.Spread.FpSpread  _fpSpreadsheet;
            private PrintInfo _PrintSettings;

            public void applyPrintSettings()
            {
                // Note: assumes a single PrintInfo is used for all sheets
                // *** Set the print settings for each sheet in the spreadsheet
                foreach (SheetView sheet in _fpSpreadsheet.Sheets)
                {
                    sheet.PrintInfo = _PrintSettings;
                }

                // *** Set the PrintDocument's page settings based on the FarPoint print settings
                if (DefaultPageSettings == null)
                    DefaultPageSettings = new PageSettings();

                DefaultPageSettings.Landscape = (_PrintSettings.Orientation == PrintOrientation.Landscape);
                DefaultPageSettings.Margins.Bottom = _PrintSettings.Margin.Bottom;
                DefaultPageSettings.Margins.Top = _PrintSettings.Margin.Top;
                DefaultPageSettings.Margins.Left = _PrintSettings.Margin.Left;
                DefaultPageSettings.Margins.Right = _PrintSettings.Margin.Right;
            }

     

    I found it a bit strange that I had to manually copy over margin and landscape settings when options like PrintInfo.ShowGrid were applied automatically, but oh well. This one's solved - thanks for the replies!

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels