// 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();
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"); } }
if (destAcroForm.getField(srcField.getFullyQualifiedName()) != null)
if (signature != null) rect = acroForm.getField(signatureFieldName).getWidgets().get(0).getRectangle();
private PDSignature findExistingSignature(PDAcroForm acroForm, String sigFieldName) { PDSignature signature = null; PDSignatureField signatureField; 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 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; }
/** * This method will import an entire FDF document into the PDF document * that this acroform is part of. * * @param fdf The FDF document to import. * * @throws IOException If there is an error doing the import. */ public void importFDF(FDFDocument fdf) throws IOException { List<FDFField> fields = fdf.getCatalog().getFDF().getFields(); if (fields != null) { for (FDFField field : fields) { FDFField fdfField = field; PDField docField = getField(fdfField.getPartialFieldName()); if (docField != null) { docField.importFDF(fdfField); } } } }
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"); } } }
PDField field = acroForm.getField(name); if (field != null)
PDField field = acroForm.getField("SampleField"); PDAnnotationWidget widget = field.getWidgets().get(0);
private void setFieldValueAndFlatten(PDAcroForm form, String fieldName, String fieldValue) throws IOException { PDField field = form.getField(fieldName); if(field != null){ field.setValue(fieldValue); field.setReadonly(true); } }
PDField field = acroForm.getField("SampleField"); PDAnnotationWidget widget = field.getWidgets().get(0);
public void check() throws Exception { PDDocument fdeb = null; fdeb = PDDocument.load( "C:\\Users\\34\\Desktop\\complaintform.pdf" ); PDAcroForm form = fdeb.getDocumentCatalog().getAcroForm(); PDField feld3 = form.getField("loan"); ((PDCheckbox) feld3).check(); fdeb.save("C:\\Users\\34\\Desktop\\complaintform.pdf"); fdeb.close(); }
public static void setFieldBold(String name, String value) throws IOException { PDDocumentCatalog docCatalog = _pdfDocument.getDocumentCatalog(); PDAcroForm acroForm = docCatalog.getAcroForm(); PDField field = acroForm.getField(name); COSDictionary dict = ((PDField) field).getDictionary(); COSString defaultAppearance = (COSString) dict .getDictionaryObject(COSName.DA); if (defaultAppearance != null) { dict.setString(COSName.DA, "/Helv 10 Tf 2 Tr .5 w 0 g"); if (name.equalsIgnoreCase("Field1")) { dict.setString(COSName.DA, "/Helv 12 Tf 0 g"); } } if (field instanceof PDTextbox) { field = new PDTextbox(acroForm, dict); ((PDField) field).setValue(value); } }
public static void setField(String name, String value, String fontName) throws IOException { PDDocumentCatalog docCatalog = _pdfDocument.getDocumentCatalog(); PDAcroForm acroForm = docCatalog.getAcroForm(); PDField field = acroForm.getField(name); COSDictionary dict = ((PDField) field).getDictionary(); COSString defaultAppearance = (COSString) dict .getDictionaryObject(COSName.DA); if (defaultAppearance != null) { dict.setString(COSName.DA, "/" + fontName + " 10 Tf 0 g"); if (name.equalsIgnoreCase("Field1")) { dict.setString(COSName.DA, "/" + fontName + " 12 Tf 0 g"); } } if (field instanceof PDTextbox) { field = new PDTextbox(acroForm, dict); ((PDField) field).setValue(value); } }
public class PDFBOX { public static void main(String[] args) throws IOException { PDDocument fdeb = null; File pdfFile = new File("C:\\Users\\pc\\Desktop\\Req-form.pdf"); fdeb = PDDocument.load(pdfFile); PDDocumentCatalog pdCatalog = fdeb.getDocumentCatalog(); PDAcroForm pdAcroForm = pdCatalog.getAcroForm(); // Text fields PDField firstName = pdAcroForm.getField("firstName"); firstName.setValue("firstName"); fdeb.save("C:\\Users\\Desktop\\Test.pdf"); fdeb.close(); } }
public static void main(String[] args) throws IOException { String formTemplate = "src/main/resources/org/apache/pdfbox/examples/interactive/form/FillFormField.pdf"; // load the document 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"); pdfDocument.close(); }
byte[] template = generateSimpleTemplate(); Files.write(new File(RESULT_FOLDER, "template.pdf").toPath(), template); try ( PDDocument finalDoc = new PDDocument(); ) { List<PDField> fields = new ArrayList<PDField>(); int i = 0; for (String value : new String[]{"eins", "zwei"}) { PDDocument doc = new PDDocument().load(new ByteArrayInputStream(template)); PDDocumentCatalog docCatalog = doc.getDocumentCatalog(); PDAcroForm acroForm = docCatalog.getAcroForm(); PDField field = acroForm.getField("SampleField"); field.setValue(value); field.setPartialName("SampleField" + i++); List<PDPage> pages = docCatalog.getAllPages(); finalDoc.addPage(pages.get(0)); fields.add(field); } PDAcroForm finalForm = new PDAcroForm(finalDoc); finalDoc.getDocumentCatalog().setAcroForm(finalForm); finalForm.setFields(fields); finalDoc.save(new File(RESULT_FOLDER, "form-two-templates.pdf")); }
/** * This method will import an entire FDF document into the PDF document * that this acroform is part of. * * @param fdf The FDF document to import. * * @throws IOException If there is an error doing the import. */ public void importFDF(FDFDocument fdf) throws IOException { List<FDFField> fields = fdf.getCatalog().getFDF().getFields(); if (fields != null) { for (FDFField field : fields) { FDFField fdfField = field; PDField docField = getField(fdfField.getPartialFieldName()); if (docField != null) { docField.importFDF(fdfField); } } } }
public static void main(String[] args) throws IOException { // Load the PDF document created by SimpleForm.java 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"); document.close(); } }