I am extracting information from SQL and writing reports. This should (I hate that word) be easy... but :
All is fine as long as the formatted text fits on one page. However, if the report becomes more involved (more data in SQL) more than one page is required.
I'm used to printers handling this automatically..
So here's some code : (note applicants, interviews, and positions are Queue's that have been filled from the DB, while departments is a string[] array also pulled out of the DB)
This first part just builds the report as a single string to pass to the rptForm web form where it is displayed in a rich text box. From there the user can edit, change the font, etc. Then either save the report as a file (which works fine), or print the report (which only prints as much as will fit on one page and looses the rest).
private void rptDeptDet(object sender, EventArgs e) {
string line = "" ;
for(int i=0 ; i<60 ; i++) line += "-" ;
string body = line + "\n\n" + getHeader() + "\n\n" + line +
"\nCompany department detail report\n" + line + "\n\n" ;
for(int i=0 ; i<departments.Length ; i++) {
int open = 0 ;
int closed = 0 ;
int filled = 0 ;
string openPos = "" ;
string closedPos = "" ;
string filledPos = "" ;
foreach(Position p in positions) {
string [] d = p.getPdate().Split(' ') ;
string when = getMonthName(d[0]) + " " + d[1] + ", " + d[2] ;
if (p.getDept() == departments[i]) {
if (p.getStatus() == "filled") {
filled++ ;
filledPos += " " + p.getPname() + ", " + p.getShft() +
" shift\n Opened date " + when + "\n" ;
} else if (p.getStatus() == "closed") {
closed ++ ;
closedPos += " " + p.getPname() + ", " + p.getShft() +
" shift\n Opened date " + when + "\n" ;
} else {
open++ ;
openPos += " " + p.getPname() + ", " + p.getShft() +
" shift\n Opened date " + when + "\n" ;
}
}
}
body += "Department : " + departments[i] + "\n" +
" Open positions (" + open.ToString() + ")\n" +
openPos + "\n" +
" Filled positions (" + filled.ToString() + ")\n" +
filledPos + "\n" +
" Closed positions (" + closed.ToString() + ")\n" +
closedPos + "\n" + line + "\n" ;
}
body += "End of report\n" + line + "\n" ;
Reports rptForm =
new Reports("HRE_Department_Detail.txt",body) ;
rptForm.Show() ;
}
The code for the print event is pretty standard stuff : (this code is in the Report.cs (code behind file) that goes with the Reports windows form.
private void genPrint(object sender, EventArgs e) {
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(myPrintPage) ;
printDialog1.Document = pd ;
DialogResult result = printDialog1.ShowDialog() ;
if (result==DialogResult.OK) pd.Print() ;
}
private void myPrintPage(object sender, PrintPageEventArgs ev) {
string junk = rtbReport.Text ;
Font printFont = new Font("Courier New", 10);
ev.Graphics.DrawString(junk,printFont,Brushes.Black,
new Point(50,50)) ;
}
So how do I get the printer to print more than one page when there is more than one page of data?