I see you've already found a fix to your issue but I thought others might
like a an answer to your original question. Here's what I do for wordwrap
using the tools supplied with .NET.
// This routine will print a document with 2 columns,
// wordraped text in each column, the left and right
// columns match row sizes, similare to equal cell sizes
private void document_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
float left = e.MarginBounds.Left;
float top = e.MarginBounds.Top;
float width = e.MarginBounds.Width / 2 - 5; // this spaces the columns
evenly
float height = e.MarginBounds.Height;
int fitted = 0; // use only to correctly format the MeasureString method
int qFilled = 0, aFilled;
// The variables are named to identify a question column and an answer
column
RectangleF qDrawRect = new RectangleF(left, top, width, height); // Left
column rectangle
RectangleF aDrawRect = new RectangleF(left + width + 10, top, width,
height); // right
// Setup to measure the line lines
SizeF qSizeF = new SizeF(width, height);
SizeF aSizeF = new SizeF(width, height);
// Although you can initialize string attributes here
// this line is only to fill in the parameter list
// We'll use whatever is given to us
StringFormat newStringFormat = new StringFormat();
// The following code sets the font to be
// the same as the displayed font (in this case, in
// a datagrid)
System.Drawing.Font printFont = new System.Drawing.Font
(myGrid.DefaultCellStyle.Font.Name,
myGrid.DefaultCellStyle.Font.Size,
myGrid.DefaultCellStyle.Font.Style);
// Insert code to render the page here.
// This code will be called when the control is drawn.
while (linesPrinted <= lines.GetUpperBound(0))
{
// Here's where we find out how many lines will be printed during a
word wrap
e.Graphics.MeasureString(lines[linesPrinted,0], printFont,
qSizeF, newStringFormat, out fitted, out qFilled);
e.Graphics.MeasureString(lines[linesPrinted,1], printFont,
aSizeF, newStringFormat, out fitted, out aFilled);
// This prints the two columns, properly formatted and,
// if necessary, wordwrapped
e.Graphics.DrawString(lines[linesPrinted, 0], printFont,
System.Drawing.Brushes.Black, qDrawRect);
e.Graphics.DrawString(lines[linesPrinted++, 1], printFont,
System.Drawing.Brushes.Black, aDrawRect);
// Now we use the info we gathered in the MeasureString statement
// If either column wrapped, qFilled and/or aFilled will be larger
than 1
// I'm using the ?: operator to determine which is larger and using
that
// one. I also add a half line to the equation to provide "paragraph"
// spacing, a nice touch to the printout
aDrawRect.Y += (int)myGrid.DefaultCellStyle.Font.Height *
(qFilled > aFilled ? qFilled : aFilled) +
(int)(myGrid.DefaultCellStyle.Font.Height * .5);
qDrawRect.Y = aDrawRect.Y;
if (aDrawRect.Y >= e.MarginBounds.Bottom)
{
e.HasMorePages = true;
return;
}
}
linesPrinted = 0;
e.HasMorePages = false;
}
"Jeff B." wrote:
Has anyone come across a decent algorithm for implementing word wrap
features in .net printing? I have a small component that uses basic
printing techniques (i.e. e.Graphics.DrawString in a PrintPage event of a
PrintDocument object) to send some formatted text to the printer. However,
if the lines are too long they run off the page rather than wrapping around.
I'm sure I can spend the time and come up with a word wrapping algorithm but
figured why go through the trouble if someone already knows of one :-)
--- Thanks, Jeff
--
Jeff Bramwell
Digerati Technologies, LLC
www.digeratitech.com
Manage Multiple Network Configurations with Select-a-Net
www.select-a-net.com