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

The following examples show how to use org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm#getFieldTree() . 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: PDDocument.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Retrieve all signature fields from the document.
 * 
 * @return a <code>List</code> of <code>PDSignatureField</code>s
 * @throws IOException if no document catalog can be found.
 */
public List<PDSignatureField> getSignatureFields() throws IOException
{
    List<PDSignatureField> fields = new ArrayList<PDSignatureField>();
    PDAcroForm acroForm = getDocumentCatalog().getAcroForm();
    if (acroForm != null)
    {
        for (PDField field : acroForm.getFieldTree())
        {
            if (field instanceof PDSignatureField)
            {
                fields.add((PDSignatureField)field);
            }
        }
    }
    return fields;
}
 
Example 2
Source File: DetermineWidgetPage.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/22074449/how-to-know-if-a-field-is-on-a-particular-page">
 * how to know if a field is on a particular page?
 * </a>
 * <p>
 * This sample document does not contain the optional page entry in its annotations.
 * Thus, the fast method fails in contrast to the safe one.
 * </p>
 */
@Test
public void testAFieldTwice() throws IOException
{
    System.out.println("aFieldTwice.pdf\n=================");
    try (   InputStream resource = getClass().getResourceAsStream("aFieldTwice.pdf")    )
    {
        PDDocument document = Loader.loadPDF(resource);
        PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
        if (acroForm != null)
        {
            for (PDField field : acroForm.getFieldTree())
            {
                System.out.println(field.getFullyQualifiedName());
                for (PDAnnotationWidget widget : field.getWidgets())
                {
                    System.out.print(widget.getAnnotationName() != null ? widget.getAnnotationName() : "(NN)");
                    System.out.printf(" - fast: %s", determineFast(document, widget));
                    System.out.printf(" - safe: %s\n", determineSafe(document, widget));
                }
            }
        }
    }
    System.out.println();
}
 
Example 3
Source File: DetermineWidgetPage.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/22074449/how-to-know-if-a-field-is-on-a-particular-page">
 * how to know if a field is on a particular page?
 * </a>
 * <p>
 * This sample document contains the optional page entry in its annotations.
 * Thus, the fast method returns the same result as the safe one.
 * </p>
 */
@Test
public void testTestDuplicateField2() throws IOException
{
    System.out.println("test_duplicate_field2.pdf\n=================");
    try (   InputStream resource = getClass().getResourceAsStream("test_duplicate_field2.pdf")    )
    {
        PDDocument document = Loader.loadPDF(resource);
        PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
        if (acroForm != null)
        {
            for (PDField field : acroForm.getFieldTree())
            {
                System.out.println(field.getFullyQualifiedName());
                for (PDAnnotationWidget widget : field.getWidgets())
                {
                    System.out.print(widget.getAnnotationName() != null ? widget.getAnnotationName() : "(NN)");
                    System.out.printf(" - fast: %s", determineFast(document, widget));
                    System.out.printf(" - safe: %s\n", determineSafe(document, widget));
                }
            }
        }
    }
    System.out.println();
}
 
Example 4
Source File: ShowFormFieldNames.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/39574021/how-can-the-internal-labels-of-the-editable-fields-in-an-acroform-pdf-be-found">
 * How can the internal labels of the editable fields in an acroform .pdf be found and listed?
 * </a>
 * <p>
 * This method retrieves the form field names from the given {@link PDDocument}. 
 * </p>
 */
List<String> getFormFieldNames(PDDocument pdDocument)
{
    PDAcroForm pdAcroForm = pdDocument.getDocumentCatalog().getAcroForm();
    if (pdAcroForm == null)
        return Collections.emptyList();

    List<String> result = new ArrayList<>();
    for (PDField pdField : pdAcroForm.getFieldTree())
    {
        if (pdField instanceof PDTerminalField)
        {
            result.add(pdField.getFullyQualifiedName());
        }
    }
    return result;
}
 
Example 5
Source File: FillInForm.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/56938135/pdfbox-inconsistent-pdtextfield-autosize-behavior-after-setvalue">
 * PDFBox Inconsistent PDTextField Autosize Behavior after setValue
 * </a>
 * <br/>
 * <a href="http://www.filedropper.com/0postfontload">
 * 0.pdf
 * </a>
 * <p>
 * Indeed, some fields look weird after fill-in; for some fields
 * this is due to weird pre-existing appearance streams. These can
 * be fixed as in {@link #testFill0DropOldAppearance()}.
 * </p>
 * @see #testFill0DropOldAppearance()
 * @see #testFill0DropOldAppearanceNoCombNoMax()
 * @see #testFill0DropOldAppearanceNoCombNoMaxNoMultiLine()
 */
@Test
public void testFill0LikeXenyal() throws IOException {
    try (   InputStream originalStream = getClass().getResourceAsStream("0.pdf");
            InputStream fontStream = getClass().getResourceAsStream("Lato-Regular.ttf"))
    {
        PDDocument doc = Loader.loadPDF(originalStream);
        PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();

        PDType0Font font = PDType0Font.load(doc, fontStream, false);
        String font_name = acroForm.getDefaultResources().add(font).getName();

        for (PDField field : acroForm.getFieldTree()) {
            if (field instanceof PDTextField) {
                PDTextField textField = (PDTextField) field;
                textField.setDefaultAppearance(String.format("/%s 0 Tf 0 g", font_name));
                textField.setValue("Test");
            }
        }
        

        doc.save(new File(RESULT_FOLDER, "0-filledLikeXenyal.pdf"));
        doc.close();
    }        
}
 
Example 6
Source File: FillInForm.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/56938135/pdfbox-inconsistent-pdtextfield-autosize-behavior-after-setvalue">
 * PDFBox Inconsistent PDTextField Autosize Behavior after setValue
 * </a>
 * <br/>
 * <a href="http://www.filedropper.com/0postfontload">
 * 0.pdf
 * </a>
 * <p>
 * Removing the old appearance streams before setting the new field
 * values removes the compression of the Resident Name and Care
 * Providers Address fields. In the latter case, though, the lower
 * part of the field value now is cut off.
 * </p>
 * <p>
 * For some fields only the first two letters are visible. This is
 * due to them being two character comb fields. These can changed
 * as in {@link #testFill0DropOldAppearanceNoCombNoMax()}.
 * </p>
 * @see #testFill0LikeXenyal()
 * @see #testFill0DropOldAppearanceNoCombNoMax()
 * @see #testFill0DropOldAppearanceNoCombNoMaxNoMultiLine()
 */
@Test
public void testFill0DropOldAppearance() throws IOException {
    try (   InputStream originalStream = getClass().getResourceAsStream("0.pdf");
            InputStream fontStream = getClass().getResourceAsStream("Lato-Regular.ttf"))
    {
        PDDocument doc = Loader.loadPDF(originalStream);
        PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();

        PDType0Font font = PDType0Font.load(doc, fontStream, false);
        String font_name = acroForm.getDefaultResources().add(font).getName();

        for (PDField field : acroForm.getFieldTree()) {
            if (field instanceof PDTextField) {
                PDTextField textField = (PDTextField) field;
                textField.setDefaultAppearance(String.format("/%s 0 Tf 0 g", font_name));
                textField.getWidgets().forEach(w -> w.getAppearance().setNormalAppearance((PDAppearanceEntry)null));
                textField.setValue("Test");
            }
        }
        

        doc.save(new File(RESULT_FOLDER, "0-filledDropOldAppearance.pdf"));
        doc.close();
    }        
}
 
Example 7
Source File: FillInForm.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/56938135/pdfbox-inconsistent-pdtextfield-autosize-behavior-after-setvalue">
 * PDFBox Inconsistent PDTextField Autosize Behavior after setValue
 * </a>
 * <br/>
 * <a href="http://www.filedropper.com/0postfontload">
 * 0.pdf
 * </a>
 * <p>
 * Resetting the comb flags and removing maximum lengths fixes the
 * appearance of the fields in which only the first two letters were
 * visible.
 * </p>
 * <p>
 * The problem of the lower part of the field value being cut off in
 * the Care Providers Address fields is due to PDFBox in case of 
 * multi line text fields using a fixed font height and not fine
 * tuning the vertical position of the field contents. In the case
 * at hand this can be fixed as in {@link #testFill0DropOldAppearanceNoCombNoMaxNoMultiLine()}.
 * </p>
 * @see #testFill0LikeXenyal()
 * @see #testFill0DropOldAppearance()
 * @see #testFill0DropOldAppearanceNoCombNoMaxNoMultiLine()
 */
@Test
public void testFill0DropOldAppearanceNoCombNoMax() throws IOException {
    final int FLAG_COMB = 1 << 24;

    try (   InputStream originalStream = getClass().getResourceAsStream("0.pdf");
            InputStream fontStream = getClass().getResourceAsStream("Lato-Regular.ttf"))
    {
        PDDocument doc = Loader.loadPDF(originalStream);
        PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();

        PDType0Font font = PDType0Font.load(doc, fontStream, false);
        String font_name = acroForm.getDefaultResources().add(font).getName();

        for (PDField field : acroForm.getFieldTree()) {
            if (field instanceof PDTextField) {
                PDTextField textField = (PDTextField) field;
                textField.getCOSObject().removeItem(COSName.MAX_LEN);
                textField.getCOSObject().setFlag(COSName.FF, FLAG_COMB, false);;
                textField.setDefaultAppearance(String.format("/%s 0 Tf 0 g", font_name));
                textField.getWidgets().forEach(w -> w.getAppearance().setNormalAppearance((PDAppearanceEntry)null));
                textField.setValue("Test");
            }
        }
        

        doc.save(new File(RESULT_FOLDER, "0-filledDropOldAppearanceNoCombNoMax.pdf"));
        doc.close();
    }        
}
 
Example 8
Source File: FillInForm.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/56938135/pdfbox-inconsistent-pdtextfield-autosize-behavior-after-setvalue">
 * PDFBox Inconsistent PDTextField Autosize Behavior after setValue
 * </a>
 * <br/>
 * <a href="http://www.filedropper.com/0postfontload">
 * 0.pdf
 * </a>
 * <p>
 * By resetting the MultiLine flags, too, one eventually gets rid
 * of the problem of the lower part of the field value being cut
 * off in the Care Providers Address fields. This actually should
 * be considered an issue of PDFBox, though, not of the source PDF
 * here.
 * </p>
 * @see #testFill0LikeXenyal()
 * @see #testFill0DropOldAppearance()
 * @see #testFill0DropOldAppearanceNoCombNoMax()
 */
@Test
public void testFill0DropOldAppearanceNoCombNoMaxNoMultiLine() throws IOException {
    final int FLAG_MULTILINE = 1 << 12;
    final int FLAG_COMB = 1 << 24;

    try (   InputStream originalStream = getClass().getResourceAsStream("0.pdf");
            InputStream fontStream = getClass().getResourceAsStream("Lato-Regular.ttf"))
    {
        PDDocument doc = Loader.loadPDF(originalStream);
        PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();

        PDType0Font font = PDType0Font.load(doc, fontStream, false);
        String font_name = acroForm.getDefaultResources().add(font).getName();

        for (PDField field : acroForm.getFieldTree()) {
            if (field instanceof PDTextField) {
                PDTextField textField = (PDTextField) field;
                textField.getCOSObject().removeItem(COSName.MAX_LEN);
                textField.getCOSObject().setFlag(COSName.FF, FLAG_COMB | FLAG_MULTILINE, false);;
                textField.setDefaultAppearance(String.format("/%s 0 Tf 0 g", font_name));
                textField.getWidgets().forEach(w -> w.getAppearance().setNormalAppearance((PDAppearanceEntry)null));
                textField.setValue("Test");
            }
        }
        

        doc.save(new File(RESULT_FOLDER, "0-filledDropOldAppearanceNoCombNoMaxNoMultiLine.pdf"));
        doc.close();
    }        
}
 
Example 9
Source File: CheckImageFieldFilled.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
/** @see #testCheckAcroFormGsa500Pdf_v4() */
boolean isFieldFilledAcroForm(PDAcroForm acroForm, String fieldName) throws IOException {
    for (PDField field : acroForm.getFieldTree()) {
        if (field instanceof PDPushButton && fieldName.equals(field.getPartialName())) {
            for (final PDAnnotationWidget widget : field.getWidgets()) {
                WidgetImageChecker checker = new WidgetImageChecker(widget);
                if (checker.hasImages())
                    return true;
            }
        }
    }
    return false;
}
 
Example 10
Source File: ExtractAppearanceText.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/49427615/how-to-extract-label-text-from-push-button-using-apache-pdfbox">
 * How to extract label text from Push button using Apache PDFBox?
 * </a>
 * <p>
 * This method extracts the text from the normal appearance streams
 * of the form fields in the given PDF document.
 * </p>
 * @see #testBtn()
 * @see #testKYF211Beställning2014()
 */
public void showNormalFieldAppearanceTexts(PDDocument document) throws IOException {
    PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();

    if (acroForm != null) {
        SimpleXObjectTextStripper stripper = new SimpleXObjectTextStripper();

        for (PDField field : acroForm.getFieldTree()) {
            if (field instanceof PDTerminalField) {
                PDTerminalField terminalField = (PDTerminalField) field;
                System.out.println();
                System.out.println("* " + terminalField.getFullyQualifiedName());
                for (PDAnnotationWidget widget : terminalField.getWidgets()) {
                    PDAppearanceDictionary appearance = widget.getAppearance();
                    if (appearance != null) {
                        PDAppearanceEntry normal = appearance.getNormalAppearance();
                        if (normal != null) {
                            Map<COSName, PDAppearanceStream> streams = normal.isSubDictionary() ? normal.getSubDictionary() :
                                Collections.singletonMap(COSName.DEFAULT, normal.getAppearanceStream());
                            for (Map.Entry<COSName, PDAppearanceStream> entry : streams.entrySet()) {
                                String text = stripper.getText(entry.getValue());
                                System.out.printf("  * %s: %s\n", entry.getKey().getName(), text);
                            }
                        }
                    }
                }
            }
        }
    }
}