The underlying problem, as I understand it:
1) an AcroFields object is created when the PDF is opened.
2) newly created fields are not added to the existing AcroFields.
3) Flattening works on the information in the AcroFields.
All AcroFields.Item's modifier functions are package private, and there's no
way to add an Item from outside the class an.... Oh Yes There Is.
AcroFields.getFields just hands over its internal name->Item map, so if you
could construct the proper Item object, you could trivially insert it.
Potential solutions (if I'm right):
1)
PdfReader reader = new PdfReader(...);
PdfFormField formFieldDict = buildFormField(); // you'll have to manage that
one on your own
PRIndirectReference fieldRef = reader.addPdfObject(formFieldDict);
// add it to the page
PdfDictionary pageDict = reader.getPageN(pageNum);
PdfArray annots = pageDict.getAsArray(PdfName.ANNOTS);
if (annots == null) {
annots = new PdfArray();
pageDict.put(PdfName.ANNOTS, annots);
}
annots.add(fieldRef);
// add it to the doc-level field list
PdfDictionary root = reader.getCatalog();
PdfDictionary acroFrm = root.getAsDict(PdfName.ACROFORM);
PdfArray flds = acroFrm.getAsArray(PdfName.FIELDS);
flds.add(fieldRef); // just tack it on the end.
PdfStamper stamper = new PdfStamper(reader, outStream);
...
This method adds the field to the reader prior to the creation of the
AcroFields object, so that when it works its way through the PDF, it finds your
new field.
2) Modify your copy of the iText source to make the various
AcroFields.Item.add* function public and use them to create a new
AcroFields.Item and add it to the AcroFields' field map.
AcroFields.Item item = new AcroFields.Item();
item.addPage( pageNum );
item.addWidget( formFieldDict );
item.addWidgetRef( fieldRef );
item.addValue( formFieldDict );
item.addMerged( formFieldDict );
item.addTabOrder( fldDictsIndexInAnnotsArray );
acroFields.getFields().put(fldName, item);
--Mark Storer
Senior Software Engineer
Cardiff.com
import legalese.Disclaimer;
Disclaimer<Cardiff> DisCard = null;
________________________________
From: José Carrizo [mailto:c4rr...@gmail.com]
Sent: Wednesday, June 29, 2011 7:08 AM
To: itext-questions@lists.sourceforge.net
Subject: Re: [iText-questions] Why iText can not flatten new fields
Well, actually there is a case where I need to do it but does not
matter, you are right, I'll try to do it that way.
Thanks so much.
El jun 29, 2011 3:39 a.m., "1T3XT BVBA" <i...@1t3xt.info> escribió:
>
> On 29/06/2011 1:55, José Carrizo wrote:
>>
>> Hello there,
>>
>> I would like to know why the code documentation of iText says "The
fields added with addAnnotation(PdfAnnotation, int) will never be flattened".
>>
>> I need to do that, flatten a field that has been just created with
iText, whats the reason for that restriction.
>
>
> 1. A field that has been added to an existing PDF can be flattened in
a second pass.
> 2. It doesn't make sense to add a field, and then immediately flatten
it. Just add the content!
>
>
------------------------------------------------------------------------------
> All of the data generated in your IT infrastructure is seriously
valuable.
> Why? It contains a definitive record of application performance,
security
> threats, fraudulent activity, and more. Splunk takes this data and
makes
> sense of it. IT sense. And common sense.
> http://p.sf.net/sfu/splunk-d2d-c2
> _______________________________________________
> iText-questions mailing list
> iText-questions@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/itext-questions
>
> iText(R) is a registered trademark of 1T3XT BVBA.
> Many questions posted to this list can (and will) be answered with a
reference to the iText book: http://www.itextpdf.com/book/
> Please check the keywords list before you ask for examples:
http://itextpdf.com/themes/keywords.php
------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions
iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples:
http://itextpdf.com/themes/keywords.php