Print and print preview of the C # tutorial

Source: Internet
Author: User

4.1 Print and Print preview

Print and print Preview is a feature that an editor must have, and this section describes how to implement print and print previews. In general, the following menu items are implemented: Print, Print preview, page setup.

4.8.1PrintDocument class

The PrintDocument component is the class used to complete the printing, with its common properties, methods, and events as follows:
L Property DocumentName: A string type that records the name of the document that is displayed when the document is printed (for example, displayed in the Print Status dialog box or in the printer queue).
L Method Print: Begins the printing of the document.
L Event BeginPrint: Occurs before the first page of a document is printed after the print method is called.
L Event PrintPage: Occurs when a new page needs to be printed.
L Event EndPrint: Occurs after the last page of the document is printed.
To print, first create an object for the PrintDocument component. You then use the Page Setup dialog box PageSetupDialog to set how the page is printed, which is the default setting for all pages to be printed. Use the Print dialog box PrintDialog to set the parameters for the printer to print to the document. Before opening two dialog boxes, first set the properties of the dialog box to document the specified PrintDocument class object, and the modified settings will be saved to the PrintDocument Component object. The third step is to call the Printdocument.print method to actually print the document. When the method is called, the following events are raised: BeginPrint, PrintPage, EndPrint. The PrintPage event is raised for each page printed, and multiple pages are printed, and the PrintPage event is raised multiple times. Once printed, you can raise one or more PrintPage events.
Programmers should write event handlers for these 3 events. The BeginPrint event handler is used for print initialization and is typically set to the same properties or common resources for all pages when printing, such as the fonts that all pages use together, the file stream to print, and so on. The PrintPage event handler is responsible for printing one page of data. The EndPrint event handler functions for printing after work. The 2nd parameter of these handler functions, System.Drawing.Printing.PrintEventArgs E, provides some additional information, mainly:
L E.cancel: Boolean variable, set to True, cancels this print job.
L E.graphics: The device environment of the printer used, see Chapter fifth.
L E.hasmorepages: Boolean variable. After the PrintPage event handler prints a page, the data is still not printed, Hasmorepages=true is set before exiting the event handler, and after exiting the PrintPage event handler, the PrintPage event is raised again, and the next page is printed.
L E.marginbounds: The size of the print area, is the rectangle structure, and the elements include the upper-left coordinates of the top, width and height: width and height. The unit is 1/100 inches.
L E.marginbounds: The size of the printing paper, is the rectangle structure. The unit is 1/100 inches.
L E.pagesettings:pagesettings Class object that contains all the information about how the page is printed using the dialog box PageSetupDialog. You can use Help to view the properties of the PageSettings class.
The following is a procedure for writing event handlers for these 3 events:
(1) Add a statement after the last using statement:
Using System.IO;
Using System.Drawing.Printing;
(2) This example prints or previews the contents of the RichTextBox, adding the variable: StringReader streamtoprint=null. If you print or preview the file, instead: StreamReader Streamtoprint, the concept of flow is described in chapter sixth. Variable to increase the font used for printing: Font printfont.
(3) Put the PrintDocument control into the form, and the property name is PrintDocument1.
(4) Add the BeginPrint event handler function for PrintDocument1 as follows:
private void Printdocument1_beginprint (object sender,
System.Drawing.Printing.PrintEventArgs e)
{printfont=richtextbox1.font;//fonts used for printing
Streamtoprint=new StringReader (richTextBox1.Text);//Print richTextBox1.Text
}//such as preview file to: streamtoprint=new StreamReader ("File path and filename");
(5) The PrintPage event handler function for PrintDocument1 is as follows. Streamtoprint.readline () reads a piece of data and may print multiple lines. This event handler prints this piece of data on one line, so the method must be improved.
private void Printdocument1_printpage (object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{Float linesperpage=0;//records the maximum number of rows per page
Float ypos=0;//Record the vertical position of the row of data that will be printed
int count=0;//records the number of rows printed per page
Float leftmargin=e.marginbounds.left;//left margin
Float topmargin=e.marginbounds.top;//Top Margin
String line=null;//reads a character from the RichTextBox to be stored in line
Maximum number of rows per page = height of one page print area/line character
Linesperpage=e.marginbounds.height/printfont.getheight (E.graphics);
Continue printing If the current page has been printed less than the maximum number of rows per page and the read data is not NULL
while (count<linesperpage&& ((Line=streamtoprint.readline ())!=null))
{//ypos is the position of the current line to be printed in the vertical direction
ypos=topmargin+ (Count*printfont.getheight (e.graphics));
E.graphics.drawstring (Line,printfont,brushes.black,
Leftmargin,ypos,new StringFormat ());//print, see Chapter Fifth
count++;//number of printed lines plus 1
}
if (line!=null)//Whether you need to print the next page
e.hasmorepages=true;//need to print next page
Else
e.hasmorepages=false;//don't need to print the next page
}
(6) Add the EndPrint event handler function for PrintDocument1 as follows:
private void Printdocument1_endprint (object sender,
System.Drawing.Printing.PrintEventArgs e)
{if (streamtoprint!=null)
Streamtoprint.close ();//Release unused resources
}

4.8.2 Print Setup dialog box control PageSetupDialog

The PageSetupDialog control for Windows Forms is a Page Setup dialog box that lets you set the details of the printed page in a Windows application, and the appearance of the dialog box 4.8.2.

Figure 4.8.2
The user can use this dialog box to set the paper size (type), paper source, portrait and landscape printing, and up and down margins, and so on. Before opening the dialog box, first set its property document to the specified PrintDocument class object, which is used to save the page settings to the PrintDocument class object. The steps for adding page Setup functionality to a text editor are as follows:
(7) Add menu items to the pop-up menus of the file's top menu items: Page setup.
(8) Put the PageSetupDialog control into the form, and the property name is PageSetupDialog1.
(9) Add the Click event handler function for the page Setup menu item as follows:
private void Menuitem5_click (Object Sender,system.eventargs e)
{pagesetupdialog1.document=printdocument1;
Pagesetupdialog1.showdialog ();
}
(10) After you open the dialog box PageSetupDialog1, if you click the OK button, the page settings in the PageSetupDialog dialog box are saved to the PrintDocument class object PrintDocument1, and if you click the Cancel button, Do not save these changes to maintain the original value. When the Printdocument.print method is called to actually print the document, the PrintPage event is raised, and the second parameter e of the event handler provides these settings information.

4.8.3 Print Preview

with The PrintPreviewDialog class can display the PrintDocument print effect on the screen, printing a preview. The steps to implement print preview are as follows:
(11) Add menu items to the pop-up menus for file top menu items: Print Preview.
(12) Put the PrintPreviewDialog control into the form, and the property name is PrintPreviewDialog1.
(13) Add the Click event handler function for the Print Preview menu item as follows:
private void Menuitemprintview_click (Object Sender,system.eventargs e)
{printpreviewdialog1.document=printdocument1;
Printpreviewdialog1.showdialog ();
}
(14) Compile, run, enter several characters, test the preview effect, preview the effect 4.8.3.

Figure 4.8.3

4.8.4 Printing with the Print dialog box PrintDialog

The PrintDialog component is a pre-defined dialog box in the class library that sets parameters for the printer that prints the document, including the name of the printer, the page to print (all or the specified page range), the number of copies printed, and whether to print to a file. Before opening the dialog box, first set its property document to the specified PrintDocument class object, and when the PrintDialog dialog box is opened, the modified settings are saved to the object of the PrintDocument class. PrintDialog The appearance of the dialog box 4.8.4.

Figure 4.8.4
The steps to increase the printing function are as follows:
(15) Put PrintDialog control to form property Name=printdialog1.
(16) Add menu items to the pop-up menus for file top menu items: Print.
(17) Add the Click event handler for the Print menu item as follows: (Can't print?) )
private void Menuitemprint_click (object sender, System.EventArgs e)
{printdialog1.document=printdocument1;
if (Printdialog1.showdialog (this) ==dialogresult.ok)
Printdocument1.print ();
}
(18) Compile, run, enter a number of characters, test the printing effect.

Print and print preview of the C # tutorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.