C # complete document printing

Source: Internet
Author: User
In windows applications, document printing is a very important feature. It has been a very complex task for the past, Microsoft. the printing function of net Framework is provided by 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
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 all print-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 time a page is printed, the event receives a PrintPageEventArgs parameter. This parameter encapsulates printed information.
The PrintPageEventArgs parameter has many important attributes.
1 Cancel printing 2 the drawing object on the Graphics page 3 HasMorePages whether there is a page to be printed
Print method this method does not have a parameter call it will start printing according to the current settings if the printing function is implemented first construct the PrintDocument object to add the printing event PrintDocument printDocument;
Private void InitializeComponent ()
{

PrintDocument = new PrintDocument ();
PrintDocument. PrintPage + = new PrintPageEventHandler (this. printDocument_PrintPage );

}

Event Printing
Similar to plotting, printing calls the Graphics class method to draw a picture. Different from drawing a picture on a display, it is on the printing paper, and some complex calculations are required for printing, 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; // a 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 link is still unfinished. This triggers the next printing event. In the next printing, the lineReader automatically reads the content that was not printed last time. is a member of the class outside the printing method. It can record the current read location.
*/
If (line! = Null)
E. HasMorePages = true;
Else
E. HasMorePages = false;
}

Print settings: In the create Print dialog box, assign the Document attribute set in the dialog box to printDocument. This will automatically save your settings to the protected void FileMenuItem_PrintSet_Click (object sender, EventArgs e) attribute in the PrinterSettings attribute of printDocument)
{
PrintDialog printDialog = new PrintDialog ();
PrintDialog. Document = printDocument;
PrintDialog. ShowDialog ();
}

The page setting and print preview work the same way as the 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 Setting Dialog Box protected void FileMenuItem_Print_Click (object sender, EventArgs e) is displayed here)
{
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 ());
}
}
}

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.