Changing Printer Driver Setting Before Printing in VB.NET

 

The Following VB.NET code demonstrates how to change the printer driver setting before printing for File Format, Output directory, and to load a predefined profile.

 

To be able to change the Printer Driver settings, the BlackIceDEVMODE ActiveX Control reference should be added to the VB.NET project in Visual Studio.

 

For detailed description about how to add Active X control to your VB.NET project, please refer to the Registering the ActiveX Controls section.

 

For 32 bit:

 

' This function changes a few printer settings in the specified printer driver

Public Sub ChangeSettings(ByVal szPrinterName As String)

     

      ' Creating BlackIceDEVMODE object

      Dim BiDevmode As BLACKICEDEVMODELib.BlackIceDEVMODE

      BiDevmode = New BLACKICEDEVMODELib.BlackIceDEVMODE()

      ' Create a new BlackIceDEVMODE structure and load current printing      settings

      Dim pDevMode As Integer = BiDevmode.LoadBlackIceDEVMODE(szPrinterName)

 

      ' Set the output directory

      BiDevmode.SetOutputDirectory("<<USERPROFILE>>\Documents", pDevMode)

 

      ' Set output file format to "PDF"

      BiDevmode.SetFileFormat(36, pDevMode)

 

      ' Apply the new settings and release the BlackICeDEVMODE structure

      BiDevmode.SaveBlackIceDEVMODE(szPrinterName, pDevMode)

      BiDevmode.ReleaseBlackIceDEVMODE(pDevMode)

 

End Sub

' This function loads a profile into the specified printer

Public Sub LoadProfile(ByVal szPrinterName As String, ByVal szProfileName As String)

     

      'Creating BlackIceDEVMODE object

      Dim BiDevmode As BLACKICEDEVMODELib.BlackIceDEVMODE

      BiDevmode = New BLACKICEDEVMODELib.BlackIceDEVMODE()

      ' Create a new BlackIceDEVMODE structure and load current printing      settings

      Dim pDevMode As Integer = BiDevmode.LoadBlackIceDEVMODE(szPrinterName)

 

      ' Load a printer driver profile.

      ' Settings that are stored in the registry get applied immediately,

      ' settings that are stored in the BlackIceDEVMODE structure get loaded to       pDevMode

      ' and get applied when we call SaveBlackIceDEVMODE

      BiDevmode.ApplyProfile(szPrinterName, szProfileName, pDevMode)

 

      ' Apply the new settings and release the BlackICeDEVMODE structure

      BiDevmode.SaveBlackIceDEVMODE(szPrinterName, pDevMode)

      BiDevmode.ReleaseBlackIceDEVMODE(pDevMode)

End Sub

' Using the functions:

' Change some of the printer settings.

ChangeSettings("Black Ice ColorPlus")

 

' Or load one of the profiles.

' To load a printer profile, the sample must be compiled to the same

' platform as the printer driver, so for 64 bit systems, set the platform to x64

LoadProfile("Black Ice ColorPlus", "Display BMP")

 

For 64 bit:

 

' This function changes a few printer settings in the specified printer driver

Public Sub ChangeSettings(ByVal szPrinterName As String)

     

      ' Creating BlackIceDEVMODE object

      Dim BiDevmode As BLACKICEDEVMODELib.BlackIceDEVMODE

      BiDevmode = New BLACKICEDEVMODELib.BlackIceDEVMODE()

      ' Create a new BlackIceDEVMODE structure and load current printing      settings

      Dim pDevMode As Long = BiDevmode.LoadBlackIceDEVMODE(szPrinterName)

 

      ' Set the output directory

      BiDevmode.SetOutputDirectory("<<USERPROFILE>>\Documents", pDevMode)

 

      ' Set output file format to "PDF"

      BiDevmode.SetFileFormat(36, pDevMode)

 

      ' Apply the new settings and release the BlackICeDEVMODE structure

      BiDevmode.SaveBlackIceDEVMODE(szPrinterName, pDevMode)

      BiDevmode.ReleaseBlackIceDEVMODE(pDevMode)

 

End Sub

' This function loads a profile into the specified printer

Public Sub LoadProfile(ByVal szPrinterName As String, ByVal szProfileName As String)

     

      ' Creating BlackIceDEVMODE object

      Dim BiDevmode As BLACKICEDEVMODELib.BlackIceDEVMODE

      BiDevmode = New BLACKICEDEVMODELib.BlackIceDEVMODE() 

      ' Create a new BlackIceDEVMODE structure and load current printing      settings

      Dim pDevMode As Long = BiDevmode.LoadBlackIceDEVMODE(szPrinterName)

 

      ' Load a printer driver profile.

      ' Settings that are stored in the registry get applied immediately,

      ' settings that are stored in the BlackIceDEVMODE structure get loaded to       pDevMode

      ' and get applied when we call SaveBlackIceDEVMODE

      BiDevmode.ApplyProfile(szPrinterName, szProfileName, pDevMode)

 

      ' Apply the new settings and release the BlackICeDEVMODE structure

      BiDevmode.SaveBlackIceDEVMODE(szPrinterName, pDevMode)

      BiDevmode.ReleaseBlackIceDEVMODE(pDevMode)

End Sub

' Using the functions:

' Change some of the printer settings.

ChangeSettings("Black Ice ColorPlus")

 

' Or load one of the profiles.

' To load a printer profile, the sample must be compiled to the same

' platform as the printer driver, so for 64 bit systems, set the platform to x64

LoadProfile("Black Ice ColorPlus", "Display BMP")