C # print

Source: Internet
Author: User
In a recent new project, a requirement is to print out the data. After investigation, a class is written.
The main application is system. Drawing.
Pint is the result to be printed as the main method parameter. Public void print (cmdcardprintdata argdcarddata)
{
Thread. currentthread. currentculture =
Cultureinfo. createspecificculture ("En-us ");

Printdata = argdcarddata;

Bool istobitmap = Boolean. parse (reportsettings ["print. tobitmap"]);
String bitmapsavedir = reportsettings ["print. bitmapsavedir"];

String printername = reportsettings ["print. printername"];

Mcode39format = reportsettings ["print. code39.format"];

Leftmargin = int32.parse (reportsettings ["print. leftmargin"]);
Topmargin = int32.parse (reportsettings ["print. topmargin"]);

If (istobitmap)
{
For Debug. Print to bitmap image. # region for Debug. Print to bitmap image.
// For Debug. Print to bitmap image.
Bitmap bitmap = new Bitmap (1024,768 );
Graphics G = graphics. fromimage (Bitmap );
G. fillrectangle (brushes. White, 0, 0, and 1024,768 );

Printdata (G, leftmargin, topmargin );

String Path = string. Format (bitmapsavedir, cmstringutils. getsysdatestring ());

Cmfileutils. ensuredirectory (PATH );

Bitmap. Save (PATH );
# Endregion
}
Else
{
Print to printer # region print to printer
// Print to printer
Printdocument Pd = new printdocument ();

If (! Cmstringutils. isempty (printername ))
{
PD. printersettings. printername = printername;
}

Margins margins = new margins (leftmargin, leftmargin, topmargin, topmargin );
PD. defaultpagesettings. Margins = margins;


PD. defaultpagesettings. Landscape = true;

PD. printpage + = new printpageeventhandler
(This. pd_printpage );

Try
{
// Print to printer.
PD. Print ();
}
Catch (exception ex)
{
// If any error occurred.
Cmlogger. Log (Ex );
Throw new cmlogicexception ("se00027", ex );
}
# Endregion
}

}

Private void pd_printpage (Object sender, printpageeventargs eV)
{

Printdata (EV. Graphics, Ev. marginbounds. Left, Ev. marginbounds. Top );

Ev. hasmorepages = false;
}

Private void drawtext (Graphics g, string curfield, string text)
{
G. translatetransform (leftmargin + fieldinfos [curfield]. Left, topmargin + fieldinfos [curfield]. Top );
G. scaletransform (1.0f, fieldinfos [curfield]. verticalratio );

G. drawstring (text,
Fieldinfos [curfield]. Font, brushes. Black,
0, 0 );

// Reset Matrix
G. resettransform ();
}

Printed result diagram:

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.