PrintDialog and PrintDocument DefaultPageSettings

rustyd

Well-known member
Joined
Mar 5, 2003
Messages
112
A user can save their printer settings for 5 different kinds of printers (wide reports, narrow reports, mailing labels, shipping labels, and envelopes).

I store the printer settings in a database. If they havent setup their printers yet and they are changing the printer settings then they get the default settings in the PrintDialog.

If they have previously saved their printer settings, I set a PrintDocuments DefaultPageSettings for the Printer Name, Orientation, Paper Size, Print Resolution, Paper Source, and Duplex according to their saved settings.

They all work great with exception of Paper Size. The wide report uses Legal paper. In order to change the Papersize.Papername property you have to set the kind to custom. No problem, did that. I then set the Papername to Legal.

Here is the code I used:
Code:
        pSz = New PaperSize("Custom Paper size", 0, 0)
        pd.DefaultPageSettings.PaperSize = pSz
        pd.DefaultPageSettings.PaperSize.PaperName = opt(3)
The PrintDocument.DefaultPageSettings.PaperSize.PaperName is now set to "Legal".

So after I get all of the PrintDocument DefaultPageSettings setup, I set the PrintDialog Document = to the PrintDocument that I just setup the DefaultPageSettings.

Code:
      dlg.Document = pd
      Console.WriteLine("DLG Defaultpagesettings: " & dlg.PrinterSettings.DefaultPageSettings.ToString)
      Dim result As DialogResult = dlg.ShowDialog()

These 3 settings are shown in correctly:
Landscape = True
PaperSize = Legal
PaperSource = Tray 2

This is displayed in the Console:
DLG Defaultpagesettings: [PageSettings: Color=False, Landscape=False, Margins=[Margins Left=100 Right=100 Top=100 Bottom=100], PaperSize=[PaperSize Letter Kind=Letter Height=1100 Width=850], PaperSource=[PaperSource Automatically Select Kind=FormSource], PrinterResolution=[PrinterResolution X=600 Y=600]]

But the PrintDialog when displayed shows the correct Landscape and PaperSource (which coincidentally are on the same form) but the PaperSize is still Letter (which you have to click the "Advanced.." button to see).

I just add 2 more Console.WriteLine statements:
Code:
      Console.WriteLine("pd Defaultpagesettings: " & pd.DefaultPageSettings.ToString)
Output:
pd Defaultpagesettings: [PageSettings: Color=False, Landscape=True, Margins=[Margins Left=100 Right=100 Top=100 Bottom=100], PaperSize=[PaperSize Legal Kind=Custom Height=0 Width=0], PaperSource=[PaperSource Tray 2 Kind=Lower], PrinterResolution=[PrinterResolution X=600 Y=600]]

Code:
      Console.WriteLine("pd PrinterSettings Defaultpagesettings: " & pd.PrinterSettings.DefaultPageSettings.ToString)
Output:
pd PrinterSettings Defaultpagesettings: [PageSettings: Color=False, Landscape=False, Margins=[Margins Left=100 Right=100 Top=100 Bottom=100], PaperSize=[PaperSize Letter Kind=Letter Height=1100 Width=850], PaperSource=[PaperSource Automatically Select Kind=FormSource], PrinterResolution=[PrinterResolution X=600 Y=600]]

Code:
      Console.WriteLine("DLG Defaultpagesettings: " & dlg.PrinterSettings.DefaultPageSettings.ToString)
Output:
DLG Defaultpagesettings: [PageSettings: Color=False, Landscape=False, Margins=[Margins Left=100 Right=100 Top=100 Bottom=100], PaperSize=[PaperSize Letter Kind=Letter Height=1100 Width=850], PaperSource=[PaperSource Automatically Select Kind=FormSource], PrinterResolution=[PrinterResolution X=600 Y=600]]

Any suggestions?
 
Ugh! I did this with the papersource, but was trying to set the papersize with the code above.

The solution is:

Code:
Private Sub SetPaperSize(ps as string) 
    Dim pd As PrintDocument = New PrintDocument()

    pd.DefaultPageSettings.PaperSize = pd.PrinterSettings.PaperSizes(getpsz(pd, ps))

End Sub

Private Function getpsz(ByVal ppd As PrintDocument, ByVal strPSz As String) As Integer
     return the papersource
    Dim pSz As PaperSize
    Dim i As Integer

    Try
      i = 0
      For Each pSz In ppd.PrinterSettings.PaperSizes
        If ((String.Compare(strPSz, pSz.PaperName)) = 0) Then Exit For
        i += 1
      Next
      If i > (ppd.PrinterSettings.PaperSizes.Count - 1) Then
        i = 0  papersize not found
      End If
    Catch ex As Exception
      MessageBox.Show("Error #" + Str$(Err.Number) + " has occured." + Err.Description)
    Finally
      getpsz = i
    End Try
End Function
 
Back
Top