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: