Asp.net crystal report printing instance code
// Try two ways to print the report table. I placed a crystalreportviewer control on form1, named it cryview, and used its built-in "print button" to print the report.
// You have also set a crystalreportdocument named cryreport1, and a button named "start printing" on form1, use it to directly call report printing.
// Initialize cryview, cryreport1, and reportds in the load of form1.
Private void form1_load (object sender, eventargs e)
{
// Initialize reportds before adding some dtpack data to it.
Datarow reprow1 = reportds. tables ["dtpack"]. newrow ();
Reprow1 ["pname"] = "basic package ";
Reprow1 ["prates"] = 12;
Reprow1 ["nummonth"] = 12;
Reprow1 ["enddate"] = "2011-6-20 ";
Reportds. tables ["dtpack"]. rows. add (reprow1 );
Datarow reprow2 = reportds. tables ["dtpack"]. newrow ();
Reprow2 ["pname"] = "classic video ";
Reprow2 ["prates"] = 2;
Reprow2 ["nummonth"] = 12;
Reprow2 ["enddate"] = "2011-6-20 ";
Reportds. tables ["dtpack"]. rows. add (reprow2 );
// The above are the two test data temporarily added to the table.
// Add other data below
Cryreport1.load ("dtvreport. rpt ");
Cryreport1.setdatasource (reportds. tables ["dtpack"]);
// The following describes how to assign values to several parameter fields in the report.
Cryreport1.setparametervalue ("tuseraddress", "user's home address ");
Cryreport1.setparametervalue ("toperator", "Sun Li ");
Cryreport1.setparametervalue ("tusername", "payer name ");
Cryview. reportsource = cryreport1;
}
Private void button#click (object sender, eventargs e)
{
System. drawing. printing. printdocument pdoc = new printdocument ();
Int rawkind = 1;
For (int I = 0; I <= pdoc. printersettings. papersizes. count-1; I ++)
{
If (pdoc. printersettings. papersizes [I]. papername = "reppage ")
{// Here, I used the custom paper name reppage
Rawkind = pdoc. printersettings. papersizes [I]. rawkind;
}
}
Reportdocument mydoc = new reportdocument ();
Mydoc. printoptions. papersize = (crystaldecisions. shared. papersize) rawkind;
Pagemargins pmargin = new pagemargins ();
Pmargin. topmargin = 100;
Pmargin. bottommargin = 100;
Pmargin. leftmargin = 10;
Pmargin. rightmargin = 10;
Mydoc. printoptions. applypagemargins (pmargin );
// Defines the boundary. I have already customized the boundary in the report, but the left and right boundary values do not seem to work during printing, there is a lot of free space on the left side of the printed invoice. It is estimated that there should be at least 1 cm. Upper and lower boundaries play a role.
// Add other data below
String fn = "dtvreport. rpt ";
// String fn = application. startuppath + "\ dtvreport. rpt ";
Mydoc. load (fn );
Mydoc. setdatasource (reportds. tables ["dtpack"]);
Mydoc. setparametervalue ("tuseraddress", "user's home address ");
Mydoc. setparametervalue ("toperator", "Sun Li www.111cn.net ");
Mydoc. setparametervalue ("tusername", "payer name ");
Mydoc. printtoprinter (1, false, 0, 0); // start printing all pages
}