You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Travis Schneeberger (JIRA)" <ji...@apache.org> on 2017/04/12 14:03:41 UTC

[jira] [Created] (PDFBOX-3753) setting a RadioButton with export values does not update the appearance

Travis Schneeberger created PDFBOX-3753:
-------------------------------------------

             Summary: setting a RadioButton with export values does not update the appearance
                 Key: PDFBOX-3753
                 URL: https://issues.apache.org/jira/browse/PDFBOX-3753
             Project: PDFBox
          Issue Type: Bug
    Affects Versions: 2.0.5
            Reporter: Travis Schneeberger
             Fix For: 2.0.6


Setting a RadioButton with export values does not update the appearance.  The attached form has two sets of RadioButtons.  One called "Group1" and one called "_6_  Treatment of Program Inco_nwAbuWIn0JWsW9e68RWN8A".  "Group1" is easy to set or unset.  I noticed during debugging doesn't have any "ExportValues" and so the value is set in a different way than the other set of RadioButtons.  "_6_  Treatment of Program Inco_nwAbuWIn0JWsW9e68RWN8A" does have ExportValues and this appears to be related to the problems with setting the value.

For example:

{code}
        try(PDDocument pdfDocument = PDDocument.load(new File("/Users/travis/Desktop/fdpAttachment2.pdf"))) {

            final PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
            final PDAcroForm acroForm = docCatalog.getAcroForm();

        final PDRadioButton group1Field = (PDRadioButton) acroForm.getField("Group1");
            group1Field.setValue("NIH");

            final PDRadioButton topiField = (PDRadioButton) acroForm.getField("_6_  Treatment of Program Inco_nwAbuWIn0JWsW9e68RWN8A");
            topiField.setValue("Additive");


            pdfDocument.save("/Users/travis/Desktop/fdpAttachment2-radio-buttons.pdf");
        }
{code}

Notice in "fdpAttachment2-radio-buttons.pdf" that "Group1" RadioButton has NIH toggled while "_6_  Treatment of Program Inco_nwAbuWIn0JWsW9e68RWN8A" is not toggled even though "Additive" is a valid value.

The workaround for this is to set the appearance state (AS).  I'm still learning the pdfbox api so I apologize if my workaround is a little strange.

{code}
        try(PDDocument pdfDocument = PDDocument.load(new File("/Users/travis/Desktop/fdpAttachment2.pdf"))) {

            final PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
            final PDAcroForm acroForm = docCatalog.getAcroForm();

            final PDRadioButton group1Field = (PDRadioButton) acroForm.getField("Group1");
            group1Field.setValue("NIH");

            final PDRadioButton topiField = (PDRadioButton) acroForm.getField("_6_  Treatment of Program Inco_nwAbuWIn0JWsW9e68RWN8A");
            topiField.setValue("Additive");

            //Additive ends up being index 0.  If I add an AS with "0" it toggle the Additive radio button
            final int idx = topiField.getExportValues().indexOf((topiField).getValue());

            topiField.getWidgets().forEach(w -> {
                PDAppearanceEntry appearanceEntry = w.getAppearance().getNormalAppearance();

                if (((COSDictionary) appearanceEntry.getCOSObject()).containsKey(String.valueOf(idx))) {
                    w.getCOSObject().setName(COSName.AS, String.valueOf(idx));
                }
            });


            pdfDocument.save("/Users/travis/Desktop/fdpAttachment2-radio-buttons-workaround.pdf");
        }
{code}



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: dev-help@pdfbox.apache.org