Am 22.01.2016 um 02:29 schrieb Adam Steen:
I have a working solution to set default values and have acrobat render them.
It was pure fluke that I got this result, but wanted to post it so other will
know and to close out the solution.
Thanks, this works nicely. You don't need the font and the appearance
streams contents (but it must exist) anymore, so the shorter code could
look like this. I've also changed the background. I'll keep that code
for the next person who asks :-)
public static void main(String[] args) throws IOException
{
String fileName = "C:\\Users\\Tilman
Hausherr\\Documents\\Java\\PDFBoxPageImageExtraction/RadioButtonsAdam.pdf";
String selectedValue = "a";
String name = "Radio";
PDDocument document = new PDDocument();
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
PDAcroForm acroForm = new PDAcroForm(document);
acroForm.setNeedAppearances(true);
document.getDocumentCatalog().setAcroForm(acroForm);
PDPageContentStream contents = new
PDPageContentStream(document, page);
List<String> options = Arrays.asList("a", "b", "c");
PDRadioButton radioButton = new PDRadioButton(acroForm);
radioButton.setPartialName("RadioButton");
List<PDAnnotationWidget> widgets = new ArrayList<>();
for (int i = 0; i < options.size(); i++)
{
PDRadioButton subRadioButton = new PDRadioButton(acroForm);
subRadioButton.setPartialName(name);
PDAnnotationWidget widget = subRadioButton.getWidgets().get(0);
PDAppearanceCharacteristicsDictionary fieldAppearance = new
PDAppearanceCharacteristicsDictionary(new COSDictionary());
fieldAppearance.setBorderColour(new PDColor(new float[]{0,
0, 0}, PDDeviceRGB.INSTANCE));
fieldAppearance.setBackground(new PDColor(new float[]{1, 1,
1}, PDDeviceRGB.INSTANCE));
widget.setRectangle(new PDRectangle(30, 800 - i * (21), 16,
16));
widget.setAppearanceCharacteristics(fieldAppearance);
widget.setPage(page);
widget.getCOSObject().setItem(COSName.PARENT, radioButton);
widget.setAnnotationFlags(4);
widget.setAppearanceState(options.get(i).equals(selectedValue) ?
selectedValue : "Off");
widget.setHighlightingMode("N");
PDAppearanceDictionary ap = new PDAppearanceDictionary();
COSDictionary aeDict = new COSDictionary();
COSStream off = new COSStream();
off.setItem(COSName.BBOX, new PDRectangle(0, 0, 16, 16));
aeDict.setItem("Off", off);
COSStream on = new COSStream();
on.setItem(COSName.BBOX, new PDRectangle(0, 0, 16, 16));
aeDict.setItem(options.get(i), on);
PDAppearanceEntry ae = new PDAppearanceEntry(aeDict);
ap.setNormalAppearance(ae);
widget.setAppearance(ap);
widgets.add(widget);
page.getAnnotations().add(widget);
}
radioButton.setWidgets(widgets);
System.out.println(radioButton.getOnValues());
radioButton.setValue(selectedValue);
acroForm.getFields().add(radioButton);
System.out.println(radioButton.getValue());
//acroForm.refreshAppearances(); // not implemented
contents.close();
document.save(new FileOutputStream(fileName));
document.close();
}
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@pdfbox.apache.org
For additional commands, e-mail: users-h...@pdfbox.apache.org