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.
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.