How can I tell if a freetext annotation contains more text than what's displayed?

58 views
Skip to first unread message

Aaron

unread,
Feb 28, 2017, 4:01:09 PM2/28/17
to pdfne...@googlegroups.com
Q:

I want to detect whenever a freetext annotation contains more text than what's displayed in its appearance, so that I can indicate that to my users.  How can I detect that?

A:

Code similar to the following should work for you:
Page first_page = doc.GetPage(1);
FreeText ft = FreeText.Create(doc, new Rect(10, 10, 100, 100));
String contents = "asdf asdf asdf asdf asdf";
int num_visible_chars = 0;
ft.SetContents(contents);
first_page.AnnotPushBack(ft);
ft.RefreshAppearance();
Obj app_stm = ft.GetAppearance();

if (app_stm != null)
ElementReader reader = new ElementReader(); 
reader.Begin(app_stm); 
Element element; 
while ((element = reader.Next()) != null) 
{
if (Element.Type.e_text != element.GetType()) continue;

Matrix2D ctm = element.GetCTM();
Matrix2D text_mtx = element.GetTextMatrix();
Matrix2D mtx = ctm * text_mtx;
double x, y;

for (CharIterator itr = element.GetCharIterator(); itr.HasNext(); itr.Next()) 
{
num_visible_chars++;
x = itr.Current().x;
y = itr.Current().y; 
mtx = ctm * text_mtx;
mtx.Mult(ref x, ref y);
Console.WriteLine("{3} Position of {2}: x={0:f} y={1:f}", x, y, itr.Current().char_code, num_visible_chars);
}
}

if (contents.Length > num_visible_chars) Console.WriteLine("It seems the contents are larger than what is displayed within the annotation");
reader.End();
}

Reply all
Reply to author
Forward
0 new messages