Click here to Skip to main content
11,479,486 members (54,810 online)
Click here to Skip to main content

Adding custom paper sizes to named printers

, 11 Nov 2005 199.6K 6.4K 57
Rate this:
Please Sign up or sign in to vote.
Using PIinvoke and marshaling techniques to call the winspool library's AddForm.

Introduction

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.

Challenges

Calling these from C# required a little bit more knowledge of marshaling techniques than I had under my belt as I was only able to get as far as getting a handle to the default printer using the winspool functions GetDefaultPrinter and OpenPrinter before requiring some help. At that point, we opened a support case with Microsoft and they sent a nice C# project with the working AddForm calls. However, when we asked about setting the printer to use the newly created form, the best they could do was provide a VB.NET project. So, I ported VB.NET over to C#. Our last requirement was to signal the previously open applications of the settings change and this was done using SendMessageTimeout from user32.dll.

Implementation

Rather than make a call to the Win32 GetDefaultPrinter function (which you have to call twice, once to get the size of the string and another time to get the string) there is an easier way to get the default printer name provided in the .NET platform:

using System.Drawing.Printing;
...
PrintDocument pd = new PrintDocument();
string sPrinterName = pd.PrinterSettings.PrinterName;

After getting the printer name, get a handle to the printer:

bool bGotPrinter = OpenPrinter(printerName, out hPrinter, ref defaults);

At this point, we can delete the custom paper size by name:

// delete the form incase it already exists
DeleteForm(hPrinter, paperName);

And create the paper size:

// 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;

Add the paper size to the printer's list of available paper sizes:

bool bFormAdded = AddForm(hPrinter, 1, ref formInfo);

I'll leave setting the printer to use the newly added form and signaling open apps of the settings change out for now. Have a look at the demo project, its all there.

Caveat

This project is currently set up to add a custom paper size of 104 mm*4000 mm to the default printer. It attempts to do this in the Form1_Load event of Test.cs. If you run this on a computer with a default printer that doesn't support these dimensions, the paper size will not show up in the list of available paper sizes for your default printer and no exceptions will be generated. If you'd like to test run a test that will work, specify a smaller paper size that your printer will support. You can convert millimeters to inches by multiplying the millimeters by 25.4. I didn't take the time to add any 'add custom paper size in inches' functionality to this project. It could be done very easily by dividing the inches by 25.4 and passing off to the 'add custom paper size by mm' function which already exists.

Check to see if it worked

The project is set to open small form with single 'Open Dialog' button after adding the custom paper size. Clicking the button opens a print dialog window. To see the list of available printer sizes for the default printer, click the properties button (opens the Properties window) and then the advanced button (opens advanced options window). You can now see the list of available paper sizes for the printer.

To check that it worked from Windows XP, open the printers and faxes window (start menu, printers and faxes), right click on your default printer (the one with the check box next to it) and select properties (opens Properties window), click the printing preferences button (opens Preferences window), click the advanced button (opens Advanced Options window) and then you can see a list of paper sizes.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

twostepted

United States United States
No Biography provided

Comments and Discussions

 
GeneralThanks It works for me Pin
akishmu26-Jun-14 18:47
memberakishmu26-Jun-14 18:47 
QuestionWhy Page setting is not added to default printer (POS-58C) ????? Pin
Azeem.5-Feb-14 8:11
memberAzeem.5-Feb-14 8:11 
GeneralMy vote of 4 Pin
Member 476897529-Sep-13 20:41
memberMember 476897529-Sep-13 20:41 
QuestionI'm not dowload file [modified] Pin
ngoctinhquangngai3-Jun-12 22:16
memberngoctinhquangngai3-Jun-12 22:16 
Generalmy vote of 5 Pin
Yifeng Ding28-Apr-12 20:05
memberYifeng Ding28-Apr-12 20:05 
GeneralRe: my vote of 5 Pin
ngoctinhquangngai4-Jun-12 0:51
memberngoctinhquangngai4-Jun-12 0:51 
I'm not download source, please send to mail: ngoctinhquangngai@gmail.com. Thank!
GeneralMy vote of 5 Pin
Yifeng Ding28-Apr-12 19:58
memberYifeng Ding28-Apr-12 19:58 
QuestionTôi muốn in khổ nhỏ hơn a5 Pin
ngoctinhquangngai4-Apr-12 16:56
memberngoctinhquangngai4-Apr-12 16:56 
GeneralMy vote of 5 Pin
igetorix10-Feb-12 5:12
memberigetorix10-Feb-12 5:12 
QuestionThank you .You saved my time so much Pin
siminal14-Dec-11 1:09
membersiminal14-Dec-11 1:09 
Questionvb6.0 query error Pin
kiran76342511-Oct-11 1:40
memberkiran76342511-Oct-11 1:40 
QuestionIt's Cool Pin
Nattawutxp8-Aug-11 18:35
memberNattawutxp8-Aug-11 18:35 
GeneralIt does not work for me Pin
scarus1-Feb-11 0:04
memberscarus1-Feb-11 0:04 
GeneralRe: It does not work for me Pin
FALU2315-Mar-11 2:23
memberFALU2315-Mar-11 2:23 
GeneralRe: It does not work for me Pin
ISharp13-Dec-11 17:13
memberISharp13-Dec-11 17:13 
GeneralMy vote of 5 Pin
pandian7811-Nov-10 1:36
memberpandian7811-Nov-10 1:36 
GeneralMy vote of 4 Pin
ADELASSI18-Aug-10 3:11
memberADELASSI18-Aug-10 3:11 
GeneralIt add in XPS Printer only Pin
Khaniya25-Jun-10 21:56
memberKhaniya25-Jun-10 21:56 
GeneralRe: It add in XPS Printer only Pin
Member 92379389-Oct-12 2:13
memberMember 92379389-Oct-12 2:13 
Generali have a question from my class Pin
kristal1238-Sep-09 4:28
memberkristal1238-Sep-09 4:28 
QuestionReseting the PaperSize Pin
GALE_29-Jun-09 8:02
memberGALE_29-Jun-09 8:02 
AnswerRe: Reseting the PaperSize Pin
Member 923793821-Oct-12 22:03
memberMember 923793821-Oct-12 22:03 
Generalvery goog Pin
ali2000031-May-09 6:36
memberali2000031-May-09 6:36 
Generalhi thanks a lot i have another problem Pin
syed tameem27-Jul-08 19:20
membersyed tameem27-Jul-08 19:20 
GeneralPaper size of printer is set to Letter instead of custom paper size Pin
syed tameem22-Jul-08 3:34
membersyed tameem22-Jul-08 3:34 
GeneralRe: Paper size of printer is set to Letter instead of custom paper size Pin
efrainro24-Jul-08 4:46
memberefrainro24-Jul-08 4:46 
GeneralRe: Paper size of printer is set to Letter instead of custom paper size Pin
ngoctinhquangngai4-Jun-12 0:50
memberngoctinhquangngai4-Jun-12 0:50 
GeneralIt is Not adding with hp1010 Pin
Gowir Shankar27-Jun-08 22:40
memberGowir Shankar27-Jun-08 22:40 
GeneralIt's all working, Except I want to change ... Pin
Vishram16-Jun-08 19:45
memberVishram16-Jun-08 19:45 
QuestionCustom size not added Pin
Member 279762812-Jun-08 4:00
memberMember 279762812-Jun-08 4:00 
QuestionIt doesn't work for me :( Pin
D_Friendly_Bear9-Mar-08 14:30
memberD_Friendly_Bear9-Mar-08 14:30 
QuestionEPSON LQ 2180 Pin
Humair1-Feb-07 22:33
memberHumair1-Feb-07 22:33 
AnswerRe: EPSON LQ 2180 Pin
twostepted2-Feb-07 9:37
membertwostepted2-Feb-07 9:37 
GeneralRe: EPSON LQ 2180 Pin
Humair3-Feb-07 13:27
memberHumair3-Feb-07 13:27 
GeneralContact me at componentworks.net Pin
twostepted18-Jan-07 18:15
membertwostepted18-Jan-07 18:15 
Generalthanks for this nice code Pin
Shah Kazmi18-Jan-07 7:48
memberShah Kazmi18-Jan-07 7:48 
GeneralRe: thanks for this nice code Pin
twostepted18-Jan-07 18:12
membertwostepted18-Jan-07 18:12 
GeneralIt made some kind of problem Pin
Aviad Barel16-Nov-06 6:45
memberAviad Barel16-Nov-06 6:45 
GeneralRe: It made some kind of problem Pin
twostepted16-Nov-06 8:42
membertwostepted16-Nov-06 8:42 
QuestionDo you have these code in VB.net? Pin
taneyezone14-Nov-06 23:13
membertaneyezone14-Nov-06 23:13 
AnswerRe: Do you have these code in VB.net? Pin
twostepted15-Nov-06 0:45
membertwostepted15-Nov-06 0:45 
AnswerRe: Do you have these code in VB.net? Pin
junk3305-Apr-07 13:14
memberjunk3305-Apr-07 13:14 
AnswerRe: Do you have these code in VB.net? Pin
Deoinbox2-Aug-07 17:47
memberDeoinbox2-Aug-07 17:47 
GeneralWhy does it not work Pin
Murre5-Nov-06 22:40
memberMurre5-Nov-06 22:40 
GeneralRe: Why does it not work Pin
Murre6-Nov-06 21:28
memberMurre6-Nov-06 21:28 
GeneralRe: Why does it not work Pin
tommydxz220-Aug-07 4:35
membertommydxz220-Aug-07 4:35 
GeneralAdding form Pin
Amphysvena6-Dec-05 21:27
memberAmphysvena6-Dec-05 21:27 
GeneralRe: Adding form Pin
twostepted7-Dec-05 19:44
membertwostepted7-Dec-05 19:44 
GeneralRe: Adding form Pin
Amphysvena7-Dec-05 20:28
memberAmphysvena7-Dec-05 20:28 
GeneralRe: Adding form Pin
twostepted11-Dec-05 17:56
membertwostepted11-Dec-05 17:56 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150520.1 | Last Updated 11 Nov 2005
Article Copyright 2005 by twostepted
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid