pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Adam Steen <ad...@rmt.com.au>
Subject How do you create a Radio Button Group with PDFBox 2.0?
Date Wed, 20 Jan 2016 00:26:50 GMT
Hi



I want to create a Radio Button group using PDFBox 2.0, I am able to create 3 Radio Buttons,
but I can't figure out how to group them (PDFBox 1.8, used PDRadioCollection<https://pdfbox.apache.org/docs/1.8.10/javadocs/org/apache/pdfbox/pdmodel/interactive/form/PDRadioCollection.html>,
but 2.0 doesn't.).



How do you create a Radio Button Group with PDFBox 2.0?

Here is my current code:
        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);

        PDResources res = new PDResources();
        COSName fontName = res.add(PDTrueTypeFont.load(document, new FileInputStream("C:/Windows/Fonts/arial.ttf"),
StandardEncoding.INSTANCE));
        acroForm.setDefaultResources(res);
        acroForm.setDefaultAppearance('/' + fontName.getName() + " 10 Tf 0 g");

        PDPageContentStream contents = new PDPageContentStream(document, page);

        List<String> options = Arrays.asList("a", "b", "c");
        for (int i = 0; i < options.size(); i++) {
            PDRadioButton button = new PDRadioButton(acroForm);
            button.setPartialName("RadioButton" + options.get(i));

            PDAppearanceCharacteristicsDictionary fieldAppearance = new PDAppearanceCharacteristicsDictionary(new
COSDictionary());
            fieldAppearance.setBorderColour(new PDColor(new float[]{0, 0, 0}, PDDeviceRGB.INSTANCE));

            PDAnnotationWidget widget = button.getWidgets().get(0);
            widget.setRectangle(new PDRectangle(30, 800 - i * (21), 16, 16));
            widget.setAppearanceCharacteristics(fieldAppearance);

            acroForm.getFields().add(button);
            page.getAnnotations().add(widget);
        }
        contents.close();
        document.save(new FileOutputStream("RadioButtonTest.pdf"));
        document.close();

Cheers
Adam

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