C # Printing detailed

Source: Internet
Author: User

Http://www.cnblogs.com/swjm119/archive/2012/03/09/2386142.html

The System.Drawing.Printing in the name space can help print the document file in the application.

The printing base is PrintDocument, method print () will activate a series of adjustments, and finally adjust the onprintpage (), the output results to the print machine.

PrintDocument: It's the most important thing. Almost all print types are related to this type. To print a document, you need a PrintDocument example.
Printcontroller: Control print a task flow. Provides events for printing to start, printing each page, and printing the end. The Printcontroller are derived from the Standardprintcontroller and previewprintcontroller of the human body.
PrintSettings: You can get the print and setup print machine configuration. This configuration is done by PageSetupDialog.
PrintDialog: It's OK to use which print machine to print and how to configure PrinterSettings.
Graphics: You can interview the print machine's design to send strings, lines, and curves to the print machine.
PageSetupDialog: Setup the print page.
Printpreviewdialg: Print the preview page.

which

PrintDialog the following diagram:

The Printsetupdialog page format is as follows:


PrintPreviewDialog the following diagram:


The application must adjust the PrintDocument print () method to activate the adjust sequence.    Because the PrintDocument itself is not responsible for the print stream, the print () method is used by Printcontroller to finish printing. The print controller is now in operation, notifying PrintDocument, and using OnBeginPrint () to start printing. If the application is to perform certain operations at the beginning of the print task, it is necessary to register an event handler in the PrintDocument to be notified in the application. As shown in the above, it is assumed that the handler OnBeginPrint () is registered, so this handler should be used in PrintDocument.

After the start stage is complete, Printcontroller enters Printloop () and adjusts the Onprintpage () method in PrintDocument for each page to be printed. Onprintpage () adjusts all the PrintPage event handlers. You have to perform this process on each page, or you won't print anything.
You can perform print codes in the Printdocument.printpage processor. Each print page has to be adjusted with the handler. If the print task only needs to perform a print code once, you must perform the BeginPrint and EndPrint events and handlers.
After the final page is printed, the Printcontroller adjusts the Onendprint () in PrintDocument. You can also perform the processing procedure that you want to apply here.

How to print (simple printing method).

To do this, the printing must have the following contents:
1, print file, used for printing machine. This is PrintDocument in the case of
2. Content to be printed. The PrintPage event method in Pintdocument is used to get the printed content.

The method is as follows:
1, the actual case of PrintDocument. Document that needs to be printed
2. Increase the Printdocument.printpage event, the method of this event is used to get the printed content
3, adjust the Printdocument.print () method to print content, before printing, this method will be prntcontroller to help the first call PrintPage event method
4, in the PrintPage event to draw the printed text, used to get print content, this method will return to the Printdocument.print () method, run the print

The codes are as follows:
Note: A multiple-line textbox:textboxedit in the interface
And a printed menu: Mifileprint
Using System.IO;
Using System.Drawing.Printing;

Namespace Simpleeditor
{
public partial class Simpleeditorform:form
{
/*
A printed menu in the window: Mifileprint
A multiline textboxg: called: textboxedit
*/

//1, Example of PrintDocument
PrintDocument pddocument = new PrintDocument ();

Public Simpleeditorform ()
{
InitializeComponent ();
//2,Subscribe to Printdocument.printpage Events
Pddocument.printpage + = new Printpageeventhandler (onprintpage);
}

<summary>
When you press print, this is the Print button event in the interface
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void OnFilePrint (object sender, EventArgs e)
{
Try
{
/*
* The PrintDocument Image's print () method performs PrintPage events with the help of Printcontroller.
*/

//3,Adjust the Printdocument.print () method
Pddocument.print ();

}
catch (Invalidprinterexception ex)
{
MessageBox.Show (ex. Message, "Simple Editor", MessageBoxButtons.OK, Messageboxicon.error);
Throw
}
}


<summary>
The PrintPage event of PrintDocument
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Onprintpage (object sender, PrintPageEventArgs e)
{
/*
* Get the string number of each line in the textbox
* Change the line
* \ r back to car
*/
char[] param ={' \ n '};
String[] lines = textBoxEdit.Text.Split (param);

int i = 0;
char[] Trimparam ={' \ R '};
foreach (string s in lines)
{
Delete \ r in each row
lines[i++] = S.trimend (Trimparam);
}

int x = 20;
int y = 20;
foreach (string line in lines)
{
/*
*4,The line of text is sent to the print machine, where e is a variation of the PrintPageEventArgs type that is connected to the print machine text.
* Print machine text can be written to the print machine.
* The position of the output results is defined with a change of X and Y.
*/
E.graphics.drawstring (line, New Font ("Arial"), Brushes.black, x, y);
Y + 15;
}
}
}
If you want to print multiple pages, use the hasmorepages of PrintPageEventArgs.

We have made the following changes to the previous codes:
Increase the PrintDocument BeginPrint and EndPrint events. The BeginPrint event is used to get the printed content. EndPrint is used for releasing resources. PrintDocument's PrintPage event is actually in the pages.
Kizhong: BeginPrint event method is called before the PrintPage event method.
The Pintpage event method is called before the Endprintpage event method.
The EndPrint event method is finally called, and the EndPrint event method is eventually returned to the Printdocument.print () method and is executed for printing.

the process is as follows:
1, the fact that the printing of the document file
2. Subscribe event (Subscribe to BeginPrint event, used to get the printed content; Pinrtpage event, used to draw each page content; EndPrint event, used for releasing resources)
3, adjust the method of BeginPrint event, get print content
4, adjust the method of Pinrtpage events, draw a number of printed on the page, and according to the sentence, set whether to do more than page printing
5. Adjust the method of EndPrint event, release the resource, and start printing after completion

The codes are as follows:


Using System.IO;
Using System.Drawing.Printing;

Namespace Simpleeditor
{
public partial class Simpleeditorform:form
{
private string filename = "Untitled";

//1,To actually print the text file
PrintDocument pddocument = new PrintDocument ();

Private string[] lines;
private int linesprinted;
Public Simpleeditorform ()
{
InitializeComponent ();

//2,Subscribe Events

Subscribe to the Pinrtpage event, used to draw each page content
Pddocument.printpage + = new Printpageeventhandler (onprintpage);
Subscribe to the BeginPrint event, which is used to get the printed content
Pddocument.beginprint + = new Printeventhandler (pddocument_beginprint);
Subscribe to EndPrint events for releasing resources
Pddocument.endprint + = new Printeventhandler (pddocument_endprint);
}

private void OnFilePrint (object sender, EventArgs e)
{
Try
{
Adjust printing
&n

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.