/** * This will tell if the AcroForm is a dynamic XFA form. * * @return true if the AcroForm is a dynamic XFA form */ public boolean xfaIsDynamic() { return hasXFA() && getFields().isEmpty(); }
private FieldIterator(PDAcroForm form) { List<PDField> fields = form.getFields(); for (PDField field : fields) { enqueueKids(field); } }
/** * Generate a unique name for the signature. * * @return the signature's unique name */ private String generatePartialName() { String fieldName = "Signature"; Set<String> sigNames = new HashSet<>(); // fixme: this ignores non-terminal fields, so will miss any descendant signatures for (PDField field : getAcroForm().getFields()) { if(field instanceof PDSignatureField) { sigNames.add(field.getPartialName()); } } int i = 1; while(sigNames.contains(fieldName+i)) { ++i; } return fieldName+i; }
/** * This will print all the fields from the document. * * @param pdfDocument The PDF to get the fields from. * * @throws IOException If there is an error getting the fields. */ public void printFields(PDDocument pdfDocument) throws IOException { PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog(); PDAcroForm acroForm = docCatalog.getAcroForm(); List<PDField> fields = acroForm.getFields(); System.out.println(fields.size() + " top-level fields were found on the form"); for (PDField field : fields) { processField(field, "|--", field.getPartialName()); } }
// Load the pdfTemplate pdfTemplate = PDDocument.load(file); PDDocumentCatalog docCatalog = pdfTemplate.getDocumentCatalog(); PDAcroForm acroForm = docCatalog.getAcroForm(); // Get field names List<PDField> fieldList = acroForm.getFields(); // String the object array String[] fieldArray = new String[fieldList.size()]; int i = 0; for (PDField sField : fieldList) { fieldArray[i] = sField.getFullyQualifiedName(); i++; } // Loop through each field in the array and do something for (String f : fieldArray) { PDField field = acroForm.getField(f); System.out.println("f is: " + f); if (f.contains("EXAMPLE FORM FIELD NAME")) { DO SOMETHING String value = "example value"; field.setValue(value); System.out.println("printed: " + value + " to: " + f); } } // Save edited file pdfTemplate.save(sPdfTemplate); pdfTemplate.close();
@Override public void createAcroFormDictionary(PDAcroForm acroForm, PDSignatureField signatureField) throws IOException { @SuppressWarnings("unchecked") List<PDField> acroFormFields = acroForm.getFields(); COSDictionary acroFormDict = acroForm.getCOSObject(); acroForm.setSignaturesExist(true); acroForm.setAppendOnly(true); acroFormDict.setDirect(true); acroFormFields.add(signatureField); // WTF sylfaen? acroForm.setDefaultAppearance("/sylfaen 0 Tf 0 g"); pdfStructure.setAcroFormFields(acroFormFields); pdfStructure.setAcroFormDictionary(acroFormDict); LOG.info("AcroForm dictionary has been created"); }
/** * This will export all FDF form data. * * @return An FDF document used to export the document. * @throws IOException If there is an error when exporting the document. */ public FDFDocument exportFDF() throws IOException { FDFDocument fdf = new FDFDocument(); FDFCatalog catalog = fdf.getCatalog(); FDFDictionary fdfDict = new FDFDictionary(); catalog.setFDF(fdfDict); List<FDFField> fdfFields = new ArrayList<>(); List<PDField> fields = getFields(); for (PDField field : fields) { fdfFields.add(field.exportFDF()); } fdfDict.setID(document.getDocument().getDocumentID()); if (!fdfFields.isEmpty()) { fdfDict.setFields(fdfFields); } return fdf; }
List fields = form.getFields();
@Override public void validate(PreflightContext ctx) throws ValidationException { PDDocumentCatalog catalog = ctx.getDocument().getDocumentCatalog(); if (catalog != null) { PDAcroForm acroForm = catalog.getAcroForm(); if (acroForm != null) { checkNeedAppearences(ctx, acroForm); try { exploreFields(ctx, acroForm.getFields()); } catch (IOException e) { throw new ValidationException("Unable to get the list of fields : " + e.getMessage(), e); } } } else { ctx.addValidationError(new ValidationError(ERROR_SYNTAX_NOCATALOG, "There is no Catalog entry in the Document")); } }
page.getAnnotations().add(widget); acroForm.getFields().add(signatureField);
textBox.setPartialName("SampleField"); textBox.setDefaultAppearance(defaultAppearanceString); acroForm.getFields().add(textBox);
if (acroForm.getFields().isEmpty())
if (acroForm.getFields().isEmpty())
acroForm.getFields().add(radioButton);
List<PDField> acroFormFields = acroForm.getFields(); acroForm.getCOSObject().setDirect(true); acroForm.setSignaturesExist(true);
acroForm.getFields().add(checkbox);
acroForm.getFields().add(textBox);
acroForm.getFields().add(textBox);
PDSignatureField signatureField = new PDSignatureField(acroForm); PDAnnotationWidget widget = signatureField.getWidgets().get(0); List<PDField> acroFormFields = acroForm.getFields(); acroForm.setSignaturesExist(true); acroForm.setAppendOnly(true);