Am 03.02.2017 um 09:54 schrieb Bharat Bhatt:
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?

The PDF specification:
https://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/PDF32000_2008.pdf
Page 430 "interactive forms".

That may be scary... also look at the form examples and the API documentation, and of course at the PDF structure with the PDFDebugger command line utility. You can see what you created. Note that there are two views (where you can't see acroform, only the widgets), one page based and one that has all.

The content of the appearance streams is not described in PDF specification. Every application must do that itself, somehow. We do generate the appearance streams in some, but not all type of fields. In the example that you didn't use I just copied from Adobe. It is possible to generate this parametrically with some reverse engineering and this formula for circle with bezier curves:
http://spencermortensen.com/articles/bezier-circle/

Tilman


Thanks,
Bharat

On Fri, Feb 3, 2017 at 1:39 AM, Tilman Hausherr <[email protected]>
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: [email protected]
For additional commands, e-mail: [email protected]




---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to