Hello,
I'm having trouble setting values for fields in a multipage PDF which I have
built from a single-page PDF including a form using PdfCopyFields. During
the concatenation of the pages I've renamed some fields using a stamper (I
have a name field in the form and concatenate pages and increment field name
if my data has more names than fits on one page). The resulting PDF opens
and works without problems in Acrobat, but when I try to set a value for the
fields using itext I get a ClassCastException.
First of all, this is what I'm doing:
ByteArrayOutputStream newdoc = new ByteArrayOutputStream();
PdfCopyFields copier = new PdfCopyFields(newdoc);
for (int i = 0; i<=count; ++i) {
PdfReader reader = new
PdfReader(this.template.toString());
ByteArrayOutputStream tempStream = new
ByteArrayOutputStream();
PdfStamper stamp = new PdfStamper(reader,
tempStream);
int numFields = 2;
if (i> 0) {
//increase counter which is the last char in
the field name string
AcroFields form = stamp.getAcroFields();
List fnames = new
ArrayList(form.getFields().keySet());
Collections.sort(fnames);
Iterator nIter = fnames.iterator();
while (nIter.hasNext()) {
//here I change the name string by
incrementing a number on it.
//the names are of the form
Formname.Fieldname0 and I increment the
//last number.
form.renameField(oldname, newname);
}
}
}
stamp.close();
PdfReader tempreader = new
PdfReader(tempStream.toByteArray());
copier.addDocument(tempreader);
}
copier.close();
Now with the newly constructed pdf I do the following to fill the form:
PdfReader reader =
new PdfReader(newdoc.toByteArray());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfStamper stamp =
new PdfStamper(reader, baos);
AcroFields form = stamp.getAcroFields();
//form.setGenerateAppearances(false);
i = vaHlot.iterator();
int c = 0;
while (i.hasNext()) {
Henkilo h = (Henkilo)i.next();
form.setField("TVastatakaussitoumus.vastatakauksenAntajat.nimi"+c,
h.getNimi());
c++;
}
...
Am I doing something wrong here or in the concatenation/renaming step?
The above form.setField() call is the one causing the problem. The Exception
is thrown in the AcroFields.getAppearance(PdfDictionary merged, String text)
method (line 306). The call
font.get(new PdfName((String)dab[DA_FONT]));
returns a PdfNull and an attempt is made to cast it to PRIndirectReference
which fails.
The above is true for version paulo-135. Itext 1.02b throws the
ClassCastException in the constructor of DocumentFont which is called as the
next thing done in AcroFields.getAppearance().
Setting AcroFields.setGenerateAppearances(false) before calling setField
solves the problem and the resulting PDF documents seem OK. What are the
consequences of not generating appearances in this case?
Thanks in advance for any help on this matter!
Best Regards,
Mika Majakorpi
-------------------------------------------------------
SF.Net email is sponsored by Shop4tech.com-Lowest price on Blank Media
100pk Sonic DVD-R 4x for only $29 -100pk Sonic DVD+R for only $33
Save 50% off Retail on Ink & Toner - Free Shipping and Free Gift.
http://www.shop4tech.com/z/Inkjet_Cartridges/9_108_r285
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions