Implementing Printing in C # (use of printdialog,printdocument in C #)

Source: Internet
Author: User
Printing in C # using PrintDialog can be very convenient to achieve the printing function of the program.

The steps are as follows:

Create an instance of PrintDialog. As follows:
System.Windows.Forms.PrintDialog printdialog1=new PrintDialog ();
Create an instance of PrintDocument. as follows:
System.Drawing.Printing.PrintDocument Doctoprint =
New System.Drawing.Printing.PrintDocument ();
Sets the event handler function for the printer to begin printing. The function is as follows:
void Doctoprint_printpage (object sender,
System.Drawing.Printing.PrintPageEventArgs e)
Adds an event handler function to the PrintDocument PrintPage event.
Doctoprint.printpage+=new Printpageeventhandler (doctoprint_printpage);
Set the related properties for PrintDocument, such as:
Printdialog1.allowsomepages = true; Printdialog1.showhelp = true;
Set the PrintDialog document property to an instance of the PrintDocument configured above:
Printdialog1.document = Doctoprint;
Call the PrintDialog ShowDialog function to display the Print dialog box:
DialogResult result = Printdialog1.showdialog ();
Start printing according to the user's choice:
if (Result==dialogresult.ok)
{
Doctoprint.print ();
}

Examples are as follows:

When used, creates an instance of the Printservice class, and then calls the void Startprint (Stream streamtoprint,string streamtype) function to start printing. Where Streamtoprint is the content to be printed (byte stream), Streamtype is the type of flow (txt for ordinary text, image representation of images);




--------------------------------------------------------------------------------



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

Namespace Edimagesystem
{
<summary>
Summary description of the Printservice.
</summary>
public class Printservice
{
Public Printservice ()
{
//
TODO: Add constructor logic here
//
This.doctoprint.printpage+=new Printpageeventhandler (doctoprint_printpage);
}//Add an event handler function to the PrintDocument PrintPage

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 would 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 would
Like to print.
System.Windows.Forms.PrintDialog printdialog1=new PrintDialog ();//Create an instance of PrintDialog.
Printdialog1.allowsomepages = true;

Show the Help button.
Printdialog1.showhelp = true;

Set the Document to the PrintDocument for
which the PrintPage Event has been handled. To display the
dialog, either this property or the PrinterSettings property
Must be set
Printdialog1.document = doctoprint;//Sets the PrintDialog Document property to an instance of the configured PrintDocument above

DialogResult result = Printdialog1.showdialog ()//Call PrintDialog ShowDialog function Display Print dialog box

If The result is OK then print the document.
if (Result==dialogresult.ok)
{
Doctoprint.print ()//Start printing
}

}

The PrintDialog'll print the document
By handling the document ' s PrintPage event.
private void Doctoprint_printpage (object sender,
System.Drawing.Printing.PrintPageEventArgs e)/Set the event handler function for the printer to start printing
{

Insert code to render the page here.
This code would be called to be drawn.

The following code would 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", System.Drawing.FontStyle.Regular);

Draw the content.
System.IO.StreamReader 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.