public static void main(String[] args) throws IOException try (PDDocument document = new PDDocument()) PDPage page = new PDPage(PDRectangle.A4); document.addPage(page); PDResources resources = new PDResources(); resources.put(COSName.getPDFName("Helv"), font); PDAcroForm acroForm = new PDAcroForm(document); document.getDocumentCatalog().setAcroForm(acroForm); acroForm.setDefaultResources(resources); acroForm.setDefaultAppearance(defaultAppearanceString); widget.setRectangle(rect); widget.setPage(page); page.getAnnotations().add(widget); acroForm.getFields().add(signatureField); document.save(args[0]);
PDPage page = getPage(startIndex); acroForm = new PDAcroForm(this); catalog.setAcroForm(acroForm); acroForm.getCOSObject().setNeedToBeUpdated(true); if (!(acroForm.getCOSObject().getDictionaryObject(COSName.FIELDS) instanceof COSArray)) acroForm.getCOSObject().setItem(COSName.FIELDS, new COSArray()); COSArray fieldArray = (COSArray) acroForm.getCOSObject().getDictionaryObject(COSName.FIELDS); fieldArray.setNeedToBeUpdated(true); signatureField = findSignatureField(acroForm.getFieldIterator(), sigObject); List<PDField> acroFormFields = acroForm.getFields(); acroForm.getCOSObject().setDirect(true); acroForm.setSignaturesExist(true); acroForm.setAppendOnly(true); boolean checkFields = checkSignatureField(acroForm.getFieldIterator(), signatureField); if (checkFields) List<PDAnnotation> annotations = page.getAnnotations(); page.setAnnotations(annotations); page.getCOSObject().setNeedToBeUpdated(true);
public static void main(String[] args) throws IOException try (PDDocument document = PDDocument.load(new File("target/SimpleForm.pdf"))) PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm(); PDField field = acroForm.getField("SampleField"); PDAnnotationWidget widget = field.getWidgets().get(0); if (resources != null) font = resources.getFont(fontName); font = acroForm.getDefaultResources().getFont(fontName);
@Override Object[][] getFlagBits() { PDAcroForm acroForm = new PDAcroForm(document, acroFormDictionary); return new Object[][]{ new Object[]{1, "SignaturesExist", acroForm.isSignaturesExist()}, new Object[]{2, "AppendOnly", acroForm.isAppendOnly()}, }; } }
@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"); }
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"); } } }
public static void main(String[] args) throws IOException try (PDDocument document = new PDDocument()) PDPage page = new PDPage(); document.addPage(page); PDAcroForm acroForm = new PDAcroForm(document); document.getDocumentCatalog().setAcroForm(acroForm); float y = page.getMediaBox().getHeight() - 50; widget.setPrinted(true); PDAppearanceCharacteristicsDictionary appearanceCharacteristics = new PDAppearanceCharacteristicsDictionary(new COSDictionary()); appearanceCharacteristics.setBorderColour(new PDColor(new float[]{1, 0, 0}, PDDeviceRGB.INSTANCE)); appearanceCharacteristics.setBackground(new PDColor(new float[]{1, 1, 0}, PDDeviceRGB.INSTANCE)); normalAppearanceDict.setItem(COSName.Off, createRadioButtonAppearanceStream(document, widget, false)); normalAppearanceDict.setItem(COSName.YES, createRadioButtonAppearanceStream(document, widget, true)); page.getAnnotations().add(checkbox.getWidgets().get(0)); acroForm.getFields().add(checkbox);
PDDocument doc = new PDDocument(); PDPage page = new PDPage(); doc.addPage(page); COSDictionary acroFormDict = new COSDictionary(); acroFormDict.setBoolean(COSName.getPDFName("NeedAppearances"), true); acroFormDict.setItem(COSName.getPDFName("Fields"), new COSArray()); doc.getDocumentCatalog().setAcroForm(acroForm); button.getWidget().setAppearanceCharacteristics(fieldAppearance); page.getAnnotations().add(button.getWidget()); acroForm.getFields().add(button); doc.save("/path");
private InputStream createVisualSignatureTemplate(PDDocument srcDoc, int pageNum, PDRectangle rect) throws IOException try (PDDocument doc = new PDDocument()) PDPage page = new PDPage(srcDoc.getPage(pageNum).getMediaBox()); doc.addPage(page); PDAcroForm acroForm = new PDAcroForm(doc); doc.getDocumentCatalog().setAcroForm(acroForm); PDSignatureField signatureField = new PDSignatureField(acroForm); PDAnnotationWidget widget = signatureField.getWidgets().get(0); List<PDField> acroFormFields = acroForm.getFields(); acroForm.setSignaturesExist(true); acroForm.setAppendOnly(true); acroForm.getCOSObject().setDirect(true); acroFormFields.add(signatureField); PDResources res = new PDResources(); form.setResources(res); form.setFormType(1); float height = bbox.getHeight(); Matrix initialScale = null; switch (srcDoc.getPage(pageNum).getRotation()) appearance.getCOSObject().setDirect(true); PDAppearanceStream appearanceStream = new PDAppearanceStream(form.getCOSObject()); appearance.setNormalAppearance(appearanceStream);
PDDocument doc = PDDocument.load(inputFile)) PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm(); PDRectangle rect = null; if (signature != null) rect = acroForm.getField(signatureFieldName).getWidgets().get(0).getRectangle(); if (doc.getVersion() >= 1.5f && accessPermissions == 0) if (acroForm != null && acroForm.getNeedAppearances()) if (acroForm.getFields().isEmpty()) acroForm.getCOSObject().removeItem(COSName.NEED_APPEARANCES);
try (PDDocument document = new PDDocument()) PDPage page1 = new PDPage(); PDPage page2 = new PDPage(); PDPage page3 = new PDPage(); document.addPage(page1); document.addPage(page2); document.addPage(page3); List<PDAnnotation> annotations = page1.getAnnotations(); PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm(); if (acroForm == null) acroForm = new PDAcroForm(document); document.getDocumentCatalog().setAcroForm(acroForm); PDResources dr = acroForm.getDefaultResources(); if (dr == null) dr = new PDResources(); acroForm.setDefaultResources(dr); dr.put(COSName.getPDFName("Helv"), PDType1Font.HELVETICA);
PDDocument pdfDoc = PDDocument.load(SOURCE); PDDocumentCatalog docCatalog = pdfDoc.getDocumentCatalog(); PDAcroForm acroForm = docCatalog.getAcroForm(); for (Object field : acroForm.getFields()) { if (field instanceof PDVariableText) { COSDictionary dict = ((PDField)field).getDictionary(); COSString defaultAppearance = (COSString) dict.getDictionaryObject(COSName.DA); if (defaultAppearance != null) dict.setString(COSName.DA, defaultAppearance.getString() + " 1 0 0 rg "); field = field instanceof PDTextbox ? new PDTextbox(acroForm, dict) : new PDChoiceField(acroForm, dict); ((PDField)field).setValue(VALUE); } } pdfDoc.save(TARGET); pdfDoc.close();
public static void main(String[] args) throws IOException try (PDDocument document = PDDocument.load(new File("target/SimpleForm.pdf"))) PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm(); PDField field = acroForm.getField("SampleField"); PDAnnotationWidget widget = field.getWidgets().get(0); field.getActions().getCOSObject().addAll(fieldActions.getCOSObject()); document.save("target/FieldTriggers.pdf");
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"); } }
PDDocument doc = new PDDocument(); PDPage page = new PDPage(); doc.addPage(page); PDAcroForm acroForm = new PDAcroForm(doc); doc.getDocumentCatalog().setAcroForm(acroForm); COSDictionary cosDict = new COSDictionary(); COSArray rect = new COSArray(); rect.add(new COSFloat(250f)); // lower x boundary rect.add(new COSFloat(75f)); // lower y boundary rect.add(new COSFloat(500f)); // upper x boundary rect.add(new COSFloat(125f)); // upper y boundary cosDict.setItem(COSName.RECT, rect); cosDict.setItem(COSName.FT, COSName.getPDFName("Tx")); // Field Type cosDict.setItem(COSName.TYPE, COSName.ANNOT); cosDict.setItem(COSName.SUBTYPE, COSName.getPDFName("Widget")); cosDict.setItem(COSName.T, new COSString("yourFieldName")); PDTextbox textField = new PDTextbox(acroForm, cosDict); acroForm.getFields().add(textField); page.getAnnotations().add(textField.getWidget());
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; }
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 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(); }
PDDocumentCatalog catalog = pdf.getDocumentCatalog(); PDAcroForm form = catalog.getAcroForm(); if (form == null) return; PDXFAResource pdxfa = form.getXFA(); List fields = form.getFields();