Implement the print function in C # (use of printdialog and printdocument in C)

Source: Internet
Author: User

Implement the print function in C # (use of printdialog and printdocument in C)

 

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 streamtoprint;
String streamtype;

// This method will set properties on the printdialog object and
// Then display the dialog.
Public void startprint (Stream streamtoprint, string streamtype)
{

This. streamtoprint = streamtoprint;
This. streamtype = streamtype;
// Allow the user to choose the page range he or she wowould
// Like to print.
System. Windows. Forms. printdialog printdialog1 = new printdialog (); // create a printdialog instance.
Printdialog1.allowsomepages = true;

// Show the Help button.
Printdialog1.showhelp = true;

// Set the document property to the printdocument
// Which the printpage event has been handled. to display
// Dialog, either this property or the printersettings Property
// Must be set
Printdialog1.document = doctoprint; // set the document attribute of printdialog to the instance of the configured printdocument.

Dialogresult result = printdialog1.showdialog (); // call the showdialog function of printdialog to display the Print dialog box.

// If the result is OK then print the document.
If (result = dialogresult. OK)
{
Doctoprint. Print (); // start printing
}

}

// The printdialog will print the document
// By handling the document's printpage event.
Private void doctoprint_printpage (Object sender,
System. Drawing. Printing. printpageeventargs e) // sets the event processing function for the printer to start printing.
{

// Insert code to render the page here.
// This code will be called when the control is drawn.

// The following code will render a simple
// Message on the printed document
Switch (this. streamtype)
{
Case "TXT ":
String text = NULL;
System. Drawing. Font printfont = new system. Drawing. Font
("Arial", 35, system. Drawing. fontstyle. Regular );

// Draw the content.
System. Io. streamreader = new streamreader (this. streamtoprint );
TEXT = streamreader. readtoend ();
E. Graphics. drawstring (text, printfont, system. Drawing. Brushes. Black, E. marginbounds. X, E. marginbounds. y );
Break;
Case "image ":
System. Drawing. Image image = system. Drawing. image. fromstream (this. streamtoprint );
Int x = E. marginbounds. X;
Int y = E. marginbounds. Y;
Int width = image. width;
Int Height = image. height;
If (width/E. marginbounds. width)> (height/E. marginbounds. Height ))
{
Width = E. marginbounds. width;
Height = image. Height * E. marginbounds. width/image. width;
}
Else
{
Height = E. marginbounds. height;
Width = image. Width * E. marginbounds. Height/image. height;
}
System. Drawing. Rectangle destrect = new system. Drawing. rectangle (X, Y, width, height );
E. Graphics. drawimage (image, destrect, 0, 0, image. Width, image. Height, system. Drawing. graphicsunit. pixel );
Break;
Default:
Break;
}

}

}
}

 

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.