Setting Printsettings from native print form does not set all needed properties

Posted by: charlie.widdicombe on 7 August 2020, 3:28 pm EST

  • Posted 7 August 2020, 3:28 pm EST

    Hello,

    we are trying to implement a custom report dialog in order to bypass an issue we were having with slowness when loading network printers we encountered with the default form. As a part of this we are using the following snippet to open the printer settings page and load the results into the reports printer settings.

     Private Declare Auto Function GlobalLock Lib "kernel32.dll" _
                (ByVal handle As IntPtr) As IntPtr
            Private Declare Auto Function GlobalUnlock Lib "kernel32.dll" _
                (ByVal handle As IntPtr) As Integer
            Private Declare Auto Function GlobalFree Lib "kernel32.dll" _
                (ByVal handle As IntPtr) As IntPtr
            Private Declare Auto Function DocumentProperties Lib "winspool.drv" _
                (ByVal hWnd As IntPtr, ByVal hPrinter As IntPtr, _
                 ByVal pDeviceName As String, ByVal pDevModeOutput As IntPtr, _
                 ByVal pDevModeInput As IntPtr, ByVal fMode As Int32) As Integer
            Private Sub ButtonProperties_Click(sender As Object, e As EventArgs) Handles ButtonProperties.Click
                ' only change PrinterName when it is required, NOT each time the userpresses
                ' the button, setting a printername resets some of the settings
                If (comboBoxPrinters.SelectedIndex <> -1) Then
                    If (ImagePrintDocument.PrinterSettings.PrinterName <> comboBoxPrinters.Text) Then
                        Me.ImagePrintDocument.PrinterSettings.PrinterName = comboBoxPrinters.Text
                    End If
                    If ImagePrintDocument.PrinterSettings.IsValid Then
                        Me.OpenPrinterPropertiesDialog(_ImagePrintDocument.PrinterSettings)
                    Else
                        MsgBox("Invalid printersettings", MsgBoxStyle.Information)
                    End If
                Else
                    MsgBox("Choose a printer", MsgBoxStyle.Information)
                End If
            End Sub
    
            Sub OpenPrinterPropertiesDialog(ByRef Settings As PrinterSettings)
                ' PrinterSettings+PageSettings -> hDEVMODE
                Dim hDevMode As IntPtr = _
                        Settings.GetHdevmode(Settings.DefaultPageSettings)
    
                ' Show Dialog ( [In,Out] pDEVMODE )
                Dim pDevMode As IntPtr = GlobalLock(hDevMode)
                DocumentProperties(Me.Handle, IntPtr.Zero, _
                                   Settings.PrinterName, pDevMode, pDevMode, 14)
                GlobalUnlock(hDevMode)
    
                ' hDEVMODE -> PrinterSettings+PageSettings
                Settings.SetHdevmode(hDevMode)
                Settings.DefaultPageSettings.SetHdevmode(hDevMode)
    
                ' cleanup
                GlobalFree(hDevMode)
    
    _report.Document.Printer.PrinterSettings = Settings.Clone()
    

    However, it seems that by doing this we are not able to correctly set all of the settings to print in the desired way. For example, the _report.Document.PrintOptions.PagesPerSheet appears to get set correctly, but the _report.Document.PrintOptions.PageScaling does not get set to the correct value to print multiple pages per sheet.

    I was wondering if there were any additional/alternate steps that would fix the issue, or failing that if you could provide a list of properties that would need to be set manually.

  • Posted 10 August 2020, 1:15 am EST

    Hello,

    AR use the Advanced printer dialog which is slower than the Standard dialog. Use the following line of code:

    Report.Document.Print(GrapeCity.Viewer.Common.PrintingSettings.UseStandardDialog);

    Thanks,

    Mohit

  • Posted 10 August 2020, 11:10 am EST

    The AR printing dialog is unacceptably slow when there are network to the point where we have had many complaints. However we have had issues with the a known bug with windows print dialog when using the standard dialog (more details can be found in this post: https://social.msdn.microsoft.com/Forums/vstudio/en-US/f39f91e3-a3de-432a-8460-e98027897b51/printersettings-not-saved-by-printdialog?forum=netfxbcl).

    Since neither the advanced dialog or the standard will work for us, we have determined that the solution is to create a custom print dialog.

  • Posted 11 August 2020, 4:56 am EST

    Hello Charlie,

    Actually, “PagesPerSheet” and “PageOrder” property are dependent on the “PageScaling” property not vice-versa. It will be only applicable when “PageScaling” set to “MultiplePages”. You can reorder your property in your custom dialog. Enable the “PagesPerSheet” and “PageOrder” property only when “PageScaling” set to “MultiplePages”.

    Hope it clarifies.

    Thanks,

    Mohit

  • Posted 12 August 2020, 10:45 am EST

    Let me try to jump in here and clarify. We can’t use the default windows print dialog to select a printer due to the known DefaultPrinterSettings bug. We can’t use the AR print dialog to select a printer due to the slowness loading the form when a connected printer is offline. So we have created our own dialog in which the user can select the printer.

    From this custom dialog, we have a “Properties” button, just like on your dialog. Just like your dialog, this “Properties” button opens the printer settings from the print driver, so it is custom to each printer and not controlled by us. The properties window returns a PrinterSettings object that has all of the settings that the user set using the printers properites window. We then set the PrinterSettings on the Report.Document.Printer to the PrinterSetttings returned by the properties dialog:

    _report.Document.Printer.PrinterSettings = Settings.Clone()
    

    This works just fine for most settings but we are finding random settings that are not being passed through to the report from the PrinterSettings object such as PageScaling. These are set in the PrinterSettings, the report just ignores them.

    We are wondering if we are doing something incorrectly with the PrinterSettings and how we are assigning them to the report.document.printer OR if you can give us a complete list of the properties we will need to copy from the PrinterSettings to the report to make them work such as:

    if _report.Document.PrintOptions.PagesPerSheet > 1
                    _report.Document.PrintOptions.PageScaling = PageScaling.MultiplePages
                End If
    

    Thanks for any help you can offer.

  • Posted 12 August 2020, 10:46 am EST

    I should note that the PageScaling gets set correctly from the print driver Properties dialog when using your print dialog, so we must be missing something…

  • Posted 13 August 2020, 8:35 am EST

    Hello,

    Could you please share the stripped-down sample so that I can look into the issue at my end.

    Thanks,

    Mohit

  • Posted 14 August 2020, 3:08 pm EST

    In our report base:

     Using printForm As New Printing.PrintReportForm(Report)
    
                    If printForm.ShowDialog() = DialogResult.OK
    
                        arView.Document.Printer.PrinterSettings = printForm.PrintDialog1.PrinterSettings.Clone()
    
    
                        arView.Print(False)
    
                    End If
    

    In our print form:

            Private Declare Auto Function GlobalLock Lib "kernel32.dll" _
                (ByVal handle As IntPtr) As IntPtr
            Private Declare Auto Function GlobalUnlock Lib "kernel32.dll" _
                (ByVal handle As IntPtr) As Integer
            Private Declare Auto Function GlobalFree Lib "kernel32.dll" _
                (ByVal handle As IntPtr) As IntPtr
            Private Declare Auto Function DocumentProperties Lib "winspool.drv" _
                (ByVal hWnd As IntPtr, ByVal hPrinter As IntPtr, _
                 ByVal pDeviceName As String, ByVal pDevModeOutput As IntPtr, _
                 ByVal pDevModeInput As IntPtr, ByVal fMode As Int32) As Integer
            Private Sub ButtonProperties_Click(sender As Object, e As EventArgs) Handles ButtonProperties.Click
                If (comboBoxPrinters.SelectedIndex <> -1) Then
                    If (ImagePrintDocument.PrinterSettings.PrinterName <> comboBoxPrinters.Text) Then
                        Me.ImagePrintDocument.PrinterSettings.PrinterName = comboBoxPrinters.Text
                    End If
                    If ImagePrintDocument.PrinterSettings.IsValid Then
                        Me.OpenPrinterPropertiesDialog(_ImagePrintDocument.PrinterSettings)
                    Else
                        MsgBox("Invalid printersettings", MsgBoxStyle.Information)
                    End If
                Else
                    MsgBox("Choose a printer", MsgBoxStyle.Information)
                End If
            End Sub
    
            Sub OpenPrinterPropertiesDialog(ByRef Settings As PrinterSettings)
    
                Dim hDevMode As IntPtr = _
                        Settings.GetHdevmode(Settings.DefaultPageSettings)
    
                Dim pDevMode As IntPtr = GlobalLock(hDevMode)
                DocumentProperties(Me.Handle, IntPtr.Zero, _
                                   Settings.PrinterName, pDevMode, pDevMode, 14)
                GlobalUnlock(hDevMode)
    
                Settings.SetHdevmode(hDevMode)
                Settings.DefaultPageSettings.SetHdevmode(hDevMode)
    
                GlobalFree(hDevMode)
    
                PrintDialog1.PrinterSettings = Settings.Clone()
    
            End Sub
    
    

    Copying the printsettings this way does not seem to set all the necessary properties.

  • Posted 17 August 2020, 7:40 am EST

    Hello,

    It is very helpful for us if you share the sample with us. As I see, there is lots of unknown variable in the PrintForm.

    Could you please share a stripped-down same which include your print form and code to print the report.

    If you don’t want to share the sample on public form, you can share the sample privately by opening a new case using the following link:

    https://supportone.componentone.com/newcase

    Note: You can login at the above link using your Forum credentials.

    Thanks,

    Mohit

  • Posted 17 August 2020, 4:18 pm EST

    I have a sample application but it is 67.5 MB and exceeds your size limit. How do I get you the project?

    Thanks!

  • Posted 18 August 2020, 6:27 am EST

    Hi Joe,

    You can upload the application over dropbox or any other file hosting server.

    You can do it here as well: https://www.dropbox.com/request/QKOhQtZZYXA7CCVnbd4H

    Thanks,

    Pragati

  • Posted 20 August 2020, 11:08 am EST

    Thank you, I’ve uploaded our .zip file with a full project in it. There is no report loaded into the viewer so you can’t actually see the issue unless you load a report BUT you can see all of the code and our questions in the comments. If you look at lines 262-266 in the PrintReportForm you will see that we need to set the PageScaling on the report.document.PrintOptions.PageScaling even though we’ve already set the PrinterSettings that have this option set.

    Let us know if you need any more clarification. Thanks!

  • Posted 21 August 2020, 6:16 am EST

    Hello,

    Sorry for the misunderstood your question. PrintOptions and PrinterSetting are different. PrinterSetting is default printer setting whereas PrintOptions is setting related to ActiveReports. So when you clone the PrinterSetting then PrintOption is not copied because PrintOptions is not part of the PrinterSetting. You need to set the PrintOptions explicitly.

    Hope it clarifies.

    Thanks,

    Mohit

Need extra support?

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

Learn More

Forum Channels