First, we define a PrintDocument object and implement the PrintPage method:
In the event of clicking a button, print and preview:
After you execute print preview, we will see the printed results:
Custom Print Preview
For custom print preview, you must set the attributes of the specified print controller PrintController.
PrintController is used by PrintDocument, which is usually not direct.
. NET Framework includes three print controllers derived from PrintController to help complete common tasks.
1.
StandardPrintControllerPrint the document to the printer.
2.
PreviewPrintControllerThe appearance preview during document printing is used by the PrintPreviewControl and PrintPreviewDialog classes.
3.
PrintControllerWithStatusDialogA print status dialog box is provided during the printing process.
| The code is as follows: |
Copy code |
|
Manually implement a print preview function
PreviewPrintControllerTo achieve
|
First, define
PreviewPrintControllerAnd assigned to PrintDocument. Then print it.
GetPreviewPageInfo () is used to obtain a PreviewPageInfo array. The printed information is included in this array, and each image and information page is stored in the array.
The print information provided by the Image and PhysicalSize objects in the array: the size of an Image and the print page. The size of the printed image is different from that of the printed page. The size of the printed image is higher than that of the printed page. So we need to convert the size. The page is displayed. To print multiple pages, you only need to switch the PreviewPageInfo array.
When you need to display the print status dialog box, you can use
PrintControllerWithStatusDialogPackage One
PreviewPrintControllerTo display the print dialog box
PrintDocument. PrintController = new PrintControllerWithStatusDialog (previewController );