. Net (C #) print-print the final structure

Source: Internet
Author: User

System. Drawing. printing;Classes in The namespace can be used for application.Program.
The printed category isPrintdocument, MethodPrint ()Will trigger a series of trial use, and finally useOnprintpage (),Send the response to the printer.

Print structure:
The following section lists the main parts of the print structure.

Description:
Printdocument : Is the most important type. Almost all print categories are related to this category. To print a text example, you must Printdocument .
Printcontroller: Type Control worker prints the task flow. It provides the ability to print start, print each plane, and print end events. Derived from Printcontroller Has Standardprintcontroller And Previewprintcontroller .
Printsettings : Can be used to print and set the printer configuration. This configuration is composed Pagesetupdialog Class.
Printdialog : Determines which printer to use for row printing and how to configure Printersettings .
Graphics : You can check the settings of the printer and send strings, operating conditions, and music to the printer.
Pagesetupdialog : Set the print plane.
Printpreviewdialg: print the zookeeper plane.

Where:

The printdialog faces are shown as follows:

 

The formats of printsetupdialog faces are as follows:

 

The layout of printpreviewdialog is as follows:

 

Print process:
For example:

Application must be used again Printdocument Of Print () Method, which is used as a sequence for sequence motion. Because Printdocument It does not parse into the print stream, so Printcontroller By using this class Print () Method to print. Print the current operation of the controller, notification Printdocument , Used by the terminal Onbeginprint () Start printing. If the application performs some operations at the beginning of a printing task Printdocument Only when an event processing program is executed in the application program class is notified. In the preceding example, we assume that the Handler has been added. Onbeginprint (), So this processing program should be Printdocument Class.
  After the start segment is complete, Printcontroller Just enter Printloop (), For each plane to be printed Printdocument Class Onprintpage () Method. Onprintpage () Using all Printpage Event handling program. This processing program must be performed on each region. Otherwise, no content will be printed.
You can Printdocument. printpage In the processing program, lines are printed on behalf of workers. Each print plane must use the processing program. If you only need to print the generation line at one time, you must print the line. Beginprint And Endprint Events and handling procedures.
  After the last layout is printed, Printcontroller Ready for use Printdocument Class Onendprint () . You can also configure the processing program to be used here.

How can I print a ticket )?

To implement the print operation, the following content must be available:
1Print the file, used by the printer. UsePrintdocumentExamples
2And printed content. HandlingPintdocumentMediumPrintpageEvent method to get printed content.

The method is as follows:
1. Implementation Printdocument Class. Description of the text to be printed
2. Add Printdocument. printpage Event. The method of this event is used to obtain the printed content.
3. Failover Printdocument. Print () Method To print content. before printing, this method will Prntcontroller Class help first call Printpage Event Method
4. Printpage The printed content is used to obtain the printed content. After this method is completed, it will return Printdocument. Print () Method, the rows are printed

The token generation is as follows:
Note: a multilineTextbox: Textboxedit
And a printed dish:Mifileprint

Using System. IO;
Using System. Drawing. printing;

Namespace Simpleeditor
{
Public   Partial   Class Simpleeditorform: Form
{
/*
A printed menu in the window: mifileprint
Textboxg: Name: textboxedit
*/

// 1, Practical printdocument type
Printdocument pddocument =   New Printdocument ();

Public Simpleeditorform ()
{
Initializecomponent ();
// 2,Printdocument. printpage event
Pddocument. printpage + =   New Printpageeventhandler (onprintpage );
}


/// <Summary>
/// When you press the print button, this indicates 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 print () method of printdocument objects is used to parse the printpage event with the help of printcontroller.
*/

//3, Using the printdocument. Print () method
Pddocument. Print ();

}
Catch (Invalidprinterexception ex)
{
MessageBox. Show (ex. Message, " Simple Editor " , Messageboxbuttons. OK, messageboxicon. Error );
Throw ;
}
}

///   <Summary>
/// Printpage event of printdocument
///   </Summary>
///   <Param name = "sender"> </param>
///   <Param name = "E"> </param>
Private   Void Onprintpage ( Object Sender, printpageeventargs E)
{
/*
* Obtain the string number of each row in the textbox.
* \ N rows
* \ R car return
*/
Char [] Param = { ' \ N ' };
String [] Lines = Textboxedit. Text. Split (PARAM );

Int I =   0 ;
Char [] Trimparam = { ' \ R ' };
Foreach ( String S In Lines)
{
// Except \ r in each row
Lines [I ++ ] = S. trimend (trimparam );
}

Int X =   20 ;
Int Y =   20 ;
Foreach ( String Line In Lines)
{
/*
*4,Send text lines to the printer. E is a variation of the printpageeventargs type, which is directly sent to the printer-related text.
* Print the host information text to the printer settings.
* The Position of the output result is changed to X and Y.
*/
E. Graphics. drawstring (line, New Font ( " Arial " , 10 ), Brushes. Black, x, y );
Y + =   15 ;
}
}
}
}

In this way, the printing function of the ticket is learned.

Although we have implemented the playing function, but you will see during the printing, when the printing content exceeds a limit, how can we implement multiple printing operations?
Please refer to the following link:. Net (C #) printing-multi-region Printing

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.