On Mon, 14 May 2007 08:25:30 -0700, Greg Esres <ge****@boundvortex.com>
wrote:
I have some text lines to print that are much longer than the width of
the paper, maybe as much as 6 times. For a given page, I'd like
everything that doesn't fit to print on a second page, and whatever
doesn't fit there to go on a third page, etc, so that I can piece
together the final output by laying the pages side by side.
Things that don't work well:
1) Breaking up the line into sections of a given # of characters.
Problem with variable pitch fonts.
2) Writing output string to internal bitmap and grabbing it piece by
piece of a given width. (Too slow.)
Granted, I have not used the .NET print API yet, but I used to do a fair
amount of work with other print APIs. I think that given the built-in
functionality in .NET, you should not have to do any text-wrapping
yourself.
Two things are important here: PrintDocument provides you with a "one page
at a time", via the PrintPage event; and MeasureString provides a way for
you to specify the width of your output and return the total line-wrapped
height for the given string.
So, what you do is use MeasureString to figure out how far down each
text-wrapped string goes (DrawString will wrap text for you if you ask it
to). When you get a string that doesn't fit on the page, you're not done
with it yet. Save the height of the output string that has been printed
so far, and use that as a negative offset when drawing the string to the
next page (in the next PrintPage event). Continue this until the bottom
of your output string is within the bounds of the printed page, at which
point you move on to the next string (if any).
It seems to me that the above process should handle any font, without
requiring the use of secondary bitmap buffer.
Now, upon a second reading of your post, I'm not exactly clear on how you
want the strings to print. I had assumed you just wanted them
line-wrapped, based on the other replies I saw. But now that I read your
post again, I think it might be that you don't want the text wrapped, but
rather just want the text to spill across subsequent pages horizontally.
If so, the above idea is still the basic concept, but it's a little more
complicated because you need to build up a layout of your output, tracking
how many pages vertically and horizontally you'll need and printing the
appropriate strings with appropriate negative horizontal positions on
pages to the right of the initial column of pages of output.
Even so, you should not have to deal with text wrapping/breaking yourself,
nor should you have to generate internal bitmaps rather than printing text
directly.
Pete