Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id E2987200C0F for ; Thu, 2 Feb 2017 20:56:51 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id E12ED160B57; Thu, 2 Feb 2017 19:56:51 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id DEF5C160B44 for ; Thu, 2 Feb 2017 20:56:50 +0100 (CET) Received: (qmail 27836 invoked by uid 500); 2 Feb 2017 19:56:50 -0000 Mailing-List: contact users-help@pdfbox.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: users@pdfbox.apache.org Delivered-To: mailing list users@pdfbox.apache.org Received: (qmail 27813 invoked by uid 99); 2 Feb 2017 19:56:49 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 02 Feb 2017 19:56:49 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id B5127C13F2 for ; Thu, 2 Feb 2017 19:56:48 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -1.2 X-Spam-Level: X-Spam-Status: No, score=-1.2 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, RP_MATCHES_RCVD=-2.999] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id mwHPyoor4FHO for ; Thu, 2 Feb 2017 19:56:45 +0000 (UTC) Received: from mailout07.t-online.de (mailout07.t-online.de [194.25.134.83]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with ESMTPS id ACD575F5F8 for ; Thu, 2 Feb 2017 19:56:44 +0000 (UTC) Received: from fwd01.aul.t-online.de (fwd01.aul.t-online.de [172.20.27.147]) by mailout07.t-online.de (Postfix) with SMTP id CEAE1420CAF6 for ; Thu, 2 Feb 2017 20:56:37 +0100 (CET) Received: from [192.168.2.105] (ZedFGOZHrhPcvMujXULuITmNRlFJRfr8toxGzBbM9ihlrfjE7ttDvjzskYaaucvwuH@[217.231.128.226]) by fwd01.t-online.de with (TLSv1.2:ECDHE-RSA-AES256-SHA encrypted) esmtp id 1cZNUd-2MeI7c0; Thu, 2 Feb 2017 20:56:31 +0100 Subject: Re: RadioButton group with one radiobutton selected To: users@pdfbox.apache.org References: <35c6c7ef-4735-d9bd-77ad-b018a7b32fe3@t-online.de> From: Tilman Hausherr Message-ID: Date: Thu, 2 Feb 2017 20:57:29 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-ID: ZedFGOZHrhPcvMujXULuITmNRlFJRfr8toxGzBbM9ihlrfjE7ttDvjzskYaaucvwuH X-TOI-MSGID: a34f2933-867b-4459-a929-a479f8df7a98 archived-at: Thu, 02 Feb 2017 19:56:52 -0000 Am 02.02.2017 um 20:27 schrieb Bharat Bhatt: > Hi Tilman, > > > Its very difficult to change the appearance string. I required different > size and it will be passed as method variable. Where to change in strings? The appearance stream is that gibberish. If you know all sizes in advance then one solution is to reactivate the setNeedAppearances(true) and not to set any appearance streams (that's the gibberish), e.g. use a COSDictionary instead of a COSStream (but it must be different object for on and off). The open with Adobe Reader. Then save that one. Then open it with PDFDebugger and go to the appearance streams. Copy that one over the gibberish. If you don't know the sizes then setNeedAppearances(true) and keep it that way. This will work with Adobe Reader but possibly not with other readers. > One more query: > > Before creation radio button I created some TextField and saved the default > PDAcroForm at the instance level variable. > I required same default behavior for other Textfield or any other control > which will be created after the radio button. Sorry, I don't understand you there. > > When I am applying back the old PDAcroForm I am not getting same appearance. > > How to set the old PDAcroForm behavior with default or old appreance? Tilman > > > > Thanks, > Bharat > > > On Fri, Feb 3, 2017 at 12:20 AM, Tilman Hausherr > wrote: > >> Am 02.02.2017 um 19:22 schrieb Bharat Bhatt: >> >>> 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. >>> >> That is because you didn't use all the code from the answer in >> >> https://stackoverflow.com/questions/41631119/radiobutton- >> display-problems-with-pdfbox >> >> In the code below, specifically this line >> >> COSStream onStream = new COSStream(); >> aeDict.setItem(options.get(i), onStream); >> aeDict.setItem(COSName.Off.getName(), onStream); >> >> will not work. You can't use the same (empty!) appearance stream for on >> and off. >> >> If you can't use the code from the answer because something is different, >> e.g. you need a different size or whatever, then please tell what the >> difference is and I'll do my best to help you. >> >> Tilman >> >> >> >> >> >> >>> Thanks, >>> Bharat >>> >>> On Thu, Feb 2, 2017 at 10:57 PM, Tilman Hausherr >>> 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 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 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 >>>> > 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 >>>>> > wrote: >>>>> >>>>> Please see the code in the answer to this question: >>>>> https://stackoverflow.com/questions/41631119/radiobutton- >>>>> display-problems-with-pdfbox >>>>> >>>> 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 options = Arrays.asList("a", >>>>> "b", "c"); >>>>> PDRadioButton radioButton = new >>>>> PDRadioButton(acroForm); >>>>> radioButton.setPartialName("Ra >>>>> dioButtonParent"); >>>>> radioButton.setExportValues(options); >>>>> radioButton.getCOSObject().set >>>>> Name(COSName.DV, >>>>> options.get(1)); >>>>> >>>>> List 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 >>>>> 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: users-unsubscribe@pdfbox.apache.org >>>>> For additional commands, e-mail: users-help@pdfbox.apache.org >>>>> >>>>> >>>> >> --------------------------------------------------------------------- >> To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org >> For additional commands, e-mail: users-help@pdfbox.apache.org >> >> --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org For additional commands, e-mail: users-help@pdfbox.apache.org