Click here to Skip to main content
15,885,782 members
Articles / Programming Languages / C#

PrintManager – Setting Advanced Printer Options

Rate me:
Please Sign up or sign in to vote.
4.83/5 (3 votes)
30 Apr 2015CPOL2 min read 14.3K   16   1
How to set advanced printer options using PrintManager

The PrintManager for .NET API allows printing of PDF files through any .NET application. The settings accessible to the PrintManager are limited to those exposed through a standard API (such as Win32 or .NET). But a printer may have additional settings that are specific to that printer and are not readily available through a standard API. This means that there could be non-standard features or settings available that vary between printers that are not able to be exposed through the PrintManager API. To remedy this, we have exposed a way to manually save and reuse a specific state of the native printer’s dialog box that when provided to the Printjob will affect that job as if those options where manually selected by a user. A full example on how to accomplish this is outlined below.

We will start with the code to invoke printer’s native dialog box and save the settings to a file.

To invoke the printer's advanced setting dialog, we will be using a Win32 function called "AdvancedDocumentProperties".

C#
AdvancedDocumentProperties(IntPtr.Zero,IntPtr.Zero,e.PrintJob.Printer.Name,e.DevMode, e.DevMode);

In order to import the above function to a .NET application, use the following DllImport.

C#
[DllImport("winspool.Drv", EntryPoint = "AdvancedDocumentPropertiesW", 
SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]

static extern int AdvancedDocumentProperties(IntPtr hwnd, IntPtr hPrinter, 
[MarshalAs(UnmanagedType.LPWStr)] string pDeviceName, IntPtr pDevModeOutput, IntPtr pDevModeInput);

Calling the Print() method will fire the Printjob.Starting event. In the event handler, call AdvancedDocumentProperties to display the printer setting dialog box. Select the appropriate setting that you would like to save to file and click OK. The file can be generated with individual settings or with a group of settings.

C#
PrintJob printJob = new PrintJob(Printer.Default, "DocumentA.pdf");

printJob.Starting += new PrintJobStartingEventHandler(this.onStartingPrint1);

printJob.Print();

PrintJobStartingEventArgs contains the following properties that will be used to retrieve the printer settings from the dialog box.

Name Type Definition
Devmode Intptr Pointer towards Devmode structure
DevmodeSize Int Size of  Devmode
DevmodeDriverExtra Int Size of  DmDriverExtra
PrintJob PrintJob DynamicPDF Printmanager PrintJob object. Generic printer settings can be changed using this object.

Here is the code for the event handler to retrieve and save the selected printer setting.

C#
private void onStartingPrint1(object sender, PrintJobStartingEventArgs e)
{
    //Invoke the printer options dialog box.
    int value = AdvancedDocumentProperties(IntPtr.Zero, IntPtr.Zero, e.PrintJob.Printer.Name, e.DevMode, e.DevMode);

    //Create a byte array to store Devmode and DmDriverExtra settings.
    byte[] bytearray = new byte[e.DevModeSize + e.DevModeDriverExtra];

    //Copy the contents of both Devmode and DmDriverExtra to the byte array.
    Marshal.Copy(e.DevMode, bytearray, 0, e.DevModeSize + e.DevModeDriverExtra);

    //Save the settings to disk.
    File.WriteAllBytes("SettingsFile.bin", bytearray); 
}

Next, we will print a PDF using the saved settings by overwriting the Devmode and DmDriverExtra structures with the file contents. Due to this, all settings applied to the PrintJob object before Printjob.Starting event handler call will be lost. However, we can set the print settings after overwriting Devmode and DmDriverExtra in the event handler.

C#
private void onStartingPrint2(object sender, PrintJobStartingEventArgs e)
{
    //Load the saved settings into a byte array. 
    byte[] bytearray = File.ReadAllBytes("SettingsFile.bin");

    //Overwrite the DevMode with the byte array restored from file. 
    IntPtr pdevmode = Marshal.AllocHGlobal(bytearray.Length);
    Marshal.Copy(bytearray, 0, e.DevMode, bytearray.Length);

    //The line below displays the print options dialog box, 
    //so the modified settings can be verified during development. 
    //It is not required for printing so it can removed, 
    //especially for non-interactive applications like windows services             
    int value = AdvancedDocumentProperties
                (IntPtr.Zero, IntPtr.Zero, e.PrintJob.Printer.Name, e.DevMode, e.DevMode);            

    //Set additional print settings as needed. 
    e.PrintJob.PrintOptions.Copies = 1;
}

The complete source code is available for download from the link at the top of this post.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralNice Article Pin
MayurDighe27-May-15 15:37
professionalMayurDighe27-May-15 15:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.