Basic usage of. NET print controls

Source: Internet
Author: User

1. Add the following controls to the WinForm

2. The code and usage are as follows:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespacewinform{/*Printing in Windows forms consists of the following two areas: using the PrintDocument Component (Windows Forms) component to enable users to print; using PrintPreviewDialog controls (Windows Forms) controls, Pri The Ntdialog components (Windows Forms) component and the PageSetupDialog component (Windows Forms) component provide a familiar graphical interface to users who understand the Windows operating system * PrintDocument usage: 1, create; 2, in PR Intpage The print logic for each page in the event, 3. Call the Print method * PrintPreviewControl usage: 1, create, 2, associate the document attribute with PrintDocument; 3, Write the printing logic in the PrintPage event of PrintDocument, 4, the PrintPage event is automatically called after running the program, 5, if you want to refresh, use the Invalidatepreview function*/     Public Partial classUsageofcontrol6:form {stringdocumentcontents;//Total Print characters        stringStringtoprint;///that you want to print         PublicUsageOfControl6 () {InitializeComponent (); ///consider that the PrintPreviewControl component invokes the PrintDocument PrintPage event at initialization, where the printed data is initialized            using(FileStream stream =NewFileStream (@"D:\redist.txt", FileMode.Open)) using(StreamReader reader =NewStreamReader (Stream)) {documentcontents=Reader.            ReadToEnd (); } stringtoprint=documentcontents; }        Private voidUsageofcontrol6_load (Objectsender, EventArgs e) {        }        Private voidButton1_Click (Objectsender, EventArgs e) {printpreviewdialog1.document= PrintDocument1;//Print Preview ControlPrintdialog1.document = PrintDocument1;//Print Settings ControlPagesetupdialog1.document=printdocument1;//Page Setup ControlsPrintpreviewcontrol1.document =PrintDocument1; using(FileStream stream =NewFileStream (@"D:\redist.txt", FileMode.Open)) using(StreamReader reader =NewStreamReader (Stream)) {documentcontents=Reader.            ReadToEnd (); } stringtoprint=documentcontents; if(Printdialog1.showdialog () ==dialogresult.ok)///set printdocument1 properties by PrintDialog1: Printer, print page, print direction{pagesetupdialog1.showdialog ();///printer, margin, header, paper size, can also get e.pagesettings in the Printdocument1_printpage event to set                 This. Printpreviewdialog1.showdialog ();///Print PreviewPrintdocument1.print (); Printpreviewcontrol1.invalidatepreview ();////Refresh, the PrintDocument PrintPage event is called again            }        }        Private voidPrintdocument1_printpage (Objectsender, System.Drawing.Printing.PrintPageEventArgs e) {            intCharactersonpage =0; intlinesPerPage =0; //sets the value of Charactersonpage to the number of characters//of Stringtoprint that would fit within the bounds of the page. //figure out the number of lines and characters to print when the stringtoprint is covered with printed pagesE.graphics.measurestring (Stringtoprint, This. Font, E.marginbounds.size, Stringformat.generictypographic, outCharactersonpage, outlinesperpage); //draws the string within the bounds of the page.///Print text on this page            ///In addition to printing text can also print lines, graphics, screens, etc., can be in the position of the fixedE.graphics.drawstring (Stringtoprint, This.            Font, Brushes.black, E.marginbounds, stringformat.generictypographic); //Remove the portion of the string that has been printed.Stringtoprint = stringtoprint.substring (charactersonpage);//next page to hit.//Check to see if more pages is printed.E.hasmorepages = (Stringtoprint.length >0);///If you print the next page without printing, this event is automatically called to print the next page            //If There is no more pages, the reset the string is printed.            if(!e.hasmorepages) Stringtoprint=documentcontents; }        Private voidButton2_Click (Objectsender, EventArgs e) {Printpreviewcontrol1.startpage+=1;///Next Page        }        Private voidButton3_Click (Objectsender, EventArgs e) {Printpreviewcontrol1.startpage-=1;///Previous Page        }        Private voidButton4_Click (Objectsender, EventArgs e) {Printpreviewcontrol1.zoom+=0.1;///Zoom in        }        Private voidButton5_click (Objectsender, EventArgs e) {Printpreviewcontrol1.zoom-=0.1;//Zoom Out        }    }}
View Code

Basic usage of. NET print controls

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.