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

The following examples show how to use org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm#getField() . 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: ReadForm.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/36964496/pdfbox-2-0-overcoming-dictionary-key-encoding">
 * PDFBox 2.0: Overcoming dictionary key encoding
 * </a>
 * <br/>
 * <a href="http://www.stockholm.se/PageFiles/85478/KYF%20211%20Best%C3%A4llning%202014.pdf">
 * KYF 211 Best&auml;llning 2014.pdf
 * </a>
 * 
 * <p>
 * Indeed, the special characters in the names are replaced by the Unicode replacement
 * character. PDFBox, when parsing a PDF name, immediately interprets its bytes as UTF-8
 * encoded which fails in the document at hand.
 * </p>
 */
@Test
public void testReadFormOptions() throws IOException
{
    try (   InputStream originalStream = getClass().getResourceAsStream("KYF 211 Best\u00e4llning 2014.pdf") )
    {
        PDDocument pdfDocument = Loader.loadPDF(originalStream);
        PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm();
        
        PDField field = acroForm.getField("Krematorier");
        List<PDAnnotationWidget> widgets = field.getWidgets();
        System.out.println("Field Name: " + field.getPartialName() + " (" + widgets.size() + ")");
        for (PDAnnotationWidget annot : widgets) {
          PDAppearanceDictionary ap = annot.getAppearance();
          Set<COSName> keys = ((COSDictionary)(ap.getCOSObject().getDictionaryObject("N"))).keySet();
          ArrayList<String> keyList = new ArrayList<>(keys.size());
          for (COSName cosKey : keys) {keyList.add(cosKey.getName());}
          System.out.println(String.join("|", keyList));
        }
    }
}
 
Example 2
Source File: FillInForm.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/49048556/pdfbox-set-field-value-doesnt-work">
 * PDFBox set field value doesn't work
 * </a>
 * <br/>
 * <a href="https://www.inps.it/Nuovoportaleinps/image.aspx?iIDModulo=7712&tipomodulo=1">
 * SR16_ANF_DIP.pdf
 * </a>
 * <p>
 * Indeed, the form field in question is hidden. Thus, one has to un-hide it
 * to make it visible.
 * </p>
 */
@Test
public void testFillLikeBarbara() throws IOException
{
    try (   InputStream originalStream = getClass().getResourceAsStream("SR16_ANF_DIP.pdf") )
    {
        PDDocument pdfDocument = Loader.loadPDF(originalStream);
        PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm();

        if (acroForm != null)
        {
            PDTextField pdfField = (PDTextField) acroForm.getField("info_15a");
            pdfField.getWidgets().get(0).setHidden(false);// <===
            pdfField.setValue("xxxxxx");
        }

        pdfDocument.setAllSecurityToBeRemoved(true);
        COSDictionary dictionary = pdfDocument.getDocumentCatalog().getCOSObject();
        dictionary.removeItem(COSName.PERMS);

        pdfDocument.save(new File(RESULT_FOLDER, "SR16_ANF_DIP-filled.pdf"));
        pdfDocument.close();
    }
}
 
Example 3
Source File: FillInForm.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/52059931/pdfbox-setvalue-for-multiple-pdtextfield">
 * PDFBox setValue for multiple PDTextField
 * </a>
 * <br/>
 * <a href="https://ufile.io/z8jzj">
 * testform.pdf
 * </a>
 * <p>
 * Cannot reproduce the issue.
 * </p>
 */
@Test
public void testFillLikeJuvi() throws IOException {
    try (   InputStream originalStream = getClass().getResourceAsStream("testform.pdf") ) {
        PDDocument document = Loader.loadPDF(originalStream);
        PDDocumentCatalog docCatalog = document.getDocumentCatalog();
        PDAcroForm acroForm = docCatalog.getAcroForm();

        PDTextField field = (PDTextField) acroForm.getField("Check1");
        field.setValue("1111");

        PDTextField field2 = (PDTextField) acroForm.getField("Check2");
        field2.setValue("2222");

        PDTextField field3 = (PDTextField) acroForm.getField("HelloWorld");
        field3.setValue("HelloWorld");

        document.save(new File(RESULT_FOLDER, "testform-filled.pdf"));
        document.close();
    }
}
 
Example 4
Source File: FillInForm.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/54820224/why-is-pdfbox-overwriting-multiple-fields-even-when-they-dont-match-the-fullyq">
 * Why is PDFBox overwriting multiple Fields, even when they don't match the fullyQualifiedName? (Kotlin Android)
 * </a>
 * <br/>
 * <a href="http://www.kylevp.com/csOnePage.pdf">
 * csOnePage.pdf
 * </a>
 * <p>
 * The problem is due to some fields of the form sharing empty
 * appearance XObjects and PDFBox assuming in case of existing
 * appearance XObjects that it can simply update this existing
 * appearance instead of having to create a new one from scratch.
 * </p>
 * <p>
 * A work-around is to remove existing appearances from a field
 * before setting its value, see below.
 * </p>
 */
@Test
public void testLikeKyle() throws IOException {
    try (   InputStream originalStream = getClass().getResourceAsStream("csOnePage.pdf") )
    {
        PDDocument doc = Loader.loadPDF(originalStream);
        PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm();

        List<String> skillList = Arrays.asList("Athletics","Acrobatics","Sleight of Hand", "Stealth","Acrana", "History","Investigation","Nature", "Religion", "Animal Handling", "Insight", "Medicine", "Perception", "Survival", "Deception", "Intimidation", "Performance", "Persuasion");

        int temp = 0;
        for (String skill : skillList) {
            PDField field = acroForm.getField(skill);
            temp += 1;
            if (field == null) {
                System.err.printf("(%d) field '%s' is null.\n", temp, skill);
            } else {
                field.getCOSObject().removeItem(COSName.AP);
                field.setValue(String.valueOf(temp));
            }
        }

        doc.save(new File(RESULT_FOLDER, "csOnePage-filled.pdf"));
        doc.close();
    }
}
 
Example 5
Source File: SetRichText.java    From testarea-pdfbox2 with Apache License 2.0 5 votes vote down vote up
/**
 * <a href="https://stackoverflow.com/questions/54988511/acroform-field-setrichtextvalue-is-not-working">
 * acroform field.setRichTextValue is not working
 * </a>
 * <br/>
 * <a href="https://drive.google.com/open?id=1jFbsYGFOnx8EMiHgDsE8LQtfwJHSa5Gh">
 * form.pdf
 * </a> as "formBee.pdf"
 * <p>
 * After correction of a few details, the code kinda runs. In
 * particular it is necessary to use PDF style rich text (and
 * not LaTeX richtext package instructions), to set the flag
 * NeedAppearances to <code>true</code>, and to provide a V
 * value equal to the RV value without markup.
 * </p>
 * <p>
 * It only "kinda" runs because the OP also wants to flatten
 * the form which doesn't work as PDFBox does not create
 * appearance streams based on the rich text value.
 * </p>
 */
@Test
public void testFormBee() throws IOException {
    try (   InputStream resource = getClass().getResourceAsStream("formBee.pdf")) {
        PDDocument pdfDocument = Loader.loadPDF(resource);

        pdfDocument.getDocument().setIsXRefStream(true);
        PDAcroForm acroForm = pdfDocument.getDocumentCatalog().getAcroForm();
        acroForm.setNeedAppearances(true);

        acroForm.getField("tenantDataValue").setValue("Deuxième texte");
        acroForm.getField("tradingAddressValue").setValue("Text replacé");
        acroForm.getField("buildingDataValue").setValue("Deuxième texte");
        acroForm.getField("oldRentValue").setValue("750");
        acroForm.getField("oldChargesValue").setValue("655");
        acroForm.getField("newRentValue").setValue("415");
        acroForm.getField("newChargesValue").setValue("358");
        acroForm.getField("increaseEffectiveDateValue").setValue("Texte 3eme contenu");

        PDTextField field = (PDTextField) acroForm.getField("tableData");
        field.setRichText(true);
        //String val = "\\rtpara[size=12]{para1}{This is 12pt font, while \\span{size=8}{this is 8pt font.} OK?}";
        String val1 = "<?xml version=\"1.0\"?>"
                + "<body xfa:APIVersion=\"Acroform:2.7.0.0\" xfa:spec=\"2.1\" xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\">"
                + "<p dir=\"ltr\" style=\"margin-top:0pt;margin-bottom:0pt;font-family:Helvetica;font-size:12pt\">"
                + "This is 12pt font, while "
                + "<span style=\"font-size:8pt\">this is 8pt font.</span>"
                + " OK?"
                + "</p>"
                + "</body>";
        String val1Clean = "This is 12pt font, while this is 8pt font. OK?";
        //String val2 = "<body xmlns=\"http://www.w3.org/1999/xhtml\"><p style=\"color:#FF0000;\">Red&#13;</p><p style=\"color:#1E487C;\">Blue&#13;</p></body>";
        //String val2Clean = "Red\rBlue\r";
        field.setValue(val1Clean);
        field.setRichTextValue(val1);

        pdfDocument.save(new File(RESULT_FOLDER, "formBee-filled.pdf"));
    }
}
 
Example 6
Source File: FillInForm.java    From testarea-pdfbox2 with Apache License 2.0 4 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/36926060/apache-pdfbox-form-fill-truetype-text-spacing-issue">
 * Apache PDFBox Form Fill TrueType text spacing issue
 * </a>
 * <br>
 * <a href="https://www.dropbox.com/sh/b7ft1k0wfesob8s/AABnrTOgX26JlWCxl85jXns0a/FillFormField.pdf?dl=0">
 * FillFormField.pdf
 * </a>
 * <p>
 * Indeed, the issue can be reproduced, it is due to a combination of two factors:
 * </p>
 * <p>
 * <b>A quirk of PDFBox when writing text</b> - When writing text into a content stream,
 * PDFBox translates each Unicode codepoint into a name and looks up that name in a map
 * generating from the inverted font encoding. For some encodings, though, there are two
 * codes mapping to the name space, and the inverted map maps back to only one of them,
 * in the case at hand the non-breaking variant. As both are expected to be typographically
 * identical, this should not be a problem. But:
 * </p>
 * <p>
 * <b>Non-conformant font in the PDF</b> - The font Impact in the PDF is defined with
 * width 176 for the normal space glyph and 750 for the nonbreaking space glyph. Thus,
 * they typographically differ vehemently.
 * </p>
 */
@Test
public void testFillLikeRichardBrown() throws IOException
{
    try (   InputStream originalStream = getClass().getResourceAsStream("FillFormField.pdf") )
    {
        // load the documents
        PDDocument pdfDocument = Loader.loadPDF(originalStream);

        // 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)
        {
            PDTextField field = (PDTextField) acroForm.getField( "Title" );
            field.setValue("Low Mileage Beauty Kill");
        }

        // Save and close the filled out form.
        pdfDocument.save(new File(RESULT_FOLDER, "FillFormFieldRichardBrown.pdf"));
        pdfDocument.close();
    }
}