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

    <p>Hi there,</p><p>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). </p><p>Anyone know why my print settings are being ignored and how I might change that?</p><p>Regards,</p><p>William W. Lin <br></p><p> </p><p><font face="courier new,courier">    public class FPointPrintDocument : PrintDocument<br>    {<br>        #region Private members<br>        private FarPoint.Win.Spread.FpSpread  _fpSpreadsheet;<br>        private int _intTotalPagesToPrint = -1;<br>        private bool _bCurrentlyPrinting = false;<br>        private int _intCurrentSheetPagePrinting = -1;<br>        private int _intCurrentSheetPrinting = -1;<br>        #endregion Private members<br><br>        #region Public properties<br>        public int NumPages<br>        {<br>            get { return _intTotalPagesToPrint; }<br>        }<br><br>        public PrintInfo PrintSettings<br>        {<br>            get<br>            {<br>                // Note: assumes a single PrintInfo is used for all sheets<br>                return _fpSpreadsheet.Sheets[0].PrintInfo;<br>            }<br><br>            set<br>            {<br>                // Note: assumes a single PrintInfo is used for all sheets<br>                // *** Set the print settings for each sheet in the spreadsheet<br>                foreach (SheetView sheet in _fpSpreadsheet.Sheets)<br>                {<br>                    sheet.PrintInfo = value;<br>                }<br>            }<br>        }<br><br>        public bool PrintActiveSheetOnly { get; set; }<br>        #endregion Public properties<br><br>        public FPointPrintDocument(FpSpread fpSpreadsheet, PrintInfo printSettings) : base()<br>        {<br>            _fpSpreadsheet = fpSpreadsheet;<br>            PrintSettings = printSettings;<br>            PrintActiveSheetOnly = true;<br>        }<br><br>        protected override void OnPrintPage(PrintPageEventArgs e) <br>        {<br>            base.OnPrintPage(e);<br><br>            Rectangle rect = new Rectangle(e.PageBounds.X, e.PageBounds.Y, e.PageBounds.Width, e.PageBounds.Height);<br><br>            // *** If we've just begun to print, set up the initial vars<br>            if (!_bCurrentlyPrinting)<br>            {<br>                if (PrintActiveSheetOnly)<br>                {<br>                    _intTotalPagesToPrint = _fpSpreadsheet.GetOwnerPrintPageCount(e.Graphics, rect,<br>                                                                                   _fpSpreadsheet.ActiveSheetIndex);<br>                    _intCurrentSheetPrinting = _fpSpreadsheet.ActiveSheetIndex; <br>                }<br>                else<br>                {<br>                    _intTotalPagesToPrint = 0;<br>                    for (int i = 0; i < _fpSpreadsheet.Sheets.Count; i++)<br>                    {<br>                        _intTotalPagesToPrint += _fpSpreadsheet.GetOwnerPrintPageCount(e.Graphics, rect, i);<br>                    }<br>                    _intCurrentSheetPrinting = 0;   // 0-based<br>                }<br><br>                _intCurrentSheetPagePrinting = 1;    // 1-based<br>                _bCurrentlyPrinting = true;<br>            }<br><br>            // *** Print the current page and increment the page counter<br>            _fpSpreadsheet.OwnerPrintDraw(e.Graphics, rect, _intCurrentSheetPrinting, _intCurrentSheetPagePrinting);<br>            _intCurrentSheetPagePrinting++;<br><br>            // *** If we've printed out all the pages for this sheet, go on to the next sheet<br>            if (_intCurrentSheetPagePrinting > _fpSpreadsheet.GetOwnerPrintPageCount(e.Graphics, rect, _intCurrentSheetPrinting))<br>            {<br>                _intCurrentSheetPrinting++;<br>                _intCurrentSheetPagePrinting = 1;<br>            }<br><br>            // *** Signal whether there are any more sheets/pages left to print<br>            if ( ( !PrintActiveSheetOnly && (_intCurrentSheetPrinting >= _fpSpreadsheet.Sheets.Count))<br>                || ( PrintActiveSheetOnly && ( _intCurrentSheetPrinting != _fpSpreadsheet.ActiveSheetIndex )))<br>            {<br>                e.HasMorePages = false;<br>                _bCurrentlyPrinting = false;<br>            }<br>            else<br>            {<br>                e.HasMorePages = true;<br>            }<br>        }    <br>    }</font><br></p>
  • Replied 8 September 2017, 1:40 pm EST

    <p>To distill my question to its essence:</p><p>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.<br></p>
  • Replied 8 September 2017, 1:40 pm EST

    <P>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?</P>
  • Replied 8 September 2017, 1:40 pm EST

    <P>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. </P>
    <P> 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.</P>
    <P>In your case the PrintInfo settings are never being transfered to the PrintDocument and its sub-objects such as DefaultPageSettings, PrinterSettings ,etc. </P>
  • Replied 8 September 2017, 1:40 pm EST

    <p>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:<br></p><p><font face="courier new,courier">        private FarPoint.Win.Spread.FpSpread  _fpSpreadsheet;<br>        private PrintInfo _PrintSettings;<br></font></p><p><font face="courier new,courier">        public void applyPrintSettings()<br>        {<br>            // Note: assumes a single PrintInfo is used for all sheets<br>            // *** Set the print settings for each sheet in the spreadsheet<br>            foreach (SheetView sheet in _fpSpreadsheet.Sheets)<br>            {<br>                sheet.PrintInfo = _PrintSettings;<br>            }<br><br>            // *** Set the PrintDocument's page settings based on the FarPoint print settings<br>            if (DefaultPageSettings == null)<br>                DefaultPageSettings = new PageSettings();<br><br>            DefaultPageSettings.Landscape = (_PrintSettings.Orientation == PrintOrientation.Landscape);<br>            DefaultPageSettings.Margins.Bottom = _PrintSettings.Margin.Bottom;<br>            DefaultPageSettings.Margins.Top = _PrintSettings.Margin.Top;<br>            DefaultPageSettings.Margins.Left = _PrintSettings.Margin.Left;<br>            DefaultPageSettings.Margins.Right = _PrintSettings.Margin.Right;<br>        }</font><br> </p><p>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!<br></p>
Need extra support?

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

Learn More

Forum Channels