pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Adam Steen <ad...@rmt.com.au>
Subject RE: How do you create a Radio Button Group with PDFBox 2.0?
Date Fri, 22 Jan 2016 01:29:39 GMT
HI Tilman

Thanks for your help, regarding "pdAcroForm.setNeedAppearances(true);", will pdfbox have a
default appearance in the future, so need appearances will not be needed?

I have a working solution to set default values and have acrobat render them. It was pure
fluke that I got this result, but wanted to post it so other will know and to close out the
solution.

Code: 
package pdfboxtesting;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSStream;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;
import org.apache.pdfbox.pdmodel.font.encoding.StandardEncoding;
import org.apache.pdfbox.pdmodel.graphics.color.PDColor;
import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceCharacteristicsDictionary;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceEntry;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDRadioButton;

public class RadioButtonTest
{
    private RadioButtonTest() {
    }

    public static void main(String[] args) throws IOException
    {
        String fileName = "output/Test.pdf";
        String selectedValue = "a";
        String name = "Radio";

        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);

        PDFont font = PDTrueTypeFont.load(document, new FileInputStream("C:/Windows/Fonts/arial.ttf"),
StandardEncoding.INSTANCE);

        PDResources res = new PDResources();
        COSName fontName = res.add(font);
        acroForm.setDefaultResources(res);
        acroForm.setDefaultAppearance('/' + fontName.getName() + " 0 Tf 0 g");

        PDPageContentStream contents = new PDPageContentStream(document, page);

        List<String> options = Arrays.asList("a", "b", "c");
        PDRadioButton radioButton = new PDRadioButton(acroForm);
        radioButton.setPartialName("RadioButton");
        List<PDAnnotationWidget> widgets = new ArrayList<>();
        for (int i = 0; i < options.size(); i++)
        {
            PDRadioButton subRadioButton = new PDRadioButton(acroForm);
            subRadioButton.setPartialName(name);

            PDAnnotationWidget widget = subRadioButton.getWidgets().get(0);

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

            widget.setRectangle(new PDRectangle(30, 800 - i * (21), 16, 16));
            widget.setAppearanceCharacteristics(fieldAppearance);
            widget.setPage(page);
            widget.getCOSObject().setItem(COSName.PARENT, radioButton);
            widget.setAnnotationFlags(4);

            widget.setAppearanceState(options.get(i).equals(selectedValue) ? selectedValue
: "Off");
            widget.setHighlightingMode("N");

            PDAppearanceDictionary ap = new PDAppearanceDictionary();

            COSDictionary aeDict = new COSDictionary();

            COSStream off = new COSStream();
            off.setItem(COSName.BBOX, new PDRectangle(0, 0, 16, 16));
            off.setItem(COSName.RESOURCES, res);
            OutputStream osOff = off.createOutputStream();
            osOff.write(("q 0 g /" + fontName.getName() + " 16 Tf BT (O) Tj ET Q").getBytes());
            osOff.write(("").getBytes());
            osOff.close();
            aeDict.setItem("Off", off);

            COSStream on = new COSStream();
            on.setItem(COSName.BBOX, new PDRectangle(0, 0, 16, 16));
            on.setItem(COSName.RESOURCES, res);
            OutputStream osOn = on.createOutputStream();
            osOn.write(("q 0 g BT /" + fontName.getName() + " 16 Tf (X) Tj ET Q").getBytes());
            osOn.write(("").getBytes());
            osOn.close();
            aeDict.setItem(options.get(i), on);

            PDAppearanceEntry ae = new PDAppearanceEntry(aeDict);
            ap.setNormalAppearance(ae);
            widget.setAppearance(ap);

            widgets.add(widget);
            page.getAnnotations().add(widget);
        }
        radioButton.setWidgets(widgets);
        System.out.println(radioButton.getOnValues());
        radioButton.setValue(selectedValue);
        acroForm.getFields().add(radioButton);

        System.out.println(radioButton.getValue());

        //acroForm.refreshAppearances(); // not implemented
        contents.close();
        document.save(new FileOutputStream(fileName));

        document.close();
    }
}

Adam

-----Original Message-----
From: Tilman Hausherr [mailto:THausherr@t-online.de] 
Sent: Thursday, 21 January 2016 4:55 PM
To: users@pdfbox.apache.org
Subject: Re: How do you create a Radio Button Group with PDFBox 2.0?

Am 21.01.2016 um 09:41 schrieb Adam Steen:
> I am now able to create a radio button group with "pdAcroForm.setNeedAppearances(true);",
acrobat rendering things nicely for me.
>
> I am also able to create a group of checkboxes with default values also with "pdAcroForm.setNeedAppearances(true);",
again acrobat rendering things nicely for me.
>
> My last hurdle is to create a radio button group with "pdAcroForm.setNeedAppearances(true);"
and have a default value (or value) be displayed.
>
> Using your code provided earlier Tilman, I was able to set a value or a default value,
but acrobat just does not want to render it.
>
> Is " pdAcroForm.setNeedAppearances(true);" a hack? Should I be rendering everything myself?

Yes, it is a bit of a hack. Good applications should create their own appearance streams.
In PDF 2.0 it will be mandatory.

>
> Adam
>
> -----Original Message-----
> From: Tilman Hausherr [mailto:THausherr@t-online.de]
> Sent: Thursday, 21 January 2016 4:29 PM
> To: users@pdfbox.apache.org
> Subject: Re: How do you create a Radio Button Group with PDFBox 2.0?
>
> Am 21.01.2016 um 06:51 schrieb Adam Steen:
>> It seems without building an appearance I am unable to set a default value?
> Indeed. I've been there too and looked through the code.
>
> Tilman
>
>> Does anyone have any ideas?
>>
>> Adam
>>
>>
>> -----Original Message-----
>> From: Adam Steen [mailto:adams@rmt.com.au]
>> Sent: Thursday, 21 January 2016 8:26 AM
>> To: users@pdfbox.apache.org
>> Subject: RE: How do you create a Radio Button Group with PDFBox 2.0?
>>
>> Hi
>>
>> I did manage to get a Radio Group functioning, I have posted the code 
>> to my question on stack overflow -> 
>> http://stackoverflow.com/a/34895158/415681
>>
>> The next problem will be getting a default value set.
>>
>> Adam
>>
>> -----Original Message-----
>> From: Tilman Hausherr [mailto:THausherr@t-online.de]
>> Sent: Wednesday, 20 January 2016 5:47 PM
>> To: users@pdfbox.apache.org
>> Subject: Re: How do you create a Radio Button Group with PDFBox 2.0?
>>
>> Here's the code I did yesterday:
>> http://justpaste.it/CreateRadioButtons
>>
>> The problem is that Adobe does things on its own even if the appearance streams are
set. This seems to be related to the appearance characteristics (/MK). I haven't been able
to teach it to "do nothing", or "do it all" when I do nothing (having empty streams).
>>
>> Tilman
>>
>>
>>
>> ---------------------------------------------------------------------
>> 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
>
>
> ---------------------------------------------------------------------
> 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

Mime
View raw message