|
HowTo: Use the PrintForm PrintPageComplete Event to Draw 'Rubber Stamp' Text on Top of the Printout
ID: W3298004
Background
The System.Drawing.Printing.PrintDocument class raises a PrintPage event to give you an opportunity to draw the content of the page, but PrintForm, which is derived from PrintDocument, draws its content on the page after the PrintPage event has fired, so if you want to draw on top of the page after all drawing has completed you must use the PrintForm.PrintPageComplete event.
Drawing in the PrintPageComplete Event
Handle the PrintForm PrintPageComplete event and add the following code to draw rotated 'rubber stamp' style text on top of the page output.

C# Code Sample:
private void printForm1_PrintPageComplete(object sender, TMGDevelopment.Printing.PrintPageCompleteEventArgs e)
{
this.DrawTextRotated("Not Valid", e.Graphics, e.PageBounds, 315F, true);
}
private void DrawTextRotated(string text, Graphics graphics, Rectangle bounds, float rotation, bool hollow)
{
// make sure no transformations we apply here affect subsequent graphics operations
System.Drawing.Drawing2D.GraphicsContainer gc = graphics.BeginContainer();
System.Drawing.Font f = new System.Drawing.Font("Arial", 100);
System.Drawing.SizeF sizeText = graphics.MeasureString(text, f);
// find centre of drawing rectangle
System.Drawing.Point pointCentre = new System.Drawing.Point(bounds.Width / 2, bounds.Height / 2);
graphics.TranslateTransform(pointCentre.X, pointCentre.Y);
// rotate around centre point
graphics.RotateTransform(rotation);
// calc start point for text centered in bounding rect, based on text length
System.Drawing.Point pointStart = new System.Drawing.Point(-(int)(sizeText.Width / 2), -(int)(sizeText.Height / 2));
if (hollow)
{
// hollow text, draw as path outline
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
float emSize = 100F * f.SizeInPoints / 72F;
path.AddString(text, f.FontFamily, (int)f.Style, emSize, pointStart, StringFormat.GenericDefault);
Pen pen = new Pen(Brushes.Gray, 2F);
graphics.DrawPath(pen, path);
pen.Dispose();
path.Dispose();
}
else
{
// filled text
graphics.DrawString(text, f, System.Drawing.Brushes.Gray, pointStart);
}
f.Dispose();
// restore previous transforms etc
graphics.EndContainer(gc);
}
Last updated
04-Aug-2005
|