Implement the print function in C # (use of PrintDialog and PrintDocument in C)

Source: Internet
Author: User

Using PrintDialog in C # allows you to conveniently print programs.

The procedure is as follows:

  1. Create a PrintDialog instance. As follows:
    System. Windows. Forms. PrintDialog PrintDialog1 = new PrintDialog ();
  2. Create an instance of PrintDocument as follows:
    System. Drawing. Printing. PrintDocument docToPrint =
    New System. Drawing. Printing. PrintDocument ();
  3. Set the event processing function for the printer to start printing. The original function is as follows:
    Void docToPrint_PrintPage (object sender,
    System. Drawing. Printing. PrintPageEventArgs e)
  4. Add the event handler function to the PrintPage event of PrintDocument.
    DocToPrint. PrintPage + = new PrintPageEventHandler (docToPrint_PrintPage );
  5. Set attributes of PrintDocument, such:
    PrintDialog1.AllowSomePages = true; PrintDialog1.ShowHelp = true;
  6. Set the Document attribute of PrintDialog to the configured PrintDocument instance:
    PrintDialog1.Document = docToPrint;
  7. Call the ShowDialog function of PrintDialog to display the Print dialog box:
    DialogResult result = PrintDialog1.ShowDialog ();
  8. Start printing based on the user's choice:
    If (result = DialogResult. OK)
    {
    DocToPrint. Print ();
    }

Example:

Create a PrintService instance and call the void StartPrint (Stream streamToPrint, string streamType) function to start printing. StreamToPrint indicates the content to be printed (byte stream), streamType indicates the stream type (txt indicates normal text, and image indicates image );

 

Using System;
Using System. Drawing. Printing;
Using System. Windows. Forms;
Using System. IO;

Namespace EDImageSystem
{
/// <Summary>
/// Summary of PrintService.
/// </Summary>
Public class PrintService
{
Public PrintService ()
{
//
// TODO: add the constructor logic here
//
This.doc ToPrint. PrintPage + = new PrintPageEventHandler (docToPrint_PrintPage );
} // Add the event processing function to PrintPage of PrintDocument

// Declare the PrintDocument object.
Private System. Drawing. Printing. PrintDocument docToPrint =
New System. Drawing. Printing. PrintDocument (); // create an instance of PrintDocument

Private System. IO. Stream

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.