pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bharat Bhatt <bharatbh...@gmail.com>
Subject Re: RadioButton group with one radiobutton selected
Date Fri, 03 Feb 2017 08:54:49 GMT
Thanks a lot Tilman. Its work as expected.
Is there any document or link I can refer to get details about streams and
different appearance?

Thanks,
Bharat

On Fri, Feb 3, 2017 at 1:39 AM, Tilman Hausherr <THausherr@t-online.de>
wrote:

> Here's code that does not use appearance stream (rather, these are dummy):
>
>
>           PDDocument document = new PDDocument();
>             PDPage page = new PDPage(PDRectangle.A4);
>
>             document.addPage(page);
>
>             PDAcroForm acroForm = new PDAcroForm(document);
>
>             acroForm.setNeedAppearances(true);
>
>             acroForm.setXFA(null);
>             document.getDocumentCatalog().setAcroForm(acroForm);
>
>             PDFont font = PDType1Font.HELVETICA;
>
>             PDResources res = new PDResources();
>             COSName fontName = res.add(font);
>             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");
>             PDRadioButton radioButton = new PDRadioButton(acroForm);
>             radioButton.setPartialName("RadioButtonParent");
>             //radioButton.setExportValues(options);
>             radioButton.getCOSObject().setName(COSName.DV,
> options.get(1));
>             radioButton.setFieldFlags(49152);
>             int on = 1;
>
>             List<PDAnnotationWidget> widgets = new ArrayList<>();
>             for (int i = 0; i < options.size(); i++)
>             {
>                 PDAppearanceCharacteristicsDictionary fieldAppearance =
> new PDAppearanceCharacteristicsDictionary(new COSDictionary());
>                 fieldAppearance.setBorderColour(new PDColor(new float[] {
> 0, 0, 0 }, PDDeviceRGB.INSTANCE));
>                 //PDAnnotationWidget widget =
> subRadioButtons.getWidgets().get(0);
>                 PDAnnotationWidget widget = new PDAnnotationWidget();
>                 widget.setRectangle(new PDRectangle(30, 811 - i * (21),
> 16, 16));
> widget.setAppearanceCharacteristics(fieldAppearance);
>                 widget.setAnnotationFlags(4);
>                 widget.setPage(page);
>                 widget.setParent(radioButton);
>
>                 COSDictionary apNDict = new COSDictionary();
>                 COSStream offNStream = new COSStream();
>                 offNStream.setItem(COSName.BBOX, new PDRectangle(16, 16));
>                 offNStream.setItem(COSName.FORMTYPE, COSInteger.ONE);
>                 offNStream.setItem(COSName.TYPE, COSName.XOBJECT);
>                 offNStream.setItem(COSName.SUBTYPE, COSName.FORM);
>
>                 apNDict.setItem(COSName.Off, offNStream);
>
>                 COSStream onNStream = new COSStream();
>                 onNStream.setItem(COSName.BBOX, new PDRectangle(16, 16));
>                 onNStream.setItem(COSName.FORMTYPE, COSInteger.ONE);
>                 onNStream.setItem(COSName.TYPE, COSName.XOBJECT);
>                 onNStream.setItem(COSName.SUBTYPE, COSName.FORM);
>
>                 apNDict.setItem(options.get(i), onNStream);
>
>                 COSDictionary apDDict = new COSDictionary();
>                 COSStream offDStream = new COSStream();
>                 offDStream.setItem(COSName.BBOX, new PDRectangle(16, 16));
>                 offDStream.setItem(COSName.FORMTYPE, COSInteger.ONE);
>                 offDStream.setItem(COSName.TYPE, COSName.XOBJECT);
>                 offDStream.setItem(COSName.SUBTYPE, COSName.FORM);
>
>                 apDDict.setItem(COSName.Off, offDStream);
>
>                 COSStream onDStream = new COSStream();
>                 onDStream.setItem(COSName.BBOX, new PDRectangle(16, 16));
>                 onDStream.setItem(COSName.FORMTYPE, COSInteger.ONE);
>                 onDStream.setItem(COSName.TYPE, COSName.XOBJECT);
>                 onDStream.setItem(COSName.SUBTYPE, COSName.FORM);
>
>                 apDDict.setItem(options.get(i), onDStream);
>
>                 PDAppearanceDictionary appearance = new
> PDAppearanceDictionary();
>                 PDAppearanceEntry appearanceNEntry = new
> PDAppearanceEntry(apNDict);
>                 appearance.setNormalAppearance(appearanceNEntry);
>                 PDAppearanceEntry appearanceDEntry = new
> PDAppearanceEntry(apDDict);
>                 appearance.setDownAppearance(appearanceDEntry);
>
>                 widget.setAppearance(appearance);
>
>                 widget.setAppearanceState(i == on ? options.get(i) :
> "Off");
>
>                 widgets.add(widget);
>                 page.getAnnotations().add(widget);
>
>                 contents.beginText();
>                 contents.setFont(font, 10);
>                 contents.newLineAtOffset(56, 811 - i * (21) + 4);
>                 contents.showText(options.get(i));
>                 contents.endText();
>             }
>             radioButton.setWidgets(widgets);
>             acroForm.getFields().add(radioButton);
>
>             radioButton.setValue("b");
>
>             contents.close();
>             try (FileOutputStream output = new
> FileOutputStream("test.pdf"))
>             {
>                 document.save(output);
>             }
>             document.close();
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
> For additional commands, e-mail: users-help@pdfbox.apache.org
>
>

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