Ok, this ought to be a no brainer.. but apparently it's ME that's a no brainer.
[rant]
OK, I'm a BIT frustrated here.. I've programmed in PERL, C++, C, and various UNIX shell languages for years. Printing was something that was the easiest thing to do! This is not rocket science. I'm not building a rocket to fly to MARS.. I just want to print my report.
[/rant]
I am creating a report that may or may not be of multiple pages. For the life of me I cannot get it to print correctly.
Here's the code. (the report is coming to it from a rich text box)
// =========================================================================
// Menu strip File -> Print click event
// send the data directly to the printer
private void genPrint(object sender, EventArgs e) { // =====================
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage) ; // function below
printDialog1.Document = pd ;
DialogResult result = printDialog1.ShowDialog() ;
if (result==DialogResult.OK) pd.Print() ;
} // =======================================================================
// =========================================================================
// function used by PrintPageEventHandler
private void pd_PrintPage(object sender, PrintPageEventArgs ev) { // =======
string[] pBody = rtbReport.Text.Split('\n') ;
Font printFont = new Font("Courier New", 10) ;
float maxLines = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics) ;
float top = ev.MarginBounds.Top ;
float left = ev.MarginBounds.Left ;
for(int i=0 ; i < pBody.Length ; i++) { // Print each line of the file
ev.HasMorePages = true ;
for(int num=0 ; num < maxLines && i < pBody.Length ; num++) {
float yPos = top + (num * printFont.GetHeight(ev.Graphics)) ;
ev.Graphics.DrawString(pBody[i++],printFont,Brushes.Black,left,yPos) ;
}
if (i >= pBody.Length) // this shouldn't be necessary, but if I leave
ev.HasMorePages = false ; // it out I get in infinite number of pages
// IF I leave it in, I just get the first page
} // of my document
} // =======================================================================
I could really use some help here. I can't find any documentation (that works) on line or in any of my books. WHAT the heck is going wrong here. I get the first page only of my report. If I take the ev.HasMorePages = false part out, then I get a never ending number of pages in my report.
PLEASE HELP!!
This is driving me absolutely NUTS.
Edited to add :
I just tried using the code at :
http://www.c-sharpcorner.com/UploadFile/mgold/PritinginCSharp11222005040630AM/PritinginCSharp.aspx
And got the same exact result.
HELP!!
Loading
Master BillaPosted Aug 14, 2009, 11:05 PM
I have worked for you to do this
Using System;
Using System.Windows.Forms;
Using System.Drawing;
Using System.Drawing.Printing;
Using System.IO;
Namespace CGPrintDemo
{
Public class Form1:Form
{
PrintDocument pd = new PrintDocument();
string strPrint;
public form1()
{
initComponenet();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtFileNm.Text = openFileDialog1.FileName;
}
}
private void btnPrint_Click(object sender, EventArgs e)
{
// Displays the document name in the print status or queue
pd.DocumentName = txtFileNm.Text;
// Associate PrintPage method with its event handler
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
// Open the document
using (FileStream fs = new FileStream(pd.DocumentName, FileMode.Open))
// Read the contents of the document in a stream reader object
using (StreamReader sr = new StreamReader(fs))
{
strPrint = sr.ReadToEnd();
}
// Calling this method invokes the PrintPage event
pd.Print();
}
private void pd_PrintPage(object sender, PrintPageEventArgs e)
{
int charCount = 0;
int lineCount = 0;
e.Graphics.MeasureString(strPrint, this.Font,
e.MarginBounds.Size, StringFormat.GenericTypographic,
out charCount, out lineCount);
// Determine the page bound and draw the string accordingly
e.Graphics.DrawString(strPrint, this.Font, Brushes.Black,
e.MarginBounds, StringFormat.GenericTypographic);
// Now remove that part of the string that has been printed.
strPrint = strPrint.Substring(charCount);
// Check if any more pages left for printing
if (strPrint.Length > 0)
e.HasMorePages = true;
else
e.HasMorePages = false;
}
}
}
Thank you
caryPosted Aug 20, 2009, 12:28 AM
Master BillaPosted Aug 17, 2009, 1:19 PM
Ken BarrettPosted Aug 17, 2009, 11:11 AM
Now I have to figure out how to make this "green" since you really helped me!!
THANKS!! VERY MUCH!!
Additionally, I have to give kudos for efficient code. All the other solutions I saw for this problem involved printing each line with a separate call to graphics.Drawstring.. a lot of unnecessary overhead there. Slurping the data into one chunk then printing it chunk style is much more efficient! Very nicely done.
ETA
Would you consider posting this solution on another forum or pointing them here? I tried as many avenues as I could, others were helpful but their solutions didn't work. Sharing a valid, working, and efficient solution would probably help many. Let me know.