Detecting Overflow when adding text

Hello
With all the various methods you have in adding text to a page i do not see any built in mechanism to detect if the string applied to the relevant Rectangle is over flowing the bounds. So, if im an adding text to a part of a PDF that has specific boundaries i need to know if it overflows and iterate over font-size etc to make the text fit. I have done this in the past with DynamicPDF as they have an overflow flag, so i just presumed Aspose would have this too… If you do, i cannot seem to find it. If not could you point me to samples where this functionality is achieved?? I. am a fully paid up User of Aspose/Conholdate. Any help much appreciated

@paul.leamy

We have understood your concerns and will definitely look into it. Can you please also share a sample code snippet to show the approach that you are using specifically to add the text inside PDF? We will log an investigation ticket in our issue tracking system and share the ID with you.

I don’t really have code as I’m just investigating and will hand off to a dev at some stage… but I don’t want to hear back from them that it can’t be done :wink:

So here is some pseudo code that reflects what I’m trying to achieve… a bit blunt, but should give you an impression of what I’m trying to do

Create an instance of Document
Add a page to the Document
Set the size of the page
Add 2 images to the page
Add 3 blocks of text to the page
For each block of text:
Check if it fits on the page using the TextFragment’s IsOverflow method
If it doesn’t fit:
Reduce font size
Check again
If font size can’t be reduced:
Reduce kerning
Check again
If kerning can’t be reduced:
Reduce horizontal scale
Check again
If horizontal scale can’t be reduced:
Return false
Add the text to the page
Save the Document as a PDF file

@paul.leamy

We are checking it and will get back to you shortly.

Thanks

Don’t spend time in the algorithm of what to do when there is overflow…

Just a mechanism to detect it !

@paul.leamy

PDF is a complex format and text in it does not get added in paragraphs that go with flow. Nevertheless, we need to look into this matter further. We will be logging an investigation ticket and sharing the ID with you shortly.

Ok, thanks for your help… as I said, I have done this before when I use
We’d to use DynamicPDF… just as an fyi :grimacing:

@paul.leamy
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-53628

You can obtain Paid Support services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Thanks for looking into this Asad. If this becomes an urgent issue for me I will look into the paid support option.

What I take from this is that there is direct overflow method at the moment.

What I haven’t looked into is line counts in text fragments or paragraphs etc… perhaps there is some rudimentary calculations I can apply.

Thanks for your help

@paul.leamy

You can please keep exploring the API features meanwhile. In case you find any missing feature or issues, please feel free to let us know.

@paul.leamy

we added new options IsFitRectangle, where you can detect overfilling of the rectangle. Please, use following code snippet to check if text is overflowing boundaries of rectangle. Available in Aspose.PDF 23.07 which will be published during this month i.e. July 2023.

var doc = new Document();
var paragraphContent = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras nisl tortor, efficitur sed cursus in, lobortis vitae nulla. Quisque rhoncus, felis sed dictum semper, est tellus finibus augue, ut feugiat enim risus eget tortor. Nulla finibus velit nec ante gravida sollicitudin. Morbi sollicitudin vehicula facilisis. Vestibulum ac convallis erat. Ut eget varius sem. Nam varius pharetra lorem, id ullamcorper justo auctor ac. Integer quis erat vitae lacus mollis volutpat eget et eros. Donec a efficitur dolor. Maecenas non dapibus nisi, ut pellentesque elit. Sed pellentesque rhoncus ante, a consectetur ligula viverra vel. Integer eget bibendum ante. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Curabitur elementum, sem a auctor vulputate, ante libero iaculis dolor, vitae facilisis dolor lorem at orci. Sed laoreet dui id nisi accumsan, id posuere diam accumsan.";
var rectangle = new Rectangle(100, 600, 500, 700, false);
var paragraph = new TextParagraph();
var fragment = new TextFragment(paragraphContent);
paragraph.VerticalAlignment = VerticalAlignment.Top;
paragraph.FormattingOptions.WrapMode = TextFormattingOptions.WordWrapMode.ByWords;
paragraph.Rectangle = rectangle;
var isFitRectangle = fragment.TextState.IsFitRectangle(paragraphContent, rectangle);
while (!isFitRectangle)
{
      fragment.TextState.FontSize -= 0.5f;
      isFitRectangle = fragment.TextState.IsFitRectangle(paragraphContent, rectangle);
}
paragraph.AppendLine(fragment);
TextBuilder builder = new TextBuilder(doc.Pages.Add());
builder.AppendParagraph(paragraph);
doc.Save(output); 

The issues you have found earlier (filed as PDFNET-53628) have been fixed in Aspose.PDF for .NET 23.7.