none
Getting accurate PrinterSettings PaperSource RRS feed

  • Question

  • I am trying to allow users to select default print settings for a certain action and persisting those settings within a System.Drawing.Printing.PrinterSettings object.  The most important option to persist is the selection of a Paper Type from the printer properties.  I know that it's stored somewhere in the PrinterSettings object because when I assign the object to PrintDialog and show it, the selection persists.  For the life of me I can't find where that is.  I thought it would be the PrinterSettings.DefaultPageSettings.PaperSource but that doesn't appear to change from default (at least as I can see it in the Watch window).

    For testing purposes, here's what I'm doing:

            Dim ps As New PrinterSettings
    
            Stop
    
            Dim pd As New PrintDialog
            pd.PrinterSettings = ps
            pd.ShowDialog()
    
            Stop
    
            pd.ShowDialog()
    
            Stop

    So I select a Paper Type when the pd PrintDialog is shown and then examine the ps object.  Nothing related to the PaperSource or anything like that appears to be changed.  When I continue debugging and show the PrintDialog again, the selection has persisted.  Any thoughts?

    Wednesday, September 11, 2013 2:20 PM

Answers

  • Then when you select a PaperSize in the PrintDialog, the PaperSource should change.  If it doesn't, check the PrintDialog.Document's settings after the PrintDialog is closed.

    • Marked as answer by mcorrente Wednesday, September 11, 2013 7:45 PM
    Wednesday, September 11, 2013 7:13 PM

All replies

  • PaperSource is a tray that holds paper.  Are you sure you aren't changing the PaperSize?
    Wednesday, September 11, 2013 3:35 PM
  • No, trying to change the PaperSource.

    You can specify that a tray contains a certain paper type (Letterhead, Preprinted, etc).  When you select that it automatically selects the correct tray.  I know that information is persisted in the PrinterSettings (as I demonstrated) but I can't figure out where.

    Wednesday, September 11, 2013 6:57 PM
  • Then when you select a PaperSize in the PrintDialog, the PaperSource should change.  If it doesn't, check the PrintDialog.Document's settings after the PrintDialog is closed.

    • Marked as answer by mcorrente Wednesday, September 11, 2013 7:45 PM
    Wednesday, September 11, 2013 7:13 PM
  • Ok, weird.  I gave what you said a shot (I'm selecting PaperType, not PaperSize, but the result should be essentially the same).

    Because I'm only using this dialog to persist default settings I hadn't attached an actual PrintDocument to the dialog.  I tried that now.  I had two problems with my code.  First, it looks like you have to have a PrintDocument associated with the dialog to get it to act right - I'm not sure why.  Second, it appears assigning a PrinterSettings object to the PrintDialog as I did above with pd.PrinterSettings = ps creates a clone(?).  At any rate, the settings under pd.PrinterSettings do not match ps after the dialog is closed.

    And third, despite apparently needing a PrintDocument related to the dialog to get this to work right, the DefaultPageSettings of the document are actually not affected - the PaperSource stays at the default there.  Luckily, the DefaultPageSettings of the PrintDialog's PrinterSettings IS updated, so I can get the value from there.  

    I'm new to PrintDocuments and all of this, so maybe this makes sense to someone else.  To me, it does not.  At any rate, your suggestion got me there John so thank you.

    Wednesday, September 11, 2013 7:45 PM
  • "I'm selecting PaperType, not PaperSize, but the result should be essentially the same."

    I imagine that "PaperType" is something that shows in your PrintDialog.  I'm assuming that it relates to the PaperKind property of the PaperSize class.

    All of the Windows.Forms printing dialogs should have a PrintDocument assigned to their Document property before they are shown.  When they are closed, they transfer their settings to the PrintDocument.  To ensure that the correct settings are transferred, the values should be checked in the PrintPage event.

    Wednesday, September 11, 2013 8:13 PM
  • Good to know, thanks.

    Perhaps you can answer another quick related question since you apparently are quite familiar with this stuff.  If you think I should start another thread I'd be happy to, but here it is.

    Now that I've worked out how to save PaperSource and other settings, I'm trying to apply this when printing.  I'm handling the QueryPageSettings event on a PrintDocument and was trying to do the following (where _MySavedPageSettings is a PageSettings object containing the settings I want to use):

        Private Sub QueryPageSettings(sender As Object, e As QueryPageSettingsEventArgs)
    
                e.PageSettings = _MySavedPageSettings
    
        End Sub

    Although e.PageSettings appears to mirror _MySavedPageSettings after this assignment (and in the PrintPage event as well), the settings don't appear to have any effect.  However, if I change it to this:

        Private Sub QueryPageSettings(sender As Object, e As QueryPageSettingsEventArgs)
    
            e.PageSettings.PaperSource = _MySavedPageSettings.PaperSource
    
        End Sub

    then that particular settings (PaperSource) takes effect just fine.  Ideas?  I'd like to have all the settings take effect without having to walk through the objects assigning setting by setting.

    (edit: When I say that the settings don't appear to have any effect, I mean that the document prints just fine but uses the default settings and not the settings contained in _MySavedPageSettings).

    • Edited by mcorrente Wednesday, September 11, 2013 8:41 PM Clarify problem
    Wednesday, September 11, 2013 8:38 PM
  • This has nothing to do with printing.  When you pass an object ByVal you pass a reference to the object.  You can change fields of the object, but you can't change the object. 
    Wednesday, September 11, 2013 9:01 PM
  • Why not?  Documentation seems to indicate you can replace the existing PageSettings object and doesn't appear to be read only.  See http://msdn.microsoft.com/en-us/library/system.drawing.printing.querypagesettingseventargs.pagesettings.aspx

    "You set page settings by modifying individual properties of the PageSettings property or by setting the property to a PageSettings."

    And after setting the property to _MySavedPageSettings it seems to stick - both in the QueryPageSettings event and when examined later in the PrintPage event.  Just doesn't seem to do anything.  It's weird.

    (Sorry about the link not being a hyperlink - for some reason it says my account needs to be verified)

    Thursday, September 12, 2013 1:09 PM
  • If you start a new thread I'm sure you'll find someone who will be able to explain it better than I have.
    Thursday, September 12, 2013 2:14 PM
  • Thanks.
    Thursday, September 12, 2013 2:31 PM