| My company needed me to programmatically add a custom paper size (a printer form) to the default printer and set the printer to use the custom paper size. After looking around,the best alternative we could find was to make use of the printing and print spooling Win32 API functions found in winspool.drv. The functions used in the project are AddForm,DeleteForm,DocumentProperties,GetPrinter and SetPrinter along with some others. The target platform is WinNT / 2000 / XP although there was already some support for earlier versions of Windows added before I began working on the project. I've never tried to use that section of code but I'm pretty sure that it will only add the custom paper size and not set the printer to use it. <PRE lang=cs nd="15">
using System.Drawing.Printing;
...
PrintDocument pd = new PrintDocument();
string sPrinterName = pd.PrinterSettings.PrinterName;</PRE>
<P nd="18">After getting the printer name,get a handle to the printer:</P><PRE lang=cs nd="20">bool bGotPrinter = OpenPrinter(printerName,out hPrinter,ref defaults);</PRE>
<P nd="23">At this point,we can delete the custom paper size by name:</P><PRE lang=cs nd="25">// delete the form incase it already exists
DeleteForm(hPrinter,paperName);</PRE>
<P nd="26">And create the paper size:</P><PRE lang=cs nd="28">
// create and initialize the FORM_INFO_1 structure
FormInfo1 formInfo = new FormInfo1();
formInfo.Flags = 0;
formInfo.pName = paperName;
// all sizes in 1000ths of millimeters
formInfo.Size.width = (int)(widthMm * 1000.0);
formInfo.Size.height = (int)(heightMm * 1000.0);
formInfo.ImageableArea.left = 0;
formInfo.ImageableArea.right = formInfo.Size.width;
formInfo.ImageableArea.top = 0;
formInfo.ImageableArea.bottom = formInfo.Size.height;</PRE>
<P nd="38">Add the paper size to the printer's list of available paper
sizes:</P><PRE lang=cs nd="40">bool bFormAdded = AddForm(hPrinter,1,ref formInfo);</PRE>
|