JudyZW <liwenzhou001 <at> yahoo.com> writes:
>
> What's the best way to determine if the field has appearance?
>
> Judy
>
The quick/none-too-reliable way is to check the AcroFields dictionary for a
NeedAppearances flag.
The reliable/hard/slow(er) way to do it is to enumerate all the fields on the
form looking for appearances.
This code has never been compiled, much less run, but none the less, here goes:
//Quick-n-dirty:
boolean hasAppearancesDirty( PdfReader reader ) {
PdfDictionary root = reader.getCatalog();
PdfDictionary form = root.getAsDict( PdfName.ACROFORM );
PdfBoolean needsAp = (form != null) ? form.getAsBoolean(
PdfName.NEEDAPPEARANCES ) : null;
boolean hasApp = needsAp == null || !needsAp.booleanValue();
return hasApp;
}
//Not-so-quick-or-dirty:
boolean hasAppearancesPrecise( PdfReader reader ) {
int fieldCount = 0;
int fieldsWithAps = 0;
AcroFields fields = reader.getAcroFields();
HashMap itemMap<String, AcroFields.Item> = fields.getFields();
for (String name : itemMap.keySet()) {
AcroFields.Item curItem = itemMap.get( name );
int count = curItem.size();
for (int i = 0; i < count; ++i) {
++fieldCount;
PdfDictionary merged = curItem.getMerged( i );
// this test could use some improvement
if (merged.contains( PdfName.AP )) {
++fieldsWithAps;
}
}
}
// as could this one
return fieldCount == fieldsWithAps;
}
Normally, a form without appearances will set the "NeedAppearances" flag,
instructing a form viewer to build those appearances. This is far easier than
doing it ones self (believe me), but leaves you open to one of the least-tested
features of Acrobat/Reader. Whee.
If you don't wish to depend on that (which is reasonably safe, but ya never
know), the other method checks every field in the document, and compares the
number with appearances to the number without. The simple presence of the /AP
dictionary might miss a case or two (radio buttons come to mind).
Hey, what do you expect for free? ;)
--Mark Storer
PDF Guy
------------------------------------------------------------------------------
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://www.1t3xt.com/docs/book.php