Print Page | Close Window

How to determine if text will fit in memo

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: I need help - I can help
Forum Description: Problems and solutions while programming with the Debenu Quick PDF Library and Debenu PDF Viewer SDK
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=3568
Printed Date: 29 Apr 24 at 7:10AM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: How to determine if text will fit in memo
Posted By: waynefulcher
Subject: How to determine if text will fit in memo
Date Posted: 18 May 18 at 2:34AM
I have a memo field on a prebuilt PDF form. I want to determine if some text will fit in the memo when being printed.
Is there an easy way to do this? if not is there a long drawn out way to do this?
From my searching and writing test code there is no way to know if the text will all fit in a memo field.
My long drawn out way always comes up short due to a lack of information that I can't seem to get using QPDF library.

I perform the following steps:
1. Open the PDF file with the memo field.
2. Get the memo field's font, text size, width and height.
3. Call the SelectFont() method for the font ID used by memo 
4. SetTextSize to the TextSize used by memo field.
4. I then loop through text one word at a time calling GetTextWidth().
5. Once words no longer fit I add 1 to my line count and start over with the last word that didn't fit.
6. Once I am done I try to determine if the combined height of all lines would fit within the height of the memo.

There are several problems with trying to do this manually but to keep this post focused on a single issue I will only list the main problem.
There is no way to get the vertical line space a memo uses "between lines" so all I know that I can do to compute the height of all lines of text would be to do:
    totalHeight := (PDF.GetTextHeight * LineCount) ;
This results in a smaller number than it would if you added in line spacing.
I searched and there is no way to get this line spacing value as far as I can find.

Any help would be greatly appreciated.

Thanks
Wayne










Replies:
Posted By: rweetch
Date Posted: 18 May 18 at 8:49AM
would an approach like this help?

procedure TsmDebenuPDFLibraryHelper.DrawDownScaledText(const aText: string; const X, Y aWidth, aHeight: Double);
var lTextSize, lStartSize: Double;
begin

  lStartSize := GetTextSize;
  lTextSize := lStartSize;

  if GetWrappedTextHeight(aWidth, aText) > aHeight then
  begin
    
     lTextSize := lStartSize;

    repeat

       lTextSize := lTextSize * 0.95;

       SetTextSize(lTextSize);

       until (GetWrappedTextHeight(aWidth, aText) <= aHeight);

  end;

  DrawWrappedText(X, Y, aWidth, aText);

   if lTextSize < lStartSize then
      SetTextSize(lStartSize);

end;




Posted By: waynefulcher
Date Posted: 18 May 18 at 2:26PM
WOW, I must have spent 8 hours working on this problem yesterday and thought I read every method from the docs like 3 times..... Somehow I never saw the method GetWrappedTextHeight().
Dang that was easy, thanks for the pointer.




Posted By: rweetch
Date Posted: 18 May 18 at 3:21PM
there's just so much in there it's easy to miss the wood for the trees.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk