By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,645 Members | 1,065 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,645 IT Pros & Developers. It's quick & easy.

Print Document && Wrapping

P: n/a
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.)

Seems like there must be an easier solution to this.

May 14 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Greg Esres" <ge****@boundvortex.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
>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.)

Seems like there must be an easier solution to this.
I assume you are using a PrintDocument and using DrawString to render the
strings you want to print. In that case, you can use MeasureString to find
out how wide your string is going to be when printed in a specified font. If
the length exceeds the page width, you remove a character from the string
and execute MeasureString again to find out how long the string is with one
character less. Loop until it fits in the page. I've done this and it is
quite fast (when compared with the speed of the printer) but if you find it
to be inefficient, you can do a dicothomic search instead of removing
characters one by one to find out the optimal length.
May 14 '07 #2

P: n/a
Hi,
"Greg Esres" <ge****@boundvortex.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
>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.
You have two options:
1- Print it yourself using PrintDocument (if so see my remarks to your
problems below)
2- Create a report using CR and let him handle those perky details :) , you
can simply pass the strings as parameters.
Things that don't work well:

1) Breaking up the line into sections of a given # of characters.
Problem with variable pitch fonts.
Nop, problem with your code, you have to use MeasureString to know how big
the string will be, and split it accordingly.
2) Writing output string to internal bitmap and grabbing it piece by
piece of a given width. (Too slow.)
I have never tried this before.

Printing using PRintDocument can be a pain in the back, at least at the
beginning, also be ready to spend a lot of papers doing test :)
May 14 '07 #3

P: n/a
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
May 14 '07 #4

P: n/a
Alberto Poblacion wrote:

<<Loop until it fits in the page. I've done this and it is
quite fast (when compared with the speed of the printer) but if you
find it
to be inefficient, you can do a dicothomic search instead of removing
characters one by one to find out the optimal length.>>

I did think of this, but thought I'd run into some issues with
variable pitch fonts; some things which line up might not do so when
a wider character is removed to the next page.

However, my current application is using fixed pitch fonts, so it will
work.

Thank you.

May 15 '07 #5

P: n/a
Ignacio Machin wrote:

<<Create a report using CR and let him handle those perky details :) ,
you
can simply pass the strings as parameters.>>
Yes, if I had known of this problem ahead of time, that would have
been an easier solution.

<<Nop, problem with your code, you have to use MeasureString to know
how big
the string will be, and split it accordingly.>>

I did think of this, but thought I'd run into some issues with
variable pitch fonts; some things which line up might not do so when a
wider character is removed to the next page.

However, my current application is using fixed pitch fonts, so it will
work.

Thank you.
May 15 '07 #6

P: n/a
Peter Duniho wrote:

<<negative offsets>>

Your second reading of my question got it right, and this was the
strategy I attempted at first. It worked well for about 3 pages, but
it seems that PrintDocument has a problem with negative offsets of
more than about -5000 to -6000. The text starts appearing from the
right side and moves towards the left.
May 15 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.