Print and provide print previews in C#

A PrintDocument object's PrintPage event handler generates graphics for printing. A PrintPreviewDialog can display a preview for a PrintDocument object. You can create these objects in code but it's easier to create them at design time.

Add a PrintDocument object and a PrintPreviewDialog object to the form. Set the PrintPreviewDialog's Document property to the PrintDocument.

Next give the PrintDocument a PrintPage event handler to generate the pages. Set e.HasMorePages to indicate whether this is the last page. The document raises this event until e.HasMorePages is false.

The following code shows this example's PrintPage event handler. It displays shapes and page numbers on four pages. This is the most interesting part of the printing process.

// Print the document's pages.
private int m_NextPage = 0;
private void pdocShapes_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
// Draw a shape depending on the page we are printing.
switch (m_NextPage)
{
case 0: // Draw an ellipse.
using (Pen the_pen = new Pen(Color.Red, 10))
{
e.Graphics.DrawEllipse(the_pen, e.MarginBounds);
}
break;
case 1: // Draw a triangle.
using (Pen the_pen = new Pen(Color.Green, 10))
{
int xmid = (int)(e.MarginBounds.X + e.MarginBounds.Width / 2);
Point[] pts =
{
new Point(xmid, e.MarginBounds.Top),
new Point(e.MarginBounds.Right, e.MarginBounds.Bottom),
new Point(e.MarginBounds.Left, e.MarginBounds.Bottom),
};
e.Graphics.DrawPolygon(the_pen, pts);
}
break;
case 2: // Draw a rectangle.
using (Pen the_pen = new Pen(Color.Blue, 10))
{
e.Graphics.DrawRectangle(the_pen, e.MarginBounds);
}
break;
case 3: // Draw a diamond.
using (Pen the_pen = new Pen(Color.Orange, 10))
{
int xmid = (int)(e.MarginBounds.X + e.MarginBounds.Width / 2);
int ymid = (int)(e.MarginBounds.Y + e.MarginBounds.Height / 2);
Point[] pts =
{
new Point(xmid, e.MarginBounds.Top),
new Point(e.MarginBounds.Right, ymid),
new Point(xmid, e.MarginBounds.Bottom),
new Point(e.MarginBounds.Left, ymid),
};
e.Graphics.DrawPolygon(the_pen, pts);
}
break;
}

// Draw the page number.
// Center it inside the margins.
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;

using (Font the_font = new Font("Times New Roman", 200, FontStyle.Bold))
{
using (Brush the_brush = new SolidBrush(Color.Black))
{
e.Graphics.DrawString(String.Format("{0}", m_NextPage + 1),
the_font, the_brush, e.MarginBounds, sf);
}
}

// Next time print the next page.
m_NextPage += 1;

// We have more pages if wee have not yet printed page 3.
e.HasMorePages = (m_NextPage <= 3);

// If we have no more pages, reset for the next time we print.
if (m_NextPage > 3) m_NextPage = 0;
}

With all of this set up, printing and displaying a preview is remarkably easy.

// Display a print preview.
private void btnPreview_Click(object sender, EventArgs e)
{
ppdShapes.ShowDialog();
}

// Print.
private void btnPrint_Click(object sender, EventArgs e)
{
pdocShapes.Print();
}

Note that there is no easy way to tell how many pages will be in your printout so you cannot print things like "Page 1 of 12." To do that, you need to examine whatever you are printing before you start the printout and figure out how many pages there will be.

(Next time I'll discuss other useful PrintDocument events.)

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.