void extractAcroForm(PDDocument pdf) throws IOException, SAXException { // Thank you, Ben Litchfield, for org.apache.pdfbox.examples.fdf.PrintFields // this code derives from Ben's code PDDocumentCatalog catalog = pdf.getDocumentCatalog(); if (catalog == null) return; PDAcroForm form = catalog.getAcroForm(); if (form == null) return; // if it has xfa, try that. // if it doesn't exist or there's an exception, // go with traditional AcroForm PDXFAResource pdxfa = form.getXFA(); if (pdxfa != null) { // if successful, return XFAExtractor xfaExtractor = new XFAExtractor(); try (InputStream is = new BufferedInputStream(new ByteArrayInputStream(pdxfa.getBytes()))) { xfaExtractor.extract(is, xhtml, metadata, context); return; } catch (XMLStreamException | IOException e) { // if there was an xml parse exception in xfa, try the AcroForm } } @SuppressWarnings("rawtypes") List fields = form.getFields(); if (fields == null) return; @SuppressWarnings("rawtypes") ListIterator itr = fields.listIterator(); if (itr == null) return; xhtml.startElement("div", "class", "acroform"); xhtml.startElement("ol"); while (itr.hasNext()) { Object obj = itr.next(); if (obj != null && obj instanceof PDField) { processAcroField((PDField) obj, 0); } } xhtml.endElement("ol"); xhtml.endElement("div"); }
public static PDSignatureField swapTextWithSignature(PDField box, List<PDField> fields) { PDSignatureField sig = null; try { PDAcroForm acroForm; acroForm = box.getAcroForm(); acroForm.getDictionary().setDirect(true); PDSignature sigobj = new PDSignature(); sig = new PDSignatureField(acroForm); sig.getDictionary().setDirect(true); sig.setSignature(sigobj); sig.getWidget().setPage(box.getWidget().getPage()); sig.setPartialName(box.getPartialName()); sig.setAlternateFieldName(box.getAlternateFieldName()); sig.getDictionary().setItem(COSName.RECT, box.getDictionary().getItem(COSName.RECT)); sig.getDictionary().setNeedToBeUpdate(true); acroForm.getDictionary().setInt(COSName.SIG_FLAGS, 3); acroForm.getDictionary().setNeedToBeUpdate(true); fields.remove(box); fields.add(sig); } catch (IOException ioe) { } return sig; }