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");
}
}
}
pdDoc.save(outputFile);
}
catch (IOException e)
{
e.printStackTrace();
throw new Exception("Error processing field [" + fieldName
+ "] " + e.getMessage());
}
} // end fillPDFForm
public static void main(String[] args)
{
String inputFile = "JMACTest.pdf";
String outputFile = "JMAC_PDFBOX_out.pdf";
Map<String, String> m = new HashMap();
m.put("Text1", "TEST Textfield valuE");
m.put("Check Box1", "true");
m.put("Dropdown1", "1");
m.put("List Box1", "1");
m.put("Group1", "RB1");
try
{
AcroFormTest.fillPDFForm(inputFile, outputFile, m, false);
}
catch (Exception e)
{
e.printStackTrace();
}
} // end main
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]