Make a print in winform (no textbox)

Source: Internet
Author: User

Recently, a winform printing function is provided. I don't know much about this because I made it for the first time. So I found a lot of information online. Finally, sort out the source code as follows:

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. Drawing. printing;

Namespace windowsapplication1
{
Public partial class daochu: Form
{
Public daochu ()
{
Initializecomponent ();
}
Printdocument;
Private void daochu_load (Object sender, eventargs E)
{
Printdocuments ();

}

Private void printdocuments () // encapsulate the event
{
Printdocument = new printdocument ();
Printdocument. printpage + = new printpageeventhandler (printdocument?printpage); // specify the printdocument_printprintpage event Delegate for printing the page.

}

Int iwidth = 800;
Int iheight = 600;
Int left = 150;
Int right = 150;
Int Top = 100;
Int bot = 100;
Graphics g;
Brush B;
Font titlefont;
Font fnttxt;
System. Drawing. Printing. printpageeventargs X;
Private void printdocumentemediprintpage (Object sender, system. Drawing. Printing. printpageeventargs E)
{
Printstyle (E );
}

Private void printstyle (system. Drawing. Printing. printpageeventargs e) // method encapsulation of the Print Style
{
This. printdocument1.defaultpagesettings. papersize = new
System. Drawing. Printing. papersize ("newprint", iwidth, iheight); // you can specify the size of the printed paper.
Titlefont = new font ("", 18, fontstyle. Bold); // title Font
Fnttxt = new font ("", 16, fontstyle. Regular); // text
Margins margins = new margins (left, right, top, bottom); // you can specify the upper, lower, and lower margins.
Printdocument. defaultpagesettings. Margins = margins;
X = E;
Float leftmargin = E. marginbounds. Left; // left margin
Float topmargin = E. marginbounds. Top; // top margin
Brush = new solidbrush (color. Black); // paint brush
// Pen = new pen (color. Black); // line color
G = E. graphics;
B = new solidbrush (color. Black );
Point po = new point (10, 10 );
Try
{
E. Graphics. drawstring (getprintsw (). tostring (), fnttxt, brush, po); // print in drawstring mode.
}
Catch (exception ex)
{
MessageBox. Show (this, "print error! "," Prompt ", messageboxbuttons. OK, messageboxicon. information );
}
}

/// The getprintsw method is used to construct the printed text. The internal stringbuilder. appendline occupies a single row in drawstring.


// Public stringbuilder getprintsw (string name, string idcard, int num) // used for external input values

Public stringbuilder getprintsw () // internally defines the value directly.
{
Stringbuilder sb = new stringbuilder ();
String adrname = txtnames. Text. Trim ();
If (adrname = "")
{
Adrname = "People's Republic of China ";
}
String title = "Greater China" + adrname + "relocation commercial housing ";

String titles = "Order Number lottery qualification confirmation form ";

String idcard = "622825198910223315"; // ID card

String name = "";

String names = "";
If (name. Length = 2)
{
Names = "\ 0 \ 0" + name + "\ 0 \ 0 ";
}
Else if (name. Length = 3)
{
Names = "\ 0" + name + "\ 0 ";
}
Else if (name. Length = 4)
{
Names = Name;
}
Else
{
Names = "\ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 ";
}
String num = "25"; // number

String Item1 = "\ 0 \ 0 \ 0 \ 0 approved by the Unit, the lottery \ 0" + names + "\ 0 is eligible for the" Chinese people ";

String item2 = "The State Council and China Mobile Migration supporting commercial housing lottery announcement" to participate in the serial number lottery activities. ";

String item3 = "Ask the notary office to go through the serial number computer lottery for notarization according to the drawing announcement. ";

String item4 = "Remarks: \ 0 credential number: \ 0" + idcard + "\ 0 draw Order Number: \ 0" + num;

String item5 = "\ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 "+ adrname +" Citizen Committee ";
// String item6 = "(the lottery holder saves the time and submits it to the relevant organization for contract signing )";

/* Horizontal text control, followed by X. pagebounds. Width-G. measurestring (title, titlefont). width)/2 control, 20 control up and down
G. drawstring ("\ r \ n" + title, titlefont, B, new pointf (X. pagebounds. width-G. measurestring (title, titlefont ). width)/2, 20); // Title

G. drawstring ("\ r \ n" + Item1 + "\ r \ n \ r ", fnttxt, B, new pointf (X. pagebounds. width-G. measurestring (title, titlefont ). width)/4, 50 ));
*
G. drawstring ("\ r \ n" + item2 + "\ r \ n \ r \ \ n \ r ", fnttxt, B, new pointf (X. pagebounds. width-G. measurestring (title, titlefont ). width)/4,120 ));

G. drawstring ("\ r \ n" + item3 + "\ r \ n \ r", fnttxt, b, new pointf (X. pagebounds. width-G. measurestring (title, titlefont ). width)/4,190 ));
*
G. drawstring ("\ r \ n" + item4 + "\ r \ n \ r ", fnttxt, B, new pointf (X. pagebounds. width-G. measurestring (title, titlefont ). width)/2,220 ));
*
G. drawstring ("\ r \ n \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 Date: "+ datetime. now. toshortdatestring (), fnttxt, B, new pointf (X. pagebounds. width-G. measurestring (title, titlefont ). width)/2,250 ));
G. drawstring ("\ r \ n" + item5 + "\ r \ n \ r ", fnttxt, B, new pointf (X. pagebounds. width-G. measurestring (title, titlefont ). width)/3,300 ));
*/

For (Int J = 1; j <= 2; j ++)
{
Int m = 0;
If (j = 1)
{
M = 0;
}
Else
{
M = 580;
}
G. drawstring ("\ r \ n \ r" + title, titlefont, B, new pointf (X. pagebounds. width-G. measurestring (title, titlefont ). width)/2, 10 + M); // Title 1
G. drawstring ("\ r \ n \ r \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 "+ titles, titlefont, B, new pointf (X. pagebounds. width-G. measurestring (title, titlefont ). width)/3, 45 + M); // Title 2
G. drawstring ("\ r \ n \ r \ 0" + Item1 + "\ r \ n \ r ", fnttxt, B, new pointf (X. pagebounds. width-G. measurestring (title, titlefont ). width)/6, 50 + M ));
G. drawstring ("\ r \ n \ r \ 0" + item2 + "\ r \ n \ r \ n \ r ", fnttxt, B, new pointf (X. pagebounds. width-G. measurestring (title, titlefont ). width)/6,120 + M ));
G. drawstring ("\ r \ n \ r \ 0" + item3 + "\ r \ n \ r \ n \ r ", fnttxt, B, new pointf (X. pagebounds. width-G. measurestring (title, titlefont ). width)/6,170 + M ));
G. drawstring ("\ r \ n \ r \ 0" + item4 + "\ r \ n \ r", fnttxt, b, new pointf (X. pagebounds. width-G. measurestring (title, titlefont ). width)/6,240 + M ));
G. drawstring ("\ r \ n \ r \ 0 \ 0" + item5 + "\ r \ n \ r ", fnttxt, B, new pointf (X. pagebounds. width-G. measurestring (title, titlefont ). width)/4,290 + M ));
G. drawstring ("\ r \ n \ r \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 Date: "+ datetime. now. toshortdatestring (), fnttxt, B, new pointf (X. pagebounds. width-G. measurestring (title, titlefont ). width)/2,300 + M ));
// G. drawstring ("\ r \ n \ r \ 0" + item6 + "\ r \ n \ r ", fnttxt, B, new pointf (X. pagebounds. width-G. measurestring (title, titlefont ). width)/6,310 + M ));

}

/* The following is the second printing method:
SB. appendline ("\ r \ n \ r"); // "\ n" indicates a linefeed, and "\ r" indicates a carriage return.
SB. appendline ("\ r \ n \ r ");
SB. appendline ("\ r \ n \ r ");
SB. appendline ("\ r \ n ");
// Title, mainly to control the Font Style
G. drawstring ("\ r \ n" + title, titlefont, B, new pointf (X. pagebounds. width-G. measurestring (title, titlefont ). width)/2, 15 ));
// Sb. appendline ("\ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 "+ title +" \ r \ n \ r ");
SB. appendline ("\ 0 \ 0 \ 0 \ 0 \ 0" + Item1 + "\ r \ n \ r ");
SB. appendline ("\ 0 \ 0 \ 0 \ 0" + item2 + "\ r \ n \ r ");
SB. appendline ("\ 0 \ 0 \ 0 \ 0" + item3 + "\ r \ n ");
SB. appendline ("\ r \ n \ r ");
// Sb. appendline ("\ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 "+ item4 +" \ r \ n \ r ");
G. drawstring ("\ r \ n \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 "+ item4 +" \ r \ n \ r ", fnttxt, B, new pointf (X. pagebounds. width-G. measurestring (title, titlefont ). width)/4,240 ));
SB. appendline ("\ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 Date: "+ datetime. now. toshortdatestring ());
SB. appendline ("\ r ");
SB. appendline ("\ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \ 0" + item5 );
// Sb. appendline ("-----------------------------------------");
*/
Return Sb;
}

Private void btntxt_click (Object sender, eventargs e) // print
{
Printing ();
}

Private void printing () // Method for Printing
{
Printdialog = new printdialog ();
Printdialog. Document = printdocument;
If (printdialog. showdialog () = dialogresult. OK)
{
Try
{
Printdocument. Print ();
}
Catch (exception excep)
{
MessageBox. Show (excep. message, "print error", messageboxbuttons. OK, messageboxicon. Error );
Printdocument. printcontroller. onendprint (printdocument, new printeventargs ());
}
}
}

Private void show_click (Object sender, eventargs e) // print preview
{
Printpreview ();
}

Private void printpreview () // print the preview Method
{
Pagesetupdialog PSD = new pagesetupdialog ();
PSD. Document = printdocument;
Printpreviewdialog = new printpreviewdialog ();
Printpreviewdialog. Document = printdocument;

/* Horizontal print preview settings PS: vertical by default
Printdocument. printersettings. printtofile = false;
Printdocument. defaultpagesettings. Landscape = true;
Printdocument. printersettings. defaultpagesettings. Landscape = true;
Printdocument. defaultpagesettings. printersettings. defaultpagesettings. Landscape = true;
Printdialog pdialog = new printdialog ();
Pdialog. Document = printdocument;
Pdialog. printersettings. defaultpagesettings. Landscape = true;
Pdialog. printersettings. printtofile = false;
Pdialog. Document. defaultpagesettings. Landscape = true;
*/

Try
{
Printpreviewdialog. showdialog ();
}
Catch (exception excep)
{
MessageBox. Show (excep. message, "preview error", messageboxbuttons. OK, messageboxicon. Error );
}
}
}
}

Because of the time, many codes do not know the specific meaning. However, this printing function is always implemented. I am posting code here today, hoping that the passing experts will give you some advice on the lack of code. Thank you ~~~

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.