Print Preview Prints Blank pages....

  • Thread starter Páll Ólafsson
  • Start date
P

Páll Ólafsson

Hi

Im trying to print out contents of a panel. I use the Graphics object to
draw what I want to print and the print preview displays everything OK but
when I print the contents the printer prints out blank page?

here is a little description what this class does; I have a Panel that
consists of other panels. Each of the inner panels consists of other
controls that I have to print out. I create an enumerator to print the
panels in reverse order.

here is my code.. most of it I found on the net...
Hopefully someone can help me with this problem.... Im sure this is a minor
problem but I can't figure it out by myself?

public class PrintForm
{

private Control _ctrl = null;
private int _iCurrentPageIndex = 0;
private int _iCurrentControlIndex = 0;
private int m_DocHeight = 900;
private int m_DocWidth = 624;
private int m_iPaperHeight = 0;
private Control m_CurrCtrl = null;
private bool m_bHasMorePages = false;
private ArrayList m_alControls = new ArrayList();
private IEnumerator ieGrunnBelti = null;
private int m_iTop = 0;


public PrintForm(Control ctrl)
{
_ctrl = ctrl;
}

public void Print()
{
Print(_ctrl);
}

public void Print(Control ctrl)
{
if (ctrl == null)
return;

ieGrunnBelti = _ctrl.Controls.GetEnumerator();
this.ControlsToArray(_ctrl);

PrintDocument pd = new PrintDocument();
pd.BeginPrint += new PrintEventHandler(this.PrintDocument_BeginPrint);
pd.PrintPage += new PrintPageEventHandler(this.PrintDocument_PrintPage);

PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = pd;
ppd.ShowDialog();
}


private void ControlsToArray(Control p_ctrl)
{
while(ieGrunnBelti.MoveNext())
m_alControls.Add(ieGrunnBelti.Current);

_iCurrentControlIndex = m_alControls.Count;
}

private Point[] GeneratePrintingOffsets(Rectangle rMargins)
{
...
}


private void DrawControl(Control ctrl,Point ptOffset, Graphics g, ref
PrintPageEventArgs e)
{
m_bHasMorePages = false;
Control c = null;
while(m_alControls.Count > 0)
{
--_iCurrentControlIndex;
if(_iCurrentControlIndex < 0)
break;
c = (Control)m_alControls[_iCurrentControlIndex];

Point p = new Point(c.Left, c.Top - m_iTop);
p.Offset(ptOffset.X, ptOffset.Y);

if(c is Skyrslubelti.clsGrunnBelti || c is
Skyrslubelti.SkyrslaAreksturBill)
{
if((m_iPaperHeight + c.Height) < m_DocHeight)
m_iPaperHeight += c.Height;
else
{
m_iPaperHeight = 0;
_iCurrentPageIndex++;
++_iCurrentControlIndex;
m_iTop = c.Top;
m_bHasMorePages = true;
break;
}
DrawFrame(c, p, g);
this.DrawSkyrsluBelti(c,p,g);
}
}
if(m_bHasMorePages)
e.HasMorePages = true;
else
e.HasMorePages = false;
}

private void DrawSkyrsluBelti(Control p_ctrl, Point ptOffset, Graphics g)
{

foreach(Control c in p_ctrl.Controls)
{
Point p = new Point(c.Left, c.Top);
p.Offset(ptOffset.X, ptOffset.Y);

if (c is Label)
DrawLabel((Label)c, p, g);
else if (c is CheckBox)
DrawCheckBox((CheckBox)c, p, g);
else if(c is PictureBox)
DrawImage((PictureBox)c, p, g);
else if (c is Skyrslubelti.clsSkyrslaTexti)
DrawLine(c, p, g);
else if (c is RichTextBox)
DrawRTB((RichTextBox)c, p, g);
else if (c is Skyrslubelti.SkyrslaAreksturBill)
DrawFrame(c, p, g);

DrawSkyrsluBelti(c, p, g);
}
}

private void DrawImage(PictureBox pb, Point p, Graphics g)
private void DrawLabel(Label lbl, Point p, Graphics g)
private void DrawLine(Control c,Point p, Graphics g)
private void DrawFrame(Control c,Point p, Graphics g)
private void DrawRTB(RichTextBox p_RTB,Point p, Graphics g)
private void DrawCheckBox(CheckBox chk, Point p, Graphics g)
private void PrintDocument_BeginPrint(object sender, PrintEventArgs e)
{
this._iCurrentPageIndex = 0;
}

private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
Rectangle rPageMargins = new Rectangle(e.MarginBounds.Location,
e.MarginBounds.Size);

Point[] ptOffsets = GeneratePrintingOffsets(rPageMargins);

e.Graphics.SetClip(rPageMargins);

Point ptOffset = new Point(-ptOffsets[0].X, -ptOffsets[0].Y);
ptOffset.Offset(rPageMargins.X, rPageMargins.Y);

DrawControl(_ctrl,ptOffset, e.Graphics, ref e);

}

}
 
P

Páll Ólafsson

Fixed it?

Páll Ólafsson said:
Hi

Im trying to print out contents of a panel. I use the Graphics object to
draw what I want to print and the print preview displays everything OK but
when I print the contents the printer prints out blank page?

here is a little description what this class does; I have a Panel that
consists of other panels. Each of the inner panels consists of other
controls that I have to print out. I create an enumerator to print the
panels in reverse order.

here is my code.. most of it I found on the net...
Hopefully someone can help me with this problem.... Im sure this is a minor
problem but I can't figure it out by myself?

public class PrintForm
{

private Control _ctrl = null;
private int _iCurrentPageIndex = 0;
private int _iCurrentControlIndex = 0;
private int m_DocHeight = 900;
private int m_DocWidth = 624;
private int m_iPaperHeight = 0;
private Control m_CurrCtrl = null;
private bool m_bHasMorePages = false;
private ArrayList m_alControls = new ArrayList();
private IEnumerator ieGrunnBelti = null;
private int m_iTop = 0;


public PrintForm(Control ctrl)
{
_ctrl = ctrl;
}

public void Print()
{
Print(_ctrl);
}

public void Print(Control ctrl)
{
if (ctrl == null)
return;

ieGrunnBelti = _ctrl.Controls.GetEnumerator();
this.ControlsToArray(_ctrl);

PrintDocument pd = new PrintDocument();
pd.BeginPrint += new PrintEventHandler(this.PrintDocument_BeginPrint);
pd.PrintPage += new PrintPageEventHandler(this.PrintDocument_PrintPage);

PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = pd;
ppd.ShowDialog();
}


private void ControlsToArray(Control p_ctrl)
{
while(ieGrunnBelti.MoveNext())
m_alControls.Add(ieGrunnBelti.Current);

_iCurrentControlIndex = m_alControls.Count;
}

private Point[] GeneratePrintingOffsets(Rectangle rMargins)
{
...
}


private void DrawControl(Control ctrl,Point ptOffset, Graphics g, ref
PrintPageEventArgs e)
{
m_bHasMorePages = false;
Control c = null;
while(m_alControls.Count > 0)
{
--_iCurrentControlIndex;
if(_iCurrentControlIndex < 0)
break;
c = (Control)m_alControls[_iCurrentControlIndex];

Point p = new Point(c.Left, c.Top - m_iTop);
p.Offset(ptOffset.X, ptOffset.Y);

if(c is Skyrslubelti.clsGrunnBelti || c is
Skyrslubelti.SkyrslaAreksturBill)
{
if((m_iPaperHeight + c.Height) < m_DocHeight)
m_iPaperHeight += c.Height;
else
{
m_iPaperHeight = 0;
_iCurrentPageIndex++;
++_iCurrentControlIndex;
m_iTop = c.Top;
m_bHasMorePages = true;
break;
}
DrawFrame(c, p, g);
this.DrawSkyrsluBelti(c,p,g);
}
}
if(m_bHasMorePages)
e.HasMorePages = true;
else
e.HasMorePages = false;
}

private void DrawSkyrsluBelti(Control p_ctrl, Point ptOffset, Graphics g)
{

foreach(Control c in p_ctrl.Controls)
{
Point p = new Point(c.Left, c.Top);
p.Offset(ptOffset.X, ptOffset.Y);

if (c is Label)
DrawLabel((Label)c, p, g);
else if (c is CheckBox)
DrawCheckBox((CheckBox)c, p, g);
else if(c is PictureBox)
DrawImage((PictureBox)c, p, g);
else if (c is Skyrslubelti.clsSkyrslaTexti)
DrawLine(c, p, g);
else if (c is RichTextBox)
DrawRTB((RichTextBox)c, p, g);
else if (c is Skyrslubelti.SkyrslaAreksturBill)
DrawFrame(c, p, g);

DrawSkyrsluBelti(c, p, g);
}
}

private void DrawImage(PictureBox pb, Point p, Graphics g)
private void DrawLabel(Label lbl, Point p, Graphics g)
private void DrawLine(Control c,Point p, Graphics g)
private void DrawFrame(Control c,Point p, Graphics g)
private void DrawRTB(RichTextBox p_RTB,Point p, Graphics g)
private void DrawCheckBox(CheckBox chk, Point p, Graphics g)
private void PrintDocument_BeginPrint(object sender, PrintEventArgs e)
{
this._iCurrentPageIndex = 0;
}

private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
Rectangle rPageMargins = new Rectangle(e.MarginBounds.Location,
e.MarginBounds.Size);

Point[] ptOffsets = GeneratePrintingOffsets(rPageMargins);

e.Graphics.SetClip(rPageMargins);

Point ptOffset = new Point(-ptOffsets[0].X, -ptOffsets[0].Y);
ptOffset.Offset(rPageMargins.X, rPageMargins.Y);

DrawControl(_ctrl,ptOffset, e.Graphics, ref e);

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top