Dynamic adjustment printer paper size

zhaozj2021-02-16  138

Dynamic adjustment printer paper size

Jiangsu Kunshan City Local Taxation Bureau

Various print functions are often available in the information management system, such as report printing, document printing, and invoice printing. The size of the paper required during these printing processes is often inconsistent, for example, printing reports may need to use A4 paper or A3 paper, print credentials or invoices, may need to set the print paper into custom size. If you print this content on the same printer, you should set different paper sizes for different print contents. Obviously if you use a manual method to set the paper size of the printer is a very troublesome thing, the best way is to let the program dynamically modify the printer's paper size. Solving the Idea of ​​Problem By looking at the API function Technical documentation, each printer has a unique structure called DEVMODE, which is stored in this structure with the printer. Through analysis of the DEVMODE structure, there are four structural members related to setting printer paper size: DMFields, DMPAPAPERSIZE, DMPAPERLENGTH, and DMPAPERWIDTH. DMFields is the DEVMODE flag initialization section. If you want to modify some members in the structure, the corresponding position in DMFields should be set. DMPAPERSIZE indicates the size of the printer's current default print paper. To set the custom paper, it should be 0 (note, the above explanation is based on the technical documentation provided by Microsoft, but in Delphi, it should set the member to $ 100 256) . DMPAPERLENGTH and DMPAPAPERWIDTH are only used when setting custom large sheets, indicating the length and width of the paper, respectively. How to modify the printer's DEVMODE structure? There is no use of two methods, one is some of the methods in the TPrinter class provided by Delphi, and is therefore called the API function associated with printing. Below with Delphi as the development tool, this function is implemented in two different ways with the Delphi for the development tools in EPSON 1600K. Method 1: TPRINTER TPRINTER class using Delphi is the package of Delphi's Windows print processing system, which helps programmers reduce workload as much as possible when developing print printers. Using TPrinter in the program, just add Printers after the unit's USE clause is added. The handle of the DEVMODE structure of the current printer can be obtained by calling the getprinter method in the TPrinter class. After the program got the handle of the DEVMODE structure, the GLOBALLOCK function is called to get a pointer to this structure, which can then modify some members in the structure. Let's take an example to illustrate this problem: assuming that the default paper size of the current printer is A3 paper, now you have to print a length of 114mm, a width of 190mm. For the sake of simplicity, we only place a button1 button on Form1, add an OnClick event, and add Printers after the USE clause.

The code is as follows: procedure tform1.button1click (sender: TOBJECT); VAR Device: array [0..cchdevicename -1] of char; driver: array [0 .. (max_path -1)] of char; port: array [ 0..32] of char; HDMODE: THANDLE; PDMODE: PDEVMODE; Begin Printer.getPrinter (Device, Driver, Port, HDMode); // Get the handle value of the printer DEVMODE structure, stored in HDMode if HDMode <> 0 THEN Begin PDMode: = Globalock (HDMODE); // Get pointing to the printer DEVMODE structure // pointer if pdmode <> nil the beginning: = 256; // If you want to change the current printer paper into custom DMPAPAPAPAPAPERSIZE must be set to 256 pDMode ^ .dmPaperLength: = 1140; pDMode ^ .dmPaperWidth: = 1900; pDMode ^ .dmFields: = pDMode ^ .dmFields or DM_PAPERSIZE; pDMode ^ .dmFields: = pDMode ^ .dmFields or DM_PAPERLENGTH; pDMode ^ .dmFields: = PDMODE ^ .dmfields or DM_PaperWidth; {The above three statements are set to the corresponding DMFields members. } Resetdc (printer.handle, pdmode ^); // Set the value of the printer device environments GlobalUnlock (hdmode); end; end; {{below The code is to test whether the printer is pressed 190 * 114 paper size to print} with Printer Do Begin Begindoc; Canvas.TextOut (10, 10, 'Hello, My Friend!); ENDDOC; END; END; Method 2: Use the idea of ​​solving problems with the printed Windows API function, first get it, first get The current printer's devmode structure pointer, then modify the structure, modify the printer paper size. To complete the above features, you have to call the DocumentProperties function. With this function program, you can get and modify members in the DEVMODE structure associated with the current printer. DocumentProperties function stated as follows: LONG DocumentProperties (HWND hWnd, HANDLE hPrinter, LPTSTR pDeviceName, PDEVMODE pDevModeOutput, PDEVMODE pDevModeInput, DWORD fMode); pDevModeOutput six parameters are only output variable, the remaining five parameters must specific values ​​are given by the program.

Among them, HWnd characterizes the handle value of the current window; Hprinter represents the handle of the current printer; PDEVMEOUT is a pointer to the printer device for the printer device; PDEVModeOutput is a pointer to the printer DEVMODE structure of the HPRinter; PDEVModeInput is a pointer to a modified devmode structure. This structure is preserved by a printer acceptable by the handle value of Hprinter; FMODE defines the specific function of the function. If the value DM_IN_BUFFER indicates that the printer accepts the DEVMODE value modified by the program by the parameter PDEVModeInput, if the value DM_OUT_BUFFER is described You can get the DEVMODE value of the printer by parameter PDEVModeoutput. If Fmode is zero, the value returned by the function represents the number of bytes required for structural DEVMODE. To undertake the above example, the 190 * 114 size paper is now set to A4 paper. Place the button button2 on Form1, add the onclick event, and add Winspool after the USE clause. Code is as follows: Procedure TForm1.Button2Click (Sender: TObject); var PrnHd: THandle; PrnInfo: PPrinterInfo1; pcbNeeded: DWORD; PDevModeBytes: DWORD; DevMode: PDeviceMode; PrnHdc: HDC; DocInfo: PDocInfo; begin OpenPrinter ( 'Epson LQ -1600K ', PrnHd, nil); // get printer handle PrnHd GetMem (prnInfo, 1024); GetPrinter (PrnHd, 1, prnInfo, 1024, @ pcbNeeded); PDevModeBytes:. = DocumentProperties (handle, PrnHd, prninfo ^ pDescription, DevMode ^ , DEVMODE ^, 0); {Get the number of bytes required for the DEVMODE structure} getmem (devmode, pdevmodebytes); // Give Structure DEVMODE Assignment Space DocumentProperties (Handle, PRNHD, PRNINFO ^ .p Description, Devmode ^, devmode ^, DM_OUT_BUFFER); // printer is acquired DevMode structure With DevMode ^ do begin dmPaperSize: = DMPAPER_A4; // set to A4 paper sheet dmFields: = dmFields or DM_PAPERSIZE; end; DocumentProperties (Handle, PrnHd, PrnInfo ^ .pDescription, DevMode ^ , Devmode ^, dm_out_buffer or dm_in_buffer; // Modify the DEVMODE structure.

转载请注明原文地址:https://www.9cbs.com/read-9036.html

New Post(0)