Click here to Skip to main content
15,890,579 members
Articles / Programming Languages / C#

Adding Custom Paper Sizes to Named Printers

Rate me:
Please Sign up or sign in to vote.
4.92/5 (31 votes)
11 Nov 20054 min read 338.6K   10.1K   59   66
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:

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

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

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

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

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

And create the paper size:

C#
// 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:

C#
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.

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.


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

 
Generalhi thanks a lot i have another problem Pin
syed tameem27-Jul-08 18:20
syed tameem27-Jul-08 18:20 
GeneralPaper size of printer is set to Letter instead of custom paper size Pin
syed tameem22-Jul-08 2:34
syed tameem22-Jul-08 2:34 
GeneralRe: Paper size of printer is set to Letter instead of custom paper size Pin
efrainro24-Jul-08 3:46
efrainro24-Jul-08 3:46 
GeneralRe: Paper size of printer is set to Letter instead of custom paper size Pin
ngoctinhquangngai3-Jun-12 23:50
ngoctinhquangngai3-Jun-12 23:50 
GeneralIt is Not adding with hp1010 Pin
Gowir Shankar27-Jun-08 21:40
Gowir Shankar27-Jun-08 21:40 
GeneralIt's all working, Except I want to change ... Pin
Vishram16-Jun-08 18:45
Vishram16-Jun-08 18:45 
QuestionCustom size not added Pin
Member 279762812-Jun-08 3:00
Member 279762812-Jun-08 3:00 
QuestionIt doesn't work for me :( Pin
D_Friendly_Bear9-Mar-08 13:30
D_Friendly_Bear9-Mar-08 13:30 
Hi twostepted,
I am REALLY new to the whole printing framework in C# and I have to say, that it seems WAY more complicated than it needs to be. (Don't even get me started on how long I spent trying to understand and work with the multi-page printing system for .Net!!! WTF | :WTF: )
I used to work with the Printing Architecture in MFC and had a great deal more success with that than I have had so far with the .Net version.

I have managed to get basic printing working no worries, HOWEVER, I am writing an application at the moment that requires several custom paper sizes and I am having an extremely difficult time getting the printer (it seems - .Net says all is right with the world) to co-operate.

I can print to a standard A4 page and get all printer operations to work correctly no problem at all (which is great), but as I said, I need to print on custom paper size(s).

I have tried MSDN, Technet, CodeProject and a variety of other sources (not as reputable), along with MANY hours scouring GOOGLE trying to get even the smallest amount of USEFUL information. Confused | :confused: Mad | :mad:

It turns out, Cool | :cool: the article and source code you have provided Cool | :cool: is one of the ONLY examples ANYWHERE WTF | :WTF: of setting custom paper sizes on printers, let alone any kind of intelligent discussion of how to achieve it.

Anyway, enough of that rambling. Shucks | :-\

My problem, as I indicated, is that I need to print to several custom size forms.
I have tried creating a custom paper size using the .Net PaperSize class, which all works in code, but when the paper is sent to the printer, i.e. the printer is told to use that paper size, it just ignores it and prints on the default (usually A4).
This is highly frustrating as when I Preview the page, using a standard Print Preview dialog, or control, the page displays correctly (right size).

I have tried at least three different models of Printer, Canon LBP-2410, Brother MFC-9180 and a Dell 1320C. All Printers run the code properly and there are no errors reported, but when the paper sizes are listed the custom paper is NEVER displayed or available, even through code.

I am getting extremely Mad | :mad: frustrated Mad | :mad: with this whole thing as it should be (IMHO) fairly simple to create, select and use a custom paper size on a printer. ( I can do it using WORD for goodness sake, why is it so hard!!!!).

Anyway, I would appreciate any help or information that you or any other reader/coder/janitor/garbage man ..... or anyone could provide.

Kind Regards,
D Poke tongue | ;-P
QuestionEPSON LQ 2180 Pin
Humair1-Feb-07 21:33
Humair1-Feb-07 21:33 
AnswerRe: EPSON LQ 2180 Pin
twostepted2-Feb-07 8:37
twostepted2-Feb-07 8:37 
GeneralRe: EPSON LQ 2180 Pin
Humair3-Feb-07 12:27
Humair3-Feb-07 12:27 
GeneralContact me at componentworks.net Pin
twostepted18-Jan-07 17:15
twostepted18-Jan-07 17:15 
Generalthanks for this nice code Pin
Shah Kazmi18-Jan-07 6:48
Shah Kazmi18-Jan-07 6:48 
GeneralRe: thanks for this nice code Pin
twostepted18-Jan-07 17:12
twostepted18-Jan-07 17:12 
GeneralIt made some kind of problem Pin
Aviad Barel16-Nov-06 5:45
Aviad Barel16-Nov-06 5:45 
GeneralRe: It made some kind of problem Pin
twostepted16-Nov-06 7:42
twostepted16-Nov-06 7:42 
QuestionDo you have these code in VB.net? Pin
taneyezone14-Nov-06 22:13
taneyezone14-Nov-06 22:13 
AnswerRe: Do you have these code in VB.net? Pin
twostepted14-Nov-06 23:45
twostepted14-Nov-06 23:45 
AnswerRe: Do you have these code in VB.net? Pin
junk3305-Apr-07 12:14
junk3305-Apr-07 12:14 
AnswerRe: Do you have these code in VB.net? Pin
Deoinbox2-Aug-07 16:47
Deoinbox2-Aug-07 16:47 
GeneralWhy does it not work Pin
Murre5-Nov-06 21:40
Murre5-Nov-06 21:40 
GeneralRe: Why does it not work Pin
Murre6-Nov-06 20:28
Murre6-Nov-06 20:28 
GeneralRe: Why does it not work Pin
tommydxz220-Aug-07 3:35
tommydxz220-Aug-07 3:35 
GeneralAdding form Pin
Amphysvena6-Dec-05 20:27
Amphysvena6-Dec-05 20:27 
GeneralRe: Adding form Pin
twostepted7-Dec-05 18:44
twostepted7-Dec-05 18:44 

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.