C # custom printing class, which can print text, lines, images, etc. The following is just an example:Code:
Public Class Printinfo { Public String Portname { Get ; Set ;} Public String Departuretime { Get ; Set ;} Public String Ampm { Get ; Set ;} Printdocument document = New Printdocument (); Private Int Pagewidth = 302 ; Private Int Pageheight = 530 ; Public Printinfo (){
Portname = "test port ";
Departuretime = "11: 30 ";
Ampm = "Morning"; document. beginprint + = New Printeventhandler (document_beginprint); document. printpage + = New Printpageeventhandler (document_printpage); document. endprint + = New Printeventhandler (document_endprint );} Private Void Document_endprint (Object Sender, printeventargs e ){} Private Void Document_beginprint ( Object Sender, printeventargs e ){} Private Void Document_printpage ( Object Sender, system. Drawing. Printing. printpageeventargs e ){ If (Departuretime! = Null ){ // Logo Bitmap imagelogo = New Bitmap (system. appdomain. currentdomain. basedirectory + @" \ Resource \ logo.bmp " ); E. Graphics. interpolationmode = Interpolationmode. highqualitybicubic; E. Graphics. smoothingmode = Smoothingmode. highquality; E. Graphics. compositingquality =Compositingquality. highquality; E. Graphics. drawimage (imagelogo, 5 , 10 , 292 , 131 ); // Line E. Graphics. drawline ( New Pen (brushes. Black, 3 ), 12 , 166 , 291 ,166 ); // Port name Font strfont = New Font ( " " , 18 ); E. Graphics. drawstring (portname, strfont, brushes. Black ,( Float ) Pagewidth/ 2 -E. Graphics. measurestring (portname, strfont). width/ 2 +1 , 198 ); // Departure time Strfont = New Font ( " " , 50 , Fontstyle. Bold); E. Graphics. drawstring (departuretime, strfont, brushes. Black ,( Float ) Pagewidth/ 2 -E. Graphics. measurestring (departuretime, strfont). width/ 2 , 248 ); // Ampm E. Graphics. drawstring (ampm, strfont, brushes. Black ,( Float ) Pagewidth/ 2 -E. Graphics. measurestring (ampm, strfont). width/ 2 , 320 ); // Line E. Graphics. drawline ( New Pen (brushes. Black, 3 ), 12 , 419 , 291 , 419 ); // Thankyou and print time Strfont = New Font ( " " , 13 , Fontstyle. Bold); printtxt = " Thank you " ; E. Graphics. drawstring (printtxt, strfont, brushes. Black, 12 , 481 ); Strfont = New Font ( " " , 8 ); Printtxt = Datetime. Now. tostring ( " Mm/DD/YYYY hh: MT \ m " ); E. Graphics. drawstring (printtxt, strfont, brushes. Black, 184 , 487 );}} Public Void Printdocument () {document. defaultpagesettings. papersize = New Papersize ( " Custum " , Pagewidth, pageheight); printcontroller = New Standardprintcontroller (); document. printcontroller = Printcontroller; // System. Windows. Forms. printpreviewdialog printpreview = new system. Windows. Forms. printpreviewdialog (); // Printpreview. Document = document; // Printpreview. showdialog (); Document. Print ();}