pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Roberto Nibali <rnib...@gmail.com>
Subject Re: Migrate form field entries from one pdf to another
Date Mon, 06 Jul 2015 15:57:39 GMT
Hi

Sorry for the late reply, I was travelling for business.

> I did a quick test with a newly created form using Adobe Acrobat and
> setting the checkbox also with Acrobat. There the value is not null when
> the checkbox has been checked.
> >
> > I have attached now PDFs, where with my tool the value is null.
>
>
> unfortunately the attachments didn't make it through the mailing list.
> Could you upload them to a public location?
>

It took me a while, before I realized that google offers the technology I
need:

https://drive.google.com/file/d/0B7Bzk_1dcyc5SmRpQUJPR3JGUkk/view?usp=sharing
https://drive.google.com/file/d/0B7Bzk_1dcyc5Tk1qcVo2Yk02dTA/view?usp=sharing

Those are the files I tried to send to this mailing list earlier.


> >
> > > How could I deal with this? Because this is exactly what seems to fail
> and
> > > also cause this dreaded exception message when trying to fill out the
> forms
> > > with anything other than PDTextbox.
> >
> > Without looking at the form:
> >
> > a) test if getValue returns null if not take that value
> > b) if it returns null test if the box has been checked - if yes take
> that value.
> >
> > Which value?
> >
> > use the value retrieved from a) or b) to set the fields value in the pdf
> template.
> >
> > I'm not sure which value you mean.
> >
> > What would be helpful is either a screenshot of the form fields entries
> using the PDFDebugger [
> http://pdfbox.apache.org/1.8/commandline.html#pdfDebugger <
> http://pdfbox.apache.org/1.8/commandline.html#pdfDebugger>] or the
> printout of the fields getDictionary() method so there is some more
> information about how the field definition looks lie. Best would be to have
> the form of course.
>
>
I haven't found the pdfDebugger tool yet; reckon I need to compile it
myself. Nevertheless, when I parse through the structure myself, I do not
get any dictionary entries:

DEBUG: Opening ./Test.pdf
No XFA data in stream
DEBUG: Checkbox [01.20.Entry1]:  On=1 Off=Off Checked=true Value=1
DEBUG: Checkbox [01.20.Entry2]:  On=1 Off=Off Checked=true Value=1
DEBUG: Checkbox [01.20.Entry3]:  On=1 Off=Off Checked=false Value=_n/a_
DEBUG: Checkbox [01.20.Entry4]:  On=2 Off=Off Checked=false Value=_n/a_
DEBUG: TextButton [01.011.Name]: Value=sddsds
DEBUG: TextButton [01.011.Prename]: Value=sdsdsd
DEBUG: Checkbox [01.011.Language]:  On=0 Off=Off Checked=false Value=_n/a_
DEBUG: Checkbox [01.011.Boxes]:  On=Mrs Off=Off Checked=true Value=_n/a_
DEBUG: Opening ./TestTemplate.pdf
No XFA data in stream
Setting CheckBox field: 01.011.Boxes to value: null
Dumping Checkbox field dictionary [01.011.Boxes] ----------
COSDictionary{}
----------------------------------------------------------------------------
Setting CheckBox field: 01.20.Entry4 to value: null
Dumping Checkbox field dictionary [01.20.Entry4] ----------
COSDictionary{}
----------------------------------------------------------------------------
Setting CheckBox field: 01.20.Entry3 to value: null
Dumping Checkbox field dictionary [01.20.Entry3] ----------
COSDictionary{}
----------------------------------------------------------------------------
Setting CheckBox field: 01.20.Entry2 to value: null
Dumping Checkbox field dictionary [01.20.Entry2] ----------
COSDictionary{}
----------------------------------------------------------------------------
Setting CheckBox field: 01.011.Language to value: null
Dumping Checkbox field dictionary [01.011.Language] ----------
COSDictionary{}
----------------------------------------------------------------------------
Setting CheckBox field: 01.20.Entry1 to value: null
Dumping Checkbox field dictionary [01.20.Entry1] ----------
COSDictionary{}

The relevant DEBUG code:

private void analyseAndPrintFields(PDField field) throws IOException {
    String fqName = field.getFullyQualifiedName();
    String value = (field.getValue() != null ? field.getValue() : "_n/a_");

    if (field instanceof PDCheckbox) {
        PDCheckbox checkbox = (PDCheckbox) field;
        logerr("DEBUG: Checkbox [" + fqName + "]:  On=" +
checkbox.getOnValue() +
                " Off=" + checkbox.getOffValue() +
                " Checked=" + (checkbox.isChecked() ? "true" : "false") +
                " Value=" + value);
        //TODO: Check if widgets handling is necessary: checkbox.getWidget();
    } else if (field instanceof PDRadioCollection) {
        PDRadioCollection collection = (PDRadioCollection) field;
        logerr("DEBUG: RadioButtons [" + fqName + "]: " +
                "CollectionValue=" + collection.getValue() +
                " Value=" + value);
    } else if (field instanceof PDPushButton) {
        PDPushButton button = (PDPushButton) field;
        logerr("DEBUG: Pushbuttons [" + fqName + "]: " +
                        "Export/Readonly/Required=" +
                        button.isNoExport() + "/" +
                        button.isReadonly() + "/" +
                        button.isRequired() +
                        " Value=" + value
        );
    } else if (field instanceof PDTextbox) {
        logerr("DEBUG: TextButton [" + fqName + "]: " +
                "Value=" + value);
    } else {
        logerr("DEBUG: Unhandled [" + fqName + "]: " +
                "Type=" + field.getClass().toString());
    }
}



And the dumping code:

    private void setFieldDC(PDDocument pdfDocument, String keyEntry,
PDField oldField) throws Exception {
        PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
        PDAcroForm pdAcroForm = docCatalog.getAcroForm();
        //TODO: Check if this makes sense: pdAcroForm.setCacheFields(true);
        PDField field = pdAcroForm.getField(keyEntry);

        if (field == null) {
            logerr("No field found with name: " + keyEntry);
            return;
        }

        String fieldValue;
        if (oldField instanceof PDTextbox) {
            fieldValue = oldField.getValue();
            if (fieldValue != null) {
                logmsg("Setting field: " + keyEntry + " to value: " +
fieldValue);
                field.setValue(fieldValue);
                if (setFieldFlags) {
                    field.setFieldFlags(oldField.getFieldFlags());
                }
            }
        } else if (oldField instanceof PDCheckbox) {
            fieldValue = oldField.getValue();
            logmsg("Setting CheckBox field: " + keyEntry + " to value:
" + fieldValue);
            if (fieldValue != null) {
                logmsg("Setting field: " + keyEntry + " to value: " +
fieldValue);
                field.setValue(fieldValue);
                if (setFieldFlags) {
                    field.setFieldFlags(oldField.getFieldFlags());
                }
            } else {
                logerr("Dumping Checkbox field dictionary [" +
keyEntry + "] ----------");
                logerr(oldField.getDictionary().toString());

logerr("----------------------------------------------------------------------------");
            }

/*            PDCheckbox oldCheckBox = (PDCheckbox) oldField;
            PDCheckbox newCheckBox = (PDCheckbox) field;

            if (oldCheckBox == null) {
                logerr("oldCheckBox is NULL");
            } else if (newCheckBox == null) {
                logerr("newCheckBox is NULL");
            }

            if (oldCheckBox.isChecked()) {
                logerr("DEBUG: >>>>> PDCheckBox [" + keyEntry + "]
wasChecked = YES");
                newCheckBox.check();
            } else {
                logerr("DEBUG: >>>>> PDCheckBox [" + keyEntry + "]
wasChecked = NO");
                newCheckBox.unCheck();
            }*/
        } else if (oldField instanceof PDChoiceField) {
            fieldValue = oldField.getValue();
            if (fieldValue != null) {
                field.setValue(fieldValue);
                if (setFieldFlags) {
                    field.setFieldFlags(oldField.getFieldFlags());
                }
            } else {
                logerr("Dumping PDChoiceField field dictionary [" +
keyEntry + "] ----------");
                logerr(oldField.getDictionary().toString());

logerr("----------------------------------------------------------------------------");
            }
        } else if (oldField instanceof PDRadioCollection) {
            fieldValue = oldField.getValue();
            if (fieldValue != null) {
                field.setValue(fieldValue);
                if (setFieldFlags) {
                    field.setFieldFlags(oldField.getFieldFlags());
                }
            } else {
                logerr("Dumping PDRadioCollection field dictionary ["
+ keyEntry + "] ----------");
                logerr(oldField.getDictionary().toString());

logerr("----------------------------------------------------------------------------");
            }
        } else if (oldField instanceof PDPushButton) {
            fieldValue = oldField.getValue();
            if (fieldValue != null) {
                field.setValue(fieldValue);
                if (setFieldFlags) {
                    field.setFieldFlags(oldField.getFieldFlags());
                }
            } else {
                logerr("Dumping PDPushButton field dictionary [" +
keyEntry + "] ----------");
                logerr(oldField.getDictionary().toString());

logerr("----------------------------------------------------------------------------");
            }
        }  else {
            logerr("Fields of type [" + oldField.getClass().toString()
+ "] are unsupported");
        }
    }


This is highly confusing. Why can Acrobat deal with those checkboxes when
their value is null and why can't PDFBox set Checkbox values?

How can I simply clone all static PDF form entries of a PDF into a new PDF?
Is PDF really that complex that such a simple thing is not possible? Right
now, only text form entries are copied, the rest shows null for getValue().

Cheers
Roberto

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message