Click here to Skip to main content
15,073,499 members
Articles / Programming Languages / C#
Posted 11 Nov 2005


59 bookmarked

Adding Custom Paper Sizes to Named Printers

Rate me:
Please Sign up or sign in to vote.
4.91/5 (30 votes)
11 Nov 20054 min read
Using PIinvoke and marshaling techniques to call the winspool library's AddForm


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.


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.


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; = 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, it's all there.


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.


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.


About the Author

United States United States
No Biography provided

Comments and Discussions

QuestionLimitation Pin
Member 1308759411-Dec-19 13:50
MemberMember 1308759411-Dec-19 13:50 
AnswerRe: Limitation Pin
Member 1308759412-Dec-19 3:52
MemberMember 1308759412-Dec-19 3:52 
Questionconvert to x64 Pin
Member 1308759417-Apr-18 8:09
MemberMember 1308759417-Apr-18 8:09 
AnswerRe: convert to x64 Pin
mthv21-Jun-18 22:55
Membermthv21-Jun-18 22:55 
GeneralRe: convert to x64 Pin
Member 122479277-Sep-18 5:37
MemberMember 122479277-Sep-18 5:37 
Questionneed help Pin
Member 1200832126-Jun-17 6:47
MemberMember 1200832126-Jun-17 6:47 
QuestionCustom Page size to DMP Pin
Er_bohra16-Nov-15 21:30
MemberEr_bohra16-Nov-15 21:30 
GeneralThanks It works for me Pin
akishmu26-Jun-14 17:47
Memberakishmu26-Jun-14 17:47 
GeneralRe: Thanks It works for me Pin
Zero Delta Omega4-Apr-17 20:02
MemberZero Delta Omega4-Apr-17 20:02 
QuestionWhy Page setting is not added to default printer (POS-58C) ????? Pin
DoingWork5-Feb-14 7:11
MemberDoingWork5-Feb-14 7:11 
Nice sharing but Why Page setting is not added to default printer (POS-58C) ????? It is added to XPS document writer.
I verify it by adding break points. POS-58C is gotten as default printer but when I open test, it is not displaying in POS-58C.

Please guide me.
GeneralMy vote of 4 Pin
Meher Khan29-Sep-13 19:41
professionalMeher Khan29-Sep-13 19:41 
QuestionI'm not dowload file Pin
ngoctinhquangngai3-Jun-12 21:16
Memberngoctinhquangngai3-Jun-12 21:16 
Generalmy vote of 5 Pin
Yifeng Ding28-Apr-12 19:05
MemberYifeng Ding28-Apr-12 19:05 
GeneralRe: my vote of 5 Pin
ngoctinhquangngai3-Jun-12 23:51
Memberngoctinhquangngai3-Jun-12 23:51 
GeneralMy vote of 5 Pin
Yifeng Ding28-Apr-12 18:58
MemberYifeng Ding28-Apr-12 18:58 
QuestionTôi muốn in khổ nhỏ hơn a5 Pin
ngoctinhquangngai4-Apr-12 15:56
Memberngoctinhquangngai4-Apr-12 15:56 
GeneralMy vote of 5 Pin
igetorix10-Feb-12 4:12
Memberigetorix10-Feb-12 4:12 
QuestionThank you .You saved my time so much Pin
siminal14-Dec-11 0:09
Membersiminal14-Dec-11 0:09 
Questionvb6.0 query error Pin
kiran76342511-Oct-11 0:40
Memberkiran76342511-Oct-11 0:40 
QuestionIt's Cool Pin
Nattawutxp8-Aug-11 17:35
MemberNattawutxp8-Aug-11 17:35 
GeneralIt does not work for me Pin
scarus31-Jan-11 23:04
Memberscarus31-Jan-11 23:04 
GeneralRe: It does not work for me Pin
FALU2315-Mar-11 1:23
MemberFALU2315-Mar-11 1:23 
GeneralRe: It does not work for me Pin
ISharp13-Dec-11 16:13
MemberISharp13-Dec-11 16:13 
GeneralMy vote of 5 Pin
pandian7811-Nov-10 0:36
Memberpandian7811-Nov-10 0:36 
GeneralMy vote of 4 Pin
ADELASSI18-Aug-10 2:11
MemberADELASSI18-Aug-10 2:11 

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.