Java Code Examples for org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm#getFields()

The following examples show how to use org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm#getFields() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PDFParser.java    From document-management-software with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Extract the text from the form fields
 */
private void parseForm(PDDocument pdfDocument, StringBuffer content) throws IOException {
	PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
	PDAcroForm acroForm = docCatalog.getAcroForm();

	if (acroForm == null)
		return;

	content.append("\n");

	List<PDField> fields = acroForm.getFields();
	log.debug("{} top-level fields were found on the form", fields.size());

	for (PDField field : fields) {
		content.append(field.getPartialName());
		content.append(" = ");
		content.append(field.getValueAsString());
		content.append(" \n ");
	}
}
 
Example 2
Source File: PDVisibleSigBuilder.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@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);
    acroForm.setDefaultAppearance("/sylfaen 0 Tf 0 g");
    pdfStructure.setAcroFormFields(acroFormFields);
    pdfStructure.setAcroFormDictionary(acroFormDict);
    LOG.info("AcroForm dictionary has been created");
}
 
Example 3
Source File: FillAndFlatten.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/60964782/pdfbox-inconsistent-pdtextfield-behaviour-after-setvalue">
 * PDFBox Inconsistent PDTextField Behaviour after setValue
 * </a>
 * </br/>
 * <a href="https://s3-us-west-2.amazonaws.com/kx-filing-docs/b3-3.pdf">
 * b3-3.pdf
 * </a>
 * <p>
 * Indeed, PDFBox assumes in some fields that it should not create
 * field appearances which a formatting additional action would do
 * differently anyways in a viewer. In a flattening use case this is
 * obviously incorrect.
 * </p>
 * @see #testLikeAbubakarRemoveAction()
 */
@Test
public void testLikeAbubakar() throws IOException {
    try (   InputStream resource = getClass().getResourceAsStream("b3-3.pdf");
            PDDocument pdDocument = Loader.loadPDF(resource)    ) {
        PDDocumentCatalog catalog = pdDocument.getDocumentCatalog();
        PDAcroForm acroForm = catalog.getAcroForm();
        int i = 0;
        for (PDField field : acroForm.getFields()) {
            i=i+1;
            if (field instanceof PDTextField) {
                PDTextField textField = (PDTextField) field;
                textField.setValue(Integer.toString(i));
            }
        }

        pdDocument.getDocumentCatalog().getAcroForm().flatten();

        pdDocument.save(new File(RESULT_FOLDER, "b3-3-filled-and-flattened.pdf"));
    }
}
 
Example 4
Source File: FillAndFlatten.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/60964782/pdfbox-inconsistent-pdtextfield-behaviour-after-setvalue">
 * PDFBox Inconsistent PDTextField Behaviour after setValue
 * </a>
 * </br/>
 * <a href="https://s3-us-west-2.amazonaws.com/kx-filing-docs/b3-3.pdf">
 * b3-3.pdf
 * </a>
 * <p>
 * After removing the actions, PDFBox again sets appearances in
 * all fields.
 * </p>
 * @see #testLikeAbubakar()
 */
@Test
public void testLikeAbubakarRemoveAction() throws IOException {
    try (   InputStream resource = getClass().getResourceAsStream("b3-3.pdf");
            PDDocument pdDocument = Loader.loadPDF(resource)    ) {
        PDDocumentCatalog catalog = pdDocument.getDocumentCatalog();
        PDAcroForm acroForm = catalog.getAcroForm();
        int i = 0;
        for (PDField field : acroForm.getFields()) {
            i=i+1;
            if (field instanceof PDTextField) {
                PDTextField textField = (PDTextField) field;
                textField.setActions(null);
                textField.setValue(Integer.toString(i));
            }
        }

        pdDocument.getDocumentCatalog().getAcroForm().flatten();

        pdDocument.save(new File(RESULT_FOLDER, "b3-3-remove-action-filled-and-flattened.pdf"));
    }
}
 
Example 5
Source File: ListFormFields.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/44817793/the-method-getkids-is-undefined-for-the-type-pdfield">
 * The method getKids() is undefined for the type PDField
 * </a>
 * <br/>
 * <a href="https://issues.apache.org/jira/secure/attachment/12651245/field%20name%20test.pdf">
 * field name test.pdf
 * </a>
 * <p>
 * The problems referred to don't exist anymore.
 * </p>
 */
@Test
public void testListFieldsInFieldNameTest() throws InvalidPasswordException, IOException
{
    PDDocument doc = Loader.loadPDF(getClass().getResourceAsStream("field name test.pdf"));
    PDAcroForm form = doc.getDocumentCatalog().getAcroForm();
    List<PDField> fields = form.getFields();
    for (int i=0; i<fields.size(); i++) {
        PDField f = fields.get(i);
        if (f instanceof PDTerminalField)
        {
            System.out.printf("%s, %s widgets\n", f.getFullyQualifiedName(), f.getWidgets().size());
            for (PDAnnotationWidget widget : f.getWidgets())
                System.out.printf("  %s\n", widget.getAnnotationName());
        }
        else if (f instanceof PDNonTerminalField)
        {
            List<PDField> kids = ((PDNonTerminalField)f).getChildren();
            for (int j=0; j<kids.size(); j++) {
                if (kids.get(j) instanceof PDField) {
                    PDField kidField = (PDField) kids.get(j);
                    System.out.println(kidField.getFullyQualifiedName());
                }
            } 
        }
    }
}
 
Example 6
Source File: FillInForm.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/39720305/ufffd-is-not-available-in-this-fonts-encoding-winansiencoding">
 * U+FFFD is not available in this font's encoding: WinAnsiEncoding
 * </a>
 * <p>
 * The issue cannot be reproduced.
 * </p>
 */
@Test
public void testFillLikeStDdt() throws IOException
{
    try (   InputStream originalStream = getClass().getResourceAsStream("FillFormField.pdf") )
    {
        PDDocument pdfDocument = Loader.loadPDF(originalStream);
        PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm();

        if (acroForm != null)
        {
            List<PDField> fields = acroForm.getFields();
            for (PDField field : fields) {
                switch (field.getPartialName()) {
                    case "Title" /*"devices"*/:
                        field.setValue("Ger�t");
                        field.setReadOnly(true);
                        break;
                }
            }
            acroForm.flatten(fields, true);
        }

        pdfDocument.save(new File(RESULT_FOLDER, "FillFormFieldStDdt.pdf"));
        pdfDocument.close();
    }
}
 
Example 7
Source File: NativePdfBoxVisibleSignatureDrawer.java    From dss with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void draw() throws IOException {
	try (PDDocument doc = new PDDocument(); ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
		
		PDPage originalPage = document.getPage(parameters.getPage() - 1);
		SignatureFieldDimensionAndPositionBuilder dimensionAndPositionBuilder = 
				new SignatureFieldDimensionAndPositionBuilder(parameters, originalPage, pdFont);
		SignatureFieldDimensionAndPosition dimensionAndPosition = dimensionAndPositionBuilder.build();
		// create a new page
		PDPage page = new PDPage(originalPage.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);

           PDRectangle rectangle = getPdRectangle(dimensionAndPosition, page);
           widget.setRectangle(rectangle);
           
           PDStream stream = new PDStream(doc);
           PDFormXObject form = new PDFormXObject(stream);
           PDResources res = new PDResources();
           form.setResources(res);
           form.setFormType(1);
           
           form.setBBox(new PDRectangle(rectangle.getWidth(), rectangle.getHeight()));
           
           PDAppearanceDictionary appearance = new PDAppearanceDictionary();
           appearance.getCOSObject().setDirect(true);
           PDAppearanceStream appearanceStream = new PDAppearanceStream(form.getCOSObject());
           appearance.setNormalAppearance(appearanceStream);
           widget.setAppearance(appearance);
           
           try (PDPageContentStream cs = new PDPageContentStream(doc, appearanceStream))
           {
           	rotateSignature(cs, rectangle, dimensionAndPosition);
           	setFieldBackground(cs, parameters.getBackgroundColor());
           	setText(cs, dimensionAndPosition, parameters);
           	setImage(cs, doc, dimensionAndPosition, parameters.getImage());
           }
           
           doc.save(baos);
           
           try (ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()))
           {
       		signatureOptions.setVisualSignature(bais);
       		signatureOptions.setPage(parameters.getPage() - 1);
           }
           
       }
}