Hi Tilman,
Whatever code I send yesterday not worked with change
|widget.setAppearanceState(i
==on
?options.get(i):"Off");|
But I refer the code https://stackoverflow.com/ques
tions/41631119/radiobutton-display-problems-with-pdfbox and tried the
|widget.setAppearanceState(i
==on
?options.get(i):"Off");| as I attached in previous mail.
and thats work fine. But issue coming when another radio button is clicked.
Please suggest me the better approach for radiobutton with selected value.
Thanks,
Bharat
On Thu, Feb 2, 2017 at 10:57 PM, Tilman Hausherr <[email protected]>
wrote:
> Hi,
>
> The code I mentioned yesterday works perfectly. You use something that has
> only parts of it and now it no longer works. For example you deleted the
> appearance streams, but didn't reactivate setNeedAppearances(true); . So it
> will show nothing.
> Then you have an empty and identical stream for selected and off. That
> can't be good. There may be other problems.
>
> Tilman
>
>
> Am 02.02.2017 um 18:10 schrieb Bharat Bhatt:
>
>> Hi Tilman,
>>
>> I am facing another issue in RadioButton. when click on unselected radio
>> button appearance of radio will disappear.
>>
>> Sample PDF is attached and sample code I am using as follow:
>> try
>> {
>> PDDocument document = new PDDocument();
>> PDPage page = new PDPage(PDRectangle.A4);
>>
>> document.addPage(page);
>>
>> PDAcroForm acroForm = new PDAcroForm(document);
>>
>> // not needed, we have appearance streams
>> //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");
>> // removed per advice of Maruan Sahyoun, setValue didn't work
>> anymore
>> //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 = new PDAnnotationWidget();
>> widget.setRectangle(new PDRectangle(30, 811 - i * (21),
>> 16, 16));
>> widget.setAppearanceCharacteristics(fieldAppearance);
>> widget.setAnnotationFlags(4);
>> widget.setPage(page);
>> PDAppearanceDictionary appearance =
>> widget.getAppearance();
>> if (appearance == null)
>> {
>> appearance = new PDAppearanceDictionary();
>> }
>>
>> COSDictionary aeDict = new COSDictionary();
>> COSStream onStream = new COSStream();
>> aeDict.setItem(options.get(i), onStream);
>> aeDict.setItem(COSName.Off.getName(), onStream);
>>
>> PDAppearanceEntry ae = new PDAppearanceEntry(aeDict);
>> appearance.setNormalAppearance(ae);
>> widget.setAppearance(appearance);
>> widgets.add(widget);
>> page.getAnnotations().add(widget);
>>
>>
>> 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);
>>
>> contents.close();
>> try (FileOutputStream output = new
>> FileOutputStream("D:\\test22.pdf"))
>> {
>> document.save(output);
>> }
>> document.close();
>> }
>> catch (IOException e)
>> {
>> e.printStackTrace();
>> }
>>
>> Please help me resolve this issue. Is any other configuration or
>> appearance I have to add.
>>
>>
>> Thanks,
>> Bharat
>>
>> On Thu, Feb 2, 2017 at 11:44 AM, Bharat Bhatt <[email protected]
>> <mailto:[email protected]>> wrote:
>>
>> Thank a lot for details.
>>
>> I tried with |widget.setAppearanceState(i ==on
>> ?options.get(i):"Off");| and its works fine for me.
>>
>> Thanks,
>> Bharat
>>
>>
>>
>> On Thu, Feb 2, 2017 at 12:40 AM, Tilman Hausherr
>> <[email protected] <mailto:[email protected]>> wrote:
>>
>> Please see the code in the answer to this question:
>> https://stackoverflow.com/questions/41631119/radiobutton-
>> display-problems-with-pdfbox
>> <https://stackoverflow.com/questions/41631119/radiobutton-
>> display-problems-with-pdfbox>
>>
>> To select a button, the appropriate line there is
>>
>> |widget.setAppearanceState(i ==on ?options.get(i):"Off");|
>>
>>
>> setValue should also work, but you'd have to delete this line
>>
>> radioButton.setExportValues(options);
>>
>>
>> because there's a bug related to that.
>>
>> Your code below seems to be based on an older answer of mine
>> (it has "new PDRadioButton" twice), which wasn't really good.
>>
>> Another strategy would be that you open your created file with
>> PDFDebugger, then do the same after selecting something in
>> Adobe Reader and saving it.
>>
>> Tilman
>>
>>
>> Am 01.02.2017 um 19:22 schrieb Bharat Bhatt:
>>
>> Hi,
>>
>> I used PDRadioButton to create group of radio button. I am
>> trying to select
>> the one radio button by default but radio button is not
>> appearing as
>> selected.
>> Code example as follows:
>> try
>> {
>> 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().set
>> Name(COSName.DV,
>> options.get(1));
>>
>> List<PDAnnotationWidget> widgets = new
>> ArrayList<>();
>> for (int i = 0; i < options.size(); i++)
>> {
>> PDRadioButton subRadioButtons = new
>> PDRadioButton(acroForm);
>> subRadioButtons.setPartialName("RadioButton");
>>
>> PDAppearanceCharacteristicsDictionary fieldAppearance =
>> new
>> PDAppearanceCharacteristicsDictionary(new
>> COSDictionary());
>> fieldAppearance.setBorderColour(new PDColor(new float[] {
>> 0, 0, 0 }, PDDeviceRGB.INSTANCE));
>>
>> PDAnnotationWidget widget =
>> subRadioButtons.getWidgets().get(0);
>> widget.setRectangle(new PDRectangle(30,
>> 811 - i * (21), 16,
>> 16));
>> widget.setAppearanceCharacteristics(fieldAppearance);
>>
>> PDAppearanceDictionary appearance =
>> widget.getAppearance();
>> if (appearance == null)
>> {
>> appearance = new
>> PDAppearanceDictionary();
>> }
>>
>> COSDictionary aeDict = new COSDictionary();
>> COSStream on = new COSStream();
>> aeDict.setItem(options.get(i), on);
>>
>> String value = null;
>> if (options != null)
>> {
>> value = options.get(i);
>> if (!"b".equalsIgnoreCase(value))
>> {
>> aeDict.setItem(COSName.Off.ge
>> <http://COSName.Off.ge>tName(), on);
>> }
>> }
>>
>> PDAppearanceEntry ae = new
>> PDAppearanceEntry(aeDict);
>> appearance.setNormalAppearance(ae);
>> widget.setAppearance(appearance);
>> 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);
>>
>> contents.close();
>> try (FileOutputStream output = new
>> FileOutputStream("D:\\RadioTest.pdf"))
>> {
>> document.save(output);
>> }
>> document.close();
>> }
>> catch (IOException e)
>> {
>> e.printStackTrace();
>> }
>>
>> How to make radio button "b" as selected?
>>
>> Thanks,
>> Bharat
>>
>>
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [email protected]
>> For additional commands, e-mail: [email protected]
>>
>
>
>