/** * Retrieve all signature fields from the document. * * @return a <code>List</code> of <code>PDSignatureField</code>s */ public List<PDSignatureField> getSignatureFields() { List<PDSignatureField> fields = new ArrayList<>(); PDAcroForm acroForm = getDocumentCatalog().getAcroForm(); if (acroForm != null) { for (PDField field : acroForm.getFieldTree()) { if (field instanceof PDSignatureField) { fields.add((PDSignatureField)field); } } } return fields; }
private boolean shouldHandleXFAOnly(PDDocument pdDocument, PDFParserConfig config) { if (config.getIfXFAExtractOnlyXFA() && pdDocument.getDocumentCatalog() != null && pdDocument.getDocumentCatalog().getAcroForm() != null && pdDocument.getDocumentCatalog().getAcroForm().getXFA() != null) { return true; } return false; }
/** * 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();
/** * This will takes the values from the fdf document and import them into the * PDF document. * * @param pdfDocument The document to put the fdf data into. * @param fdfDocument The FDF document to get the data from. * * @throws IOException If there is an error setting the data in the field. */ public void importFDF( PDDocument pdfDocument, FDFDocument fdfDocument ) throws IOException { PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog(); PDAcroForm acroForm = docCatalog.getAcroForm(); acroForm.setCacheFields( true ); acroForm.importFDF( fdfDocument ); }
PDAcroForm form = pdf.getDocumentCatalog().getAcroForm(); if( form == null )
PDAcroForm form = pdf.getDocumentCatalog().getAcroForm(); if( form == null )
PDAcroForm destAcroForm = destCatalog.getAcroForm(); PDAcroForm srcAcroForm = srcCatalog.getAcroForm();
/** * This will takes the values from the fdf document and import them into the * PDF document. * * @param pdfDocument The document to put the fdf data into. * @param fdfDocument The FDF document to get the data from. * * @throws IOException If there is an error setting the data in the field. */ public void importFDF( PDDocument pdfDocument, FDFDocument fdfDocument ) throws IOException { PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog(); PDAcroForm acroForm = docCatalog.getAcroForm(); if (acroForm == null) { return; } acroForm.setCacheFields( true ); acroForm.importFDF( fdfDocument ); //TODO this can be removed when we create appearance streams acroForm.setNeedAppearances(true); }
private void initRectMap() { PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm(); if (acroForm == null) { return; } for (PDField field : acroForm.getFieldTree()) { String fullyQualifiedName = field.getFullyQualifiedName(); for (PDAnnotationWidget widget : field.getWidgets()) { if (page.equals(widget.getPage())) { rectMap.put(widget.getRectangle(), fullyQualifiedName); } } } }
public static void main(String[] args) throws IOException { String formTemplate = "src/main/resources/org/apache/pdfbox/examples/interactive/form/FillFormField.pdf"; try (PDDocument pdfDocument = PDDocument.load(new File(formTemplate))) { // get the document catalog PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm(); // as there might not be an AcroForm entry a null check is necessary if (acroForm != null) { // Retrieve an individual field and set its value. PDTextField field = (PDTextField) acroForm.getField( "sampleField" ); field.setValue("Text Entry"); // If a field is nested within the form tree a fully qualified name // might be provided to access the field. field = (PDTextField) acroForm.getField( "fieldsContainer.nestedSampleField" ); field.setValue("Text Entry"); } // Save and close the filled out form. pdfDocument.save("target/FillFormField.pdf"); } }
private PDSignature findExistingSignature(PDDocument doc, String sigFieldName) { PDSignature signature = null; PDSignatureField signatureField; PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm(); if (acroForm != null) { signatureField = (PDSignatureField) acroForm.getField(sigFieldName); if (signatureField != null) { // retrieve signature dictionary signature = signatureField.getSignature(); if (signature == null) { signature = new PDSignature(); // after solving PDFBOX-3524 // signatureField.setValue(signature) // until then: signatureField.getCOSObject().setItem(COSName.V, signature); } else { throw new IllegalStateException("The signature field " + sigFieldName + " is already signed."); } } } return signature; }
private void handleXFAOnly(PDDocument pdDocument, ContentHandler handler, Metadata metadata, ParseContext context) throws SAXException, IOException, TikaException { XFAExtractor ex = new XFAExtractor(); XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata); xhtml.startDocument(); try (InputStream is = new ByteArrayInputStream( pdDocument.getDocumentCatalog().getAcroForm().getXFA().getBytes())) { ex.extract(is, xhtml, metadata, context); } catch (XMLStreamException e) { throw new TikaException("XML error in XFA", e); } xhtml.endDocument(); }
return; PDAcroForm form = catalog.getAcroForm(); if (form == null) return;
@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")); } }
public static void main(String[] args) throws IOException { // Load the PDF document created by SimpleForm.java try (PDDocument document = PDDocument.load(new File("target/SimpleForm.pdf"))) { PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm(); // Get the field and the widget associated to it. // Note: there might be multiple widgets PDField field = acroForm.getField("SampleField"); PDAnnotationWidget widget = field.getWidgets().get(0); // Create the definition for a green border PDAppearanceCharacteristicsDictionary fieldAppearance = new PDAppearanceCharacteristicsDictionary(new COSDictionary()); PDColor green = new PDColor(new float[] { 0, 1, 0 }, PDDeviceRGB.INSTANCE); fieldAppearance.setBorderColour(green); // Set the information to be used by the widget which is responsible // for the visual style of the form field. widget.setAppearanceCharacteristics(fieldAppearance); document.save("target/AddBorderToField.pdf"); } } }
PDAcroForm acroForm = docCatalog.getAcroForm(); PDField field = acroForm.getField(name); if (field != null)
PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm(); if (acroForm != null && acroForm.getNeedAppearances())
PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();