It has always been a headache to use custom paper printing. Many times when considering the project solution, this problem will become an important factor in the selection of B/S or C/S (in fact, I am too lazy to do it myself ). However, in this project, I made up my mind to solve the problem of Custom paper printing perfectly. So I finally got a lot of related information from Google.
To be honest, you only need to understand the mechanism of Custom paper in windows. In the NT-based Windows operating system, print spooling manages the settings of all printers. To add a custom paper, you must set it in the print server. For manual operations, you can find the settings of the print server in the printer folder of the control panel, and then add custom paper in the Settings dialog box. If you want to implement all these functions programmatically, you must use the related functions of winspool. DRV. There are not many related functions, but the data structure is complicated. Because C # is used, native types must be mapped to C # Before using these functions.
Fortunately, most of the functions have been completed for me. Twostepted: an article published in code projectArticleAdding custom paper sizes to named printers has completed most of the functions for me, and you need to do it yourself. In hisProgramThe user-defined paper addition, deletion, and default setting functions are implemented through API calls, but these functions are all mixed together, so I put hisCodeSome modifications have been made to meet my requirements. Let's take a look at the functional testing interface.
I don't need to talk about the function. You can understand it at a glance. In order to make this article have a little technical component, the following describes several related API functions:
OpenprinterAll operations need to be connected to the printer first. That's good. It's just used. If the execution is successful, a handle corresponding to the printer name is returned.
AddformThe form here is not a form, but a form, or it can be translated into a form, that is, the settings of the paper. Obviously, its function is to add a kind of paper.
DeleteformDelete the paper settings. Of course, only Custom paper can be deleted.
CloseprinterOf course, you have to turn it off after use.
If you are interested, you can download the source code to try it out.
NOTE: If UAC is enabled, start vs as an administrator.