How to use C # To print complete documents

Source: Internet
Author: User

In windows applications, document printing is a very important function. It has been a very complex task in the past.
The printing function is provided in the form of components, which provides great convenience for programmers. However, the use of these components is still very complicated. It is necessary to explain it.
Print operations generally include the following four functions
1. Print and set some printer parameters, such as changing the printer driver.
2. Set page size and paper type on the page
3. print preview is similar to printing preview in word.
4 print

Below I will write my notepad (all source code can be downloaded in the http://www.cndot.net) in the print function of the Code to explain the hope to give you some help
The core of Printing is the PrintDocument class, which belongs to the System. Drawing. Printing namespace class. This class encapsulates the current Printing settings page settings and
Some printing-related events and Methods
This class includes the following attribute events and Methods
1. PrinterSettings Properties
This attribute does not need to be set by the programmer because it is obtained by the Print dialog box.
2. PrintCountroller attributes
Control the printing process
3. DefaultPageSettings attributes
You do not need to set the size and direction of the printed paper because it is obtained by the page Setting dialog box.
4. DocumentName attributes
The specified document name appears in the printer status window.
1. BeginPrint event
Issue before printing
2. PrintPage event
Each printed page is sent, and the event receives a PrintPageEventArgs parameter. This parameter encapsulates printed information.
The PrintPageEventArgs parameter has many important attributes.
1 Cancel printing
2 Graphics page Drawing Object
3. Does HasMorePages have pages to print?
Print method this method does not have a parameter call it will start printing according to the current settings
If the print function is implemented, the PrintDocument object is constructed first to add a print event.
PrintDocument printDocument;
Private void InitializeComponent ()
{
...
PrintDocument = new PrintDocument ();
PrintDocument. PrintPage + = new PrintPageEventHandler (this. printDocument_PrintPage );
...
}
Event Printing
Printing and plotting are similar to calling Graphics methods for drawing. Different from drawing on a display, printing on a printing paper requires some complex calculations.
Such as line feed pages.
Private void printDocument_PrintPage (object sender, PrintPageEventArgs e)
{
Graphics g = e. Graphics; // obtain the Drawing Object
Float linesPerPage = 0; // The row number of the page
Float yPosition = 0; // draw the vertical position of the string
Int count = 0; // The row counter.
Float leftMargin = e. MarginBounds. Left; // Left margin
Float topMargin = e. MarginBounds. Top; // Top margin
String line = null; line string
Font printFont = this. textBox. Font; // current print Font
SolidBrush myBrush = new SolidBrush (Color. Black); // brush
LinesPerPage = e. MarginBounds. Height/printFont. GetHeight (g); // Number of printable lines per page
// Print one page in a row-by-row Loop
While (count <linesPerPage & (line = lineReader. ReadLine ())! = Null ))
{
YPosition = topMargin + (count * printFont. GetHeight (g ));
G. DrawString (line, printFont, myBrush, leftMargin, yPosition, new StringFormat ());
Count ++;
}
If the page is printed and the line is not empty, the page is still incomplete. This will trigger the next printing event. In the next printing, lineReader will
Automatically reads the last printed content because lineReader is a member of the class outside the printing method and can record the current read location.
If (line! = Null)
E. HasMorePages = true;
Else
E. HasMorePages = false;
}
Print settings. In the print structure dialog box, the Document attribute set in the dialog box is assigned to printDocument. This will automatically save your settings to printDocument.
In the PrinterSettings attribute
Protected void FileMenuItem_PrintSet_Click (object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog ();
PrintDialog. Document = printDocument;
PrintDialog. ShowDialog ();
}
The principle of page setting and print preview is the same as that of print setting. The create dialog box saves the settings in the dialog box to the attributes of the corresponding class.
Protected void FileMenuItem_PageSet_Click (object sender, EventArgs e)
{
PageSetupDialog pageSetupDialog = new PageSetupDialog ();
PageSetupDialog. Document = printDocument;
PageSetupDialog. ShowDialog ();
}
Print preview
Protected void FileMenuItem_PrintView_Click (object sender, EventArgs e)
{
PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog ();
PrintPreviewDialog. Document = printDocument;
LineReader = new StringReader (textBox. Text );
Try
{
PrintPreviewDialog. ShowDialog ();
}
Catch (Exception excep)
{
MessageBox. Show (excep. Message, "print Error", MessageBoxButtons. OK, MessageBoxIcon. Error );
}
}
Print, you can directly call the Print () method of printDocument because you may need to change the Print settings before printing.
The print Settings dialog box is displayed again.
Protected void FileMenuItem_Print_Click (object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog ();
PrintDialog. Document = printDocument;
LineReader = new StringReader (textBox. Text );
If (printDialog. ShowDialog () = DialogResult. OK)
{
Try
{
PrintDocument. Print ();
}
Catch (Exception excep)
{
MessageBox. Show (excep. Message, "print Error", MessageBoxButtons. OK, MessageBoxIcon. Error );
PrintDocument. PrintController. OnEndPrint (printDocument, new PrintEventArgs ());
}
}
}
The process of summary printing is
1. Construct the PrintPage Method for adding PrintDocument to the printDocument object during application form Initialization
2 implement PrintPage Method 4 call the Print method of printDocument in the user's Click Event to implement the printing function
PrintDialog PrintPreviewDialog PageSetupDialog may be used to set and view the print effect.
These methods are usually triggered by a menu click.

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.