How to use C # to achieve the full document printing function

Source: Internet
Author: User
Tags count implement
Print

Printing a document in a Windows application is a very important feature that has been a very complex work in the past, and the Microsoft. NET Framework
Printing features are provided in the form of components, providing a great convenience to programmers, but the use of these components is still very complex, it is necessary to explain.
Print operations typically include the following four features
1 print settings Set some of the printer's parameters such as changing the printer driver, etc.
2 page settings Set page size paper type etc
3 Print Preview is similar to print preview in Word
4 Printing

I'm going to explain the code for the printing function I wrote in Notepad (all source code can be downloaded in http://www.cndot.net). I hope I can give you some help.
The core of the implementation of the printing function is the PrintDocument class, which belongs to the System.Drawing.Printing namespace, which encapsulates the current print settings page settings and the
Some printing-related events and methods
This class includes several property events and methods
1, PrinterSettings Property
Store setup information for a printer this property does not require programmer setup because it is obtained by the Print dialog box
2, Printcountroller Property
Controlling the printing process
3, DefaultPageSettings Property
Store page settings information paper size direction and so do not need programmer settings because it is obtained by the Page Setup dialog box
4, DocumentName Property
Specify the document name, which appears in the Printer status window
1. BeginPrint Events
Issued before printing
2. The PrintPage incident
Each printed page is issued, and the event accepts a PrintPageEventArgs parameter that encapsulates printing-related information
The PrintPageEventArgs parameter has many important properties
1 Cancel Print
2 drawing objects for Graphics pages
3 HasMorePages If there is a page to print
Print method The method has no arguments to call it will start printing as it is currently set
If you implement the print function first constructs the PrintDocument object to add the printing event
PrintDocument PrintDocument;
private void InitializeComponent ()
{
...
Printdocument=new PrintDocument ();
Printdocument.printpage + = new Printpageeventhandler (this.printdocument_printpage);
...
}
Implementing Print Event Functionality
Printing and drawing are similar to the method of calling the graphics class to paint different is one on the monitor on the printed paper and printing to perform some complex calculations
such as line break page.
private void Printdocument_printpage (Object Sender,printpageeventargs e)
{
Graphics g = e.graphics; Get drawing objects
float linesperpage = 0; The line number of the page
float yposition = 0; Draw the portrait position of a string
int count = 0; Line 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; The current print font
SolidBrush MyBrush = new SolidBrush (color.black);//brushes
linesPerPage = E.marginbounds.height/printfont.getheight (g);//printable number of lines per page
Print one page at a line-by-row cycle
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, there are still unfinished pages. This triggers the next print event in the next print Linereader will
Automatically reads content that was not printed last time because Linereader is a member of a class outside of this print method, it can record the current read location
if (line!= null)
E.hasmorepages = true;
Else
E.hasmorepages = false;
}
Print settings, construct a Print dialog box to assign the document property set in the dialog box to PrintDocument this will automatically save the user's settings to the PrintDocument
In the PrinterSettings property of the
protected void Filemenuitem_printset_click (Object Sender,eventargs e)
{
PrintDialog PrintDialog = new PrintDialog ();
Printdialog.document = PrintDocument;
Printdialog.showdialog ();
}
Page Setup and print preview are the same as the print Setup principle of constructing a dialog box to save a user's settings in a dialog box to the properties 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);
}
}
Printing allows you to call the PrintDocument print () method directly because the user may have to change the print settings before printing, so
Display the Print Setup dialog box again here
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 summarizing the printing is
1 PrintPage method for constructing PrintDocument objects to add PrintDocument when the application form is initialized

2 Implement PrintPage method

3 Calling the PrintDocument Print method in the user's Click event to implement the printing function
In the middle of this you may want to use the PrintDialog PrintPreviewDialog PageSetupDialog settings and view the printing effect
These methods are usually triggered by the click of a menu.



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.