C # printdocument print multi-page print preview

Source: Internet
Author: User

All subscription events of the printdocument instance are as follows:

1. Create a printdocument instance as follows:
System. Drawing. Printing. printdocument doctoprint =
New system. Drawing. Printing. printdocument ();
2. Set the event processing function for the printer to start printing. The original function is as follows:
Void doctoprint_printpage (Object sender,
System. Drawing. Printing. printpageeventargs E)
3. Add the event handler function to the printpage event of printdocument.
Doctoprint. printpage + = new printpageeventhandler (doctoprint_printpage );
4. Set attributes of printdocument, such:
Printdialog1.allowsomepages = true; printdialog1.showhelp = true;
5. Set the document attribute of printdialog to the configured printdocument instance:
Printdialog1.document = doctoprint;
6. Call the showdialog function of printdialog to display the Print dialog box:
Dialogresult result = printdialog1.showdialog ();
7. Start printing based on the user's choice:
If (result = dialogresult. OK)
{
Doctoprint. Print ();
}
8. Print the preview control printpreviewdialog
Example:

Create a printservice instance and call the void startprint (Stream streamtoprint, string streamtype) function to start printing. Streamtoprint indicates the content to be printed (byte stream), streamtype indicates the stream type (txt indicates normal text, and image indicates image );

Public partial class printtxt {private printpreviewdialog printpreview = new printpreviewdialog (); Private string streamtype; private image = NULL; private stream streamtoprint = NULL; font mainfont = new font (" ", 12); // print the Public String filename = NULL; // 1. instantiate the printed document printdocument pddocument = new printdocument (); Private string [] lines; private int linesprinted; public printtxt (string fil Epath, string filetype) {filename = path. getfilenamewithoutextension (filepath); // subscribe to the beginprint event pddocument. beginprint + = new printeventhandler (pddocument_beginprint); // release the resource pddocument. printpage + = new printpageeventhandler (onprintpage); // subscribes to the print event. This method must be placed at the end of the print event subscription filestream FS = new filestream (filepath, filemode. open, fileaccess. read); startprint (FS, filetype); // print the end pddocument. Endprint + = new printeventhandler (pddocument_endprint);} // 2. Start the print printing method public void startprint (Stream streamtoprint, string streamtype) {// pagesettings A4 \ A5 pagesettings PS = new pagesettings (); // display setting print page dialog box pagesetupdialog PSDL = new pagesetupdialog (); // print multiple settings, note: This method must be placed after the printpage method. Printdialog Pt = new printdialog (); PT. allowcurrentpage = true; PT. allowsomepages = true; PT. allowprinttofile = true; streamtoprint = streamtoprint; // print the byte stream streamtype = streamtype; // print the type of pddocument. documentname = filename; // printed file name PSDL. document = pddocument; printpreview. document = pddocument; PT. document = pddocument; PSDL. pagesettings = pddocument. defaultpagesettings; try {// displayed dialog box if (PSDL. Showdialog () = dialogresult. OK) {PS = PSDL. pagesettings; pddocument. defaultpagesettings = PSDL. pagesettings;} If (pt. showdialog () = dialogresult. OK) {pddocument. printersettings. copies = pt. printersettings. copies; pddocument. print ();} If (printpreview. showdialog () = dialogresult. OK) // call to print the pddocument. print (); * The print () method of the printdocument object executes the printpage event in the printcontroller class. */} Catch (invalidprinterexception ex) {MessageBox. show (ex. message, "simple Editor", messageboxbuttons. OK, messageboxicon. error); throw ;}/// <summary> /// 3. Obtain the print content. // each print task only calls onbeginprint () once. /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> void pddocument_beginprint (object sender, printeventargs e) {char [] Param = {'\ n'}; char [] trimparam = {' \ R'}; // press ENTER switch (streamtype) {Case "TXT": stringbuilder text = new stringbuilder (); system. io. streamreader = new streamreader (streamtoprint, encoding. default); While (streamreader. peek ()> = 0) {Li NES = streamreader. readtoend (). split (PARAM); For (INT I = 0; I <lines. length; I ++) {lines [I] = lines [I]. trimend (trimparam) ;}} break; Case "image": Image = system. drawing. image. fromstream (streamtoprint); break; default: break ;}} /// <summary> /// 4. Draw multiple printpage events of the printdocument /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> private void onprintpage (obje CT sender, printpageeventargs e) {int leftmargin = convert. toint32 (E. marginbounds. left) * 3/4); // The left margin is int topmargin = convert. toint32 (E. marginbounds. top * 2/3); // top margin switch (streamtype) {Case "TXT": While (linesprinted <lines. length) {// enter e. graphics. drawstring (lines [linesprinted ++], new font ("Arial", 10), brushes. black, leftmargin, topmargin, new stringformat (); topmargin + = 55 ;/ /The Row Height is 55, which can be adjusted. // If (topmargin> = E. pagebounds. Height-60) // The accumulated page height is greater than the page height. You can adjust it as needed {// if it is greater than the configured height E. hasmorepages = true;/** when the haemorepages attribute of the printpageeventargs class is true, the notification widget must use the onprintpage () method again to print a page. * Printloopi () has a sequence example for each page to be printed. If hasmorepages is false, printloop () stops. */Return ;}} break; Case "image": // specifies the image to be cut. Int width = image. width; int Height = image. height; If (width/e. marginbounds. width)> (height/e. marginbounds. height) {width = E. marginbounds. width; Height = image. height * E. marginbounds. width/image. width;} else {Height = E. marginbounds. height; width = image. width * E. marginbounds. height/image. height;} system. drawing. rectangle destrect = New system. drawing. rectangle (topmargin, leftmargin, width, height); // write an image to the canvas for (INT I = 0; I <convert. toint32 (math. floor (double) image. height/820) + 1; I ++) {e. graphics. drawimage (image, destrect, I * 820, I * 1170, image. width, image. height, system. drawing. graphicsunit. pixel); // if (I * 1170> = E. pagebounds. height-60) // The page height is greater than the page height. You can adjust it as needed {// if it is greater than the configured height E. hasmorepages = true;/** when the haemorepages attribute of the printpageeventargs class is true, the notification widget must use the onprintpage () method again to print a page. * Printloopi () has a sequence example for each page to be printed. If hasmorepages is false, printloop () stops. */Return ;}} break;} // after printing, draw a line and specify the printing date E. graphics. drawline (new pen (color. black), leftmargin, topmargin, E. marginbounds. right, topmargin); string strdatetime = datetime. now. tolongdatestring () + datetime. now. tolongtimestring (); E. graphics. drawstring (string. format ("Print time: {0}", strdatetime), mainfont, brushes. black, E. marginbounds. right-240, topmargin + 40, new stringformat (); linesprinted = 0; // after the painting is complete, turn off the multi-page printing function E. hasmorepages = false;} // <summary> // 5. endprint event, release resources /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> void pddocument_endprint (Object sender, printeventargs e) {// the variable lines occupies and references the string array, and now releases lines = NULL ;}// printtxt simple = new printtxt ("D: \ mainsoft \ 12.txt", "TXT ");

Related Article

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.