ABCPDF - calculate space required for text before drawing it to the PDF page

2.1k Views Asked by At

I'm hoping to tell if a piece of text is going to fit at the bottom of a page before I draw it on the page and check its y position.

Does anyone know if this is possible with ABCPDF?

4

There are 4 best solutions below

0
On

As far as I remember from when I was doing PDF stuff (like ... 6-7 years ago with ABCPDF) we used allot of time with Graphics.MeasureString methods.
You might wanna take a peek at it on MSDN

0
On

If graphics.Measurestring does not work for you the other option is to literally measure the size of certain fonts so that you know how many letters fit on a line and can then work out how many lines you will have in a given rectangle and size the rectangle accordingly using something like yVal = lineCount * 5.

If I remember correctly I could not use the measurestring function for some reason and so had to do it the awkward way but it did work very well in my situation where I had multiple boxes of varying sizes.

0
On

In my experience, the best way to deal with this is not to worry about whether it will fit by trying to measure.

  1. Set the rectangle you are writing into to span all the way from your current position to the bottom of the media box.
  2. Write the text into the rectangle and then check if the text was truncated. If it was truncated, then delete the object you just wrote. If not, then all is good and you can find the extent of the text you did write.
  3. If it didn't fit, move-to/create the next page, set the rectangle to the full page and re-try the text write.
  4. If that gets truncated, then you have a problem, because your text will never fit on a single page without decreasing the font-size, line spacing or something else.

It's not insurmountable, because you can chain text across pages, but that's a different question.

0
On

Yep, ABCPDF will tell you this information: yourPdfDocInstance.AddHtml() will return 0 (and not add any text) if text could not be added to the current rectangle