Adding Custom Paper Sizes To Named Printers

CK CK 3788 
Created at
Updated at  
647 0 0

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>


 

Tags C# Print OnMediaChanged Print Facebook X
Comments 0
Similar posts
  1. Print HTML In C# With Or Without The Web Browser Control And The Print Dialog
    972
  2. A Customizable Printing Text Class
    432
  3. Simplified .NET Printing In C#
    692
  4. How To Print Text In C#
    455
  5. Printing A Page
    378
  6. Get A Print Dialog Box By Clicking A Button
    818
  7. How To Show The Print Dialog And Print Text Files
    1,295
  8. How to print external document ?
    414
  9. Changing the Title of a Print Dialog in Delphi
    1,093
  10. How To Check If Your Printer Is Connected Using C#
    1,026
  1. The best way to enable and disable buttons, menu items and toolbar buttons
    667
  2. How to exit from a console application
    811
  3. How to restrict a program to a single instance
    729
  4. Compress and decompress strings in C#
    971
  5. Graphics in C# - Irregular Forms
    908
  6. Extracting the Country from IP Address
    468
  7. How to run shell command by MFC ?
    435
  8. Binary Search Sample Code
    405
  9. Double Linked List based in C++
    796
  10. String Replace Function For C#
    1,547
  11. How to print character in C# by ASCII code
    476
  12. MFC based World Wide Web HTTP Server Source Code
    459
  13. Creating and Using a CAsyncSocket Object to use CAsyncSocket
    472
  14. UDP Send and Receive Using CAsyncSocket
    1,204
  15. Using the shell to receive notification of removable media being inserted or removed
    680
  16. Print HTML In C# With Or Without The Web Browser Control And The Print Dialog
    972
  17. A Customizable Printing Text Class
    432
  18. Simplified .NET Printing In C#
    692
  19. How To Print Text In C#
    455
  20. C# Lists
    1,151
  21. Simple Example of IF/THEN using C#
    1,265
  22. Fast and Good Keyboard/Mouse Test without the message handler
    982
  23. Simple CGI Programming in C
    545
  24. C++ CGI Example working on Apache
    484
  25. Simple C# CGI working on Apache
    480
  26. How to send binary data through C# CGI app?
    994
  27. Interacting With TinyPic From C#
    1,303
  28. Creating A Watched Folder With Assigned Events
    1,658
  29. Calling Your Main Thread From A Worker Thread In C#
    546
  30. HashTable Tutorial In C#
    1,787
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    40
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    34
  3. How do I create animated background for Google Meet?
    20,611
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    244
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  7. Array of arguments passed to script in PHP
    90
  8. Invalid Auction Storms in Ad Tech
    98
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  10. The Two Clocks of Ad Server Architecture
    70
  11. Frequency Cap Architecture Overview
    514
  12. Collecting Proof-of-Play from 100,000 Screens
    146
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    287
  14. What is RedisVL?
    109
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    230
  16. TPU & GPU Cluster Management Tools
    243
  17. The Concept of Scaffolding in AI Agents
    233
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    258
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  22. Machine Learning: Training vs. Ranking
    221
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  25. Designing a Data Curation System Around a Vector Database
    220
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    195
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    217
  30. Understanding Data Lineage
    260