if (dict == null || !dict.checkType(PdfName.ANNOT))
return;
PdfDictionary ap = dict.getAsDict(PdfName.AP);
if (ap == null)
return;
PdfNumber structParent = dict.getAsNumber(PdfName.STRUCTPARENT);
if (structParent == null)
return;
PdfStream stream = ap.getAsStream(PdfName.N);
if (stream == null)
return;
PdfIndirectReference xobjr = ap.getAsIndirectObject(PdfName.N);
if (xobjr == null)
return;
// remove the annotation from the page
for (int i = 0; i < annots.size(); i++) {
PdfIndirectReference annotref = annots.getAsIndirectObject(i);
if (item.getObjRef().getNumber() == annotref.getNumber()) {
annots.remove(i);
break;
}
}
// replace the existing attributes by a PrintField attribute
PdfDictionary attribute = new PdfDictionary();
attribute.put(PdfName.O, PdfName.PRINTFIELD);
PdfString description = dict.getAsString(PdfName.TU);
if (description == null)
description = dict.getAsString(PdfName.T);
if (PdfName.BTN.equals(dict.get(PdfName.FT))) {
PdfNumber fflags = dict.getAsNumber(PdfName.FF);
if (fflags != null) {
int ff = fflags.intValue();
if ((ff & PdfFormField.FF_PUSHBUTTON) != 0)
attribute.put(PdfName.ROLE, PdfName.PB);
// I don't think the condition below will ever be true
if ((ff & PdfFormField.FF_RADIO) != 0)
attribute.put(PdfName.ROLE, PdfName.rb);
else
attribute.put(PdfName.ROLE, PdfName.CB);
}
}
else {
attribute.put(PdfName.ROLE, PdfName.TV);
}
attribute.put(PdfName.DESC, description);
// Updating the values of the StructElem dictionary
PdfString t = structElem.getAsString(PdfName.T);
if (t == null || t.toString().trim().length() == 0)
structElem.put(PdfName.T, dict.getAsString(PdfName.T));
structElem.put(PdfName.A, attribute);
structElem.put(PdfName.S, PdfName.P);
structElem.put(PdfName.PG, pageref);
// Defining a new MCID
int mcid = items.processMCID(structParents, item.getRef());
LOGGER.info("Using MCID " + mcid);
structElem.put(PdfName.K, new PdfNumber(mcid));
// removing the annotation from the parent tree
items.removeFromParentTree(structParent);
// Adding the XObject to the page
PdfName xobj = new PdfName("XObj" + structParent.intValue());
LOGGER.info("Creating XObject with name " + xobj);