Hi,

> Am 28.06.2015 um 21:51 schrieb Roberto Nibali <[email protected]>:
> 
> Hi
> 
> Thanks for the quick reply. This is exactly the approach I wanted to take
> for my next option.
> 
> On Sun, Jun 28, 2015 at 8:54 PM, Tilman Hausherr <[email protected]>
> wrote:
> 
>> Here's some code I have from working on
>> https://issues.apache.org/jira/browse/PDFBOX-2249 with the file
>> JMACTest.pdf that is in that issue. While that issue was about listbox
>> controls, the PDF file JMACTest.pdf does also have some radio and checkbox
>> elements. Of the attached code, I tested changing "Check Box1" and "Group1"
>> and it worked as expected. What I can see is that PDCheckbox uses a
>> different approach than yours, so it may be worth a try. The other ones use
>> all the same approach, i.e. setValue().
>> 
>> What I would also suggest:
>> Take one of the things that don't work. Then open the "old" and the "new"
>> PDF with an editor like NOTEPAD++, search for the field name and look what
>> the differences are. (And have you verified that the template PDF does
>> really have the same field names, or the same type?)
>> 
>> You could of course, while waiting for our expert, try the 2.0 version
>> (see https://pdfbox.apache.org/2.0/getting-started.html ) and create a
>> 2nd project and try to see whether it gets better.
>> 
>> Tilman
>> 
>> 
>> package testpdfbox18;
>> 
>> 
>> import java.io.File;
>> import java.io.IOException;
>> import java.util.HashMap;
>> import java.util.Iterator;
>> import java.util.List;
>> import java.util.Map;
>> 
>> import org.apache.pdfbox.pdmodel.PDDocument;
>> import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
>> import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
>> import org.apache.pdfbox.pdmodel.interactive.form.PDCheckbox;
>> import org.apache.pdfbox.pdmodel.interactive.form.PDChoiceField;
>> import org.apache.pdfbox.pdmodel.interactive.form.PDField;
>> import org.apache.pdfbox.pdmodel.interactive.form.PDRadioCollection;
>> import org.apache.pdfbox.pdmodel.interactive.form.PDTextbox;
>> 
>> public class AcroFormTest
>> {
>> 
>>    public static void fillPDFForm(String inputFile, String outputFile,
>>            Map<String, String> fieldValues, boolean
>> ignoreUnknownFieldTypes) throws Exception
>>    {
>>        File myFile = new File(inputFile);
>>        PDDocument pdDoc;
>>        String fieldName = null;
>>        String fieldType;
>>        try
>>        {
>>            pdDoc = PDDocument.loadNonSeq(myFile, null);
>> 
>>            PDDocumentCatalog pdCatalog = pdDoc.getDocumentCatalog();
>>            PDAcroForm pdAcroForm = pdCatalog.getAcroForm();
>>            pdAcroForm.setCacheFields(true);
>>            List<PDField> l = pdAcroForm.getFields();
>>            Iterator<PDField> it = l.iterator();
>> 
>>            while (it.hasNext())
>>            {
>>                PDField f = it.next();
>>                fieldName = f.getFullyQualifiedName();
>>                fieldType = f.getClass().getSimpleName();
>>                System.out.println(fieldType);
>>                System.out.println(f.getClass().getName());
>>                String fieldValue;
>>                if (f instanceof PDTextbox)
>>                {
>>                    fieldValue = fieldValues.get(fieldName);
>>                    if (fieldValue != null)
>>                    {
>>                        f.setValue(fieldValue);
>>                    }
>> 
>>                } // end PDTextbox
>>                else if (f instanceof PDCheckbox)
>>                {
>>                    fieldValue = fieldValues.get(fieldName);
>>                    if (("TRUE".equalsIgnoreCase(fieldValue))
>>                            || ("CHECKED".equalsIgnoreCase(fieldValue))
>>                            || ("YES".equalsIgnoreCase(fieldValue)))
>>                    {
>>                        ((PDCheckbox) f).check();
>>                    }
>>                    else
>>                    {
>>                        ((PDCheckbox) f).unCheck();
>>                    }
>>                } // end PDCheckbox
>>                else if (f instanceof PDChoiceField)
>>                {
>>                    fieldValue = fieldValues.get(fieldName);
>>                    if (fieldValue != null)
>>                    {
>>                        f.setValue(fieldValue);
>>                    }
>>                } // PDChoiceField
>>                else if (f instanceof PDRadioCollection)
>>                {
>>                    fieldValue = fieldValues.get(fieldName);
>>                    if (fieldValue != null)
>>                    {
>>                        f.setValue(fieldValue);
>>                    }
>>                } // end PDRadioCollection
>>                else
>>                {
>>                    if (!ignoreUnknownFieldTypes)
>>                    {
>>                        throw new Exception("Fields of type [" + fieldType
>> + "] are unsupported");
>>                    }
>>                }
>> 
>>            }
>> 
> 
> However, I do get the same dreaded:
> 
> org.apache.pdfbox.pdmodel.interactive.form.PDRadioCollection cannot be cast
> to org.apache.pdfbox.pdmodel.interactive.form.PDCheckbox
> 
> as I get with the following code:
> 
>        HashMap<String, PDAcroForm> pdAcroFormMap = new HashMap<>();
>        @SuppressWarnings("unchecked")
>        List<PDField> oldFields =
> oldPDF.getDocumentCatalog().getAcroForm().getFields();
>        for (PDField pdField : oldFields) {
>            pdAcroFormMap.put(pdField.getFullyQualifiedName(),
> pdField.getAcroForm());
>            pdField.getAcroForm().exportFDF();
>        }
> 
>        @SuppressWarnings("unchecked")
>        List<PDField> templateFields =
> oldPDF.getDocumentCatalog().getAcroForm().getFields();
>        for (PDField pdField : templateFields) {
>            
> pdField.setAcroForm(pdAcroFormMap.get(pdField.getFullyQualifiedName()));
>        }
> 
> This is probably due to the class definitions of PDRadioCollection and
> PDCheckbox.

I'm not sure I understand what you are trying to achieve. 

Is ist that the template PDF has no form fields at all and you would like to 
add the fields from the source document to the template or would you like to 
get the values from the source and set the values of existing fields in the 
template PDF?

BR
Maruan



> 
> I'll keep investigating ...
> 
> Best regards
> Roberto


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to