PrintPage and table format

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
does anybody have an example of PrintPage event which builds a PrintDocument
as a table and handles also the size of columns contents like this:

Title1 |Title2 |Title3
================================
first cell has |second cell |also third
a text longer | |cell has a longer
than the size | | text
column | |
-----------------------------------------------------------
new line and |new line,second |new line third cell
new cell | cell |
-----------------------------------------------------------

So far, I've built only the table, but I can't handle the content according to
the column size.

PLEASE HELP ME..... ;)
 
Siu,
I haven't done this directly as a table but I have handled wrapping text
within a boundary to multiple lines. Use the Graphics.DrawString overload
that returns the number of lines used i.e.
Graphics.DrawString("draw this", aFont, aRectangleF,
StringFormat.GenericTypographic)
to draw the string and
MeasureString("draw this", aFont, aSizeF,
StringFormat.GenericTypographic, nChars, nLines) where nChars and nLines are
out variables and aSizeF is a SizeF the width and height of the RectangleF
used to draw the string. Make aRectangleF as wide as you need to draw in
and high enough to take several lines of text. Its' left x and top y values
are where the drawing will start.
Using these for each cell you can get the maximum height in lines of the
cells in a row and use this to advance the y location as required. The
DrawString will wrap at word breaks and truncate the text when it runs out
of space in the specified rectangle. You can check nChars to see how many
characters fit in the specified rectangle and do a page advance if not
enough fit. Of course if you do this you will have to do all the
MeasureString calls for a line first.

Ron Allen
 
Hi Ron,
can you please send to me your code? It's difficult to me understanding what
you have written...

Thank you ;)
 
Siu,
I have this encapsulated in a routine that may be somewhat opaque but
here it is. Note that all my drawing routines take measurements in inches
and assume that the default (1/100th inch) measurement unit is used for the
Graphics. The following draws the string supplied in a rectangle with the
specified left, right, top, bottom coordinates using the specified brush and
font.
I can't really send the code that calls this out-of-house.

Ron Allen

public int PrintInRect(Graphics g, Brush b, Font f, float x, float y,
float xr, float yb, String s, StringFormat sFmt)
{
// arguments are supplied in inches and the default measurement is used
// places the left edge of the string at the supplied x location
// assume user has done a translatetransform on the graphics for the hard
margins
// x is the right hand edge of the rectangle to print in, xr is the
lefthand edge
// y is the top edge and yb is the bottom edge
float x1 = (100.0f * x); // convert to 100ths of inches
float y1 = (100.0f * y);
float x1r = (100.0f * xr);
float y1b = (100.0f * yb);
// make sure that these are all positive or zero
x1 = Math.Max(x1, 0.0f);
y1 = Math.Max(y1, 0.0f);
x1r = Math.Max(x1r, 0.0f);
y1b = Math.Max(y1b, 0.0f);
int nLines, nChars;
// create the RectangleF to draw in
RectangleF rect = new RectangleF(x1, y1, (x1r - x1), (y1b - y1));
// and the SizeF for measuring
SizeF sz = new SizeF((x1r - x1), (y1b - y1));
// measure lines and characters used. Note nChars is presently
disregarded.
g.MeasureString(s, f, sz, sFmt, out nChars, out nLines);
// draw in the desired rectangle with wrapping
g.DrawString(s, f, b, rect, sFmt);
return nLines; // get back the # of lines that this used
}
 
Back
Top