0
votes

I am using iText to analyse a pdf form and allow a user to change fieldnames and else of existing fields. I am able to display the possible values for a checkbox by calling getAppearanceStates(fieldname) on AcroFields. But I can't find a way to change the appearance state.

Has anyone ever done this or knows how to do it ?

For Example: I have a checkbox Field with name "checkbox"and the appearance states are "yes". I want it to be "on".

acroFields.setField("checkbox", "on", "on")

has no influence and setting the property doesn't seem to be correct either.

2
Did you try setField() or am I misinterpreting the question? - Bruno Lowagie
I did, see my edited question - user5103374

2 Answers

0
votes

Do I understand you correctly that you want to set the appearache state? The appearance state corresponds to the /AS key with values like /Off or /Yes. The values which you use must correspond to the appearance dictionaries you defined for you checkboxes while you can omit the /Off apperance. The name for the /Off appearance is fix but you can (at least up to PDF 1.7) define your own /On (or /Yes) appearance...

To set the appearance state with iText:

Item item = acroFields.getFieldItem("checkbox");
PdfDictionary dict = item.getWidget(0);
dict.put(PdfName.AS, PdfName.Off);
//dict.put(PdfName.AS, new PdfName("Yes"));
0
votes

I was able to solve it with the PdfDictionary. My Solution looks as follows:

PdfDictionary appearanceDictionary = (PdfDictionary) acroFields.getFieldItem("checkbox").getWidget(0).get(PdfName.AP);
PdfDictionary appearanceStateDictionary = (PdfDictionary) appearanceDictionary.get(PdfName.N);
PdfName oldAppearanceState = new PdfName("yes);
PdfName newAppearanceState = new PdfName("on");

PdfObject referenceOnAppearanceState = appearanceStateDictionary.get(oldAppearanceState);
appearanceStateDictionary.remove(oldAppearanceState);
appearanceStateDictionary.put(newAppearanceState, referenceOnAppearanceState);

I wasn't quite sure what hid behind the reference referenceOnAppearanceState but I didn't want to change it, I just wanted the text to change that is used to set the checkbox checked, so I removed it and added it with another PdfName.

Hope it helps someone else, too.