Recently, a charging system was completed, and the customer requested a complicated printing format. He wanted to use word as a template for printing, but then the customer changed the printing format, even wrod cannot do that format. Therefore, I tried to print the image as a template.
I think the printing principle is: generate an MDI file. When the system encounters an MDI file, it will automatically print the file. Therefore, no matter what template or method is used, you can generate an image to print the content in the printpage event processing! (If it is an incorrect understanding, please point it out !)
# Region Printing
Private void btnprint_click (Object sender, eventargs E)
{
// Print and preview
// Printpreviewdialog PPD = new printpreviewdialog ();
Printdocument Pd = new printdocument ();
// Set the margin
Margins margin = new margins (20, 20, 20, 20 );
PD. defaultpagesettings. Margins = margin;
/// Set the default value for the paper
// Papersize pagesize = new papersize ("first custom size", 800,600 );
// PD. defaultpagesettings. papersize = pagesize;
// Print event settings
PD. printpage + = new printpageeventhandler (this. pd_printpage );
// PPD. Document = Pd;
// PPD. showdialog ();
Try
{
PD. Print ();
}
Catch (exception ex)
{
MessageBox. Show (ex. message, "print error", messageboxbuttons. OK, messageboxicon. Error );
PD. printcontroller. onendprint (PD, new printeventargs ());
}
}
// Print event processing
Private void pd_printpage (Object sender, printpageeventargs E)
{
String date = lbldate. Text; // current date
String flowid = lblflowid. Text; // serial number
String paydate = paydate. year. tostring () + "year" + paydate. Month. tostring () + "month"; // month
String adminid = lbladminid. Text; // operator ID
String baseexpense = lblbaseexpense. Text; // basic fee
String fine = lblfine. Text; // number of fines
String upexpense = lblupexpense. Text; // last month
String actualexpense = txtactualexpense. Text; // actual fee
String chineseexpense = decimaltochinese. convertsum (actualexpense); // in upper case of the actual fee payable
// Read the image Template
Image temp = image. fromfile (@ "receipts.jpg ");
Getresultintoimage (ref temp, userid, flowid, date, baseexpense, fine, upexpense, actualexpense, chineseexpense, paydate, adminid );
Int x = E. marginbounds. X;
Int y = E. marginbounds. Y;
Int width = temp. width;
Int Height = temp. height;
Rectangle destrect = new rectangle (X, Y, width, height );
E. Graphics. drawimage (temp, destrect, 0, 0, temp. Width, temp. Height, system. Drawing. graphicsunit. pixel );
}
/// <Summary>
/// Fill the billing result in the image Template
/// </Summary>
Private void getresultintoimage (
Ref image temp,
String userid,
String flowid,
String currentdate,
String baseexpense,
String fine,
String upexpense,
String actualexpense,
String chineseexpense,
String paydate,
String adminname)
{
// Read the image Template
Graphics G = graphics. fromimage (temp );
Font F = new font ("", 12 );
Brush B = new solidbrush (color. Black );
// Fill in the data to the image template (the location must be measured when the image template is created)
G. drawimage (temp, 0, 0, temp. Width, temp. Height );
G. drawstring (User ID, F, B, 168,105 );
G. dragwstring (username, F, B, 166,134 );
G. drawstring (flowid, F, B, 535,105 );
G. drawstring (currentdate, F, B, 535,134 );
G. drawstring (baseexpense, F, B, and 219,202 );
G. drawstring (fine, F, B, 372,202 );
G. drawstring (upexpense, F, B, 486,202 );
G. drawstring (actualexpense, F, B, and 596,202 );
G. drawstring (chineseexpense, F, B, 196,238 );
G. drawstring (paydate, F, B, 176,269 );
G. drawstring (adminname, F, B, 497,298 );
G. Dispose ();
}
# Endregion