I\'m doing some custom printing using a PrintDocument in my application. I have a requirement of logging when our items are successfully printed. I originally achieved this
I too managed to figure out a different way that worked for me...
I had a list of MyPrintFileDetail classes each containing a PrintDocument and a StreamReader for said document.
While setting up my PrintDocument, I added an PrintPage event. In the PrintPage event handler I identified which PrintDocument I was working with via casting the "sender" to a PrintDocument. Then wrote a foreach loop to identify the working MyPrintFileDetail object from the list to get the StreamReader I was using to print. Once there were no more lines to print, I disposed of the StreamReader and set it to null.
Then within my Timer callback for processing the list of MyPrintFileDetail objects, I checked the StreamReader for null and if null, I was done printing.
Kind of clunky but it worked.
private void PD_PrintPage(object sender, PrintPageEventArgs e)
{
PrintDocument p = (PrintDocument)sender;
PrintFileDetail pfdWorkingOn = null;
foreach (PrintFileDetail pfd in pfds)
{
if (pfd._PrintDoc.DocumentName == p.DocumentName)
{
pfdWorkingOn = pfd;
break;
}
}
float yPos = 0f;
int count = 0;
float leftMargin = e.MarginBounds.Left;
float topMargin = e.MarginBounds.Top;
string line = null;
float linesPerPage = e.MarginBounds.Height / _TextFilePrintingFont.GetHeight(e.Graphics);
while (count < linesPerPage)
{
line = pfdWorkingOn._TxtFileBeingPrinted.ReadLine();
if (line == null)
{
break;
}
yPos = topMargin + count * _TextFilePrintingFont.GetHeight(e.Graphics);
e.Graphics.DrawString(line, _TextFilePrintingFont, Brushes.Black, leftMargin, yPos, new StringFormat());
count++;
}
if (line != null)
{
e.HasMorePages = true;
}
else
{
pfdWorkingOn._TxtFileBeingPrinted.Dispose();
pfdWorkingOn._TxtFileBeingPrinted = null;
}
}