You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by clovis <cl...@gmail.com> on 2016/01/20 16:48:02 UTC

PDPushButton Appearance

Hi,

I'm using 2.0.0-RC3 and trying to add a push button to submit the form to a
web app.
Following the samples, I wrote the following code.
I can add boder and backgorund color to the Text Field. But the Push Button
is still invisible. I can click on it but I can't see it.
What is wrong?


        // Add a new AcroForm and add that to the document
        PDAcroForm acroForm = new PDAcroForm(doc);
        doc.getDocumentCatalog().setAcroForm(acroForm);

        // Add and set the resources and default appearance at the form
level
        acroForm.setDefaultResources(resources);
        String defaultAppearanceString = "/Helv 0 Tf 0 g";
        acroForm.setDefaultAppearance(defaultAppearanceString);

        // Add a form field to the form.
        PDTextField textBox = new PDTextField(acroForm);
        textBox.setPartialName("SampleField");
        // Acrobat sets the font size to 12 as default
        // This is done by setting the font size to '12' on the
        // field level.
        defaultAppearanceString = "/Helv 12 Tf 0 g";
        textBox.setDefaultAppearance(defaultAppearanceString);

        // add the field to the acroform
        acroForm.getFields().add(textBox);

        // Specify the annotation associated with the field
        PDAnnotationWidget widget = textBox.getWidgets().get(0);
        PDRectangle rect = new PDRectangle(50, 750, 100, 20);
        widget.setRectangle(rect);
        PDAppearanceCharacteristicsDictionary fieldAppearance = new
PDAppearanceCharacteristicsDictionary(new COSDictionary());
        PDColor blue = new PDColor(new float[] { 0, 0, 1 },
PDDeviceRGB.INSTANCE);
        fieldAppearance.setBorderColour(blue);
        widget.setAppearanceCharacteristics(fieldAppearance);

        // Add the annotation to the page
        page.getAnnotations().add(widget);

        // set the field value
        textBox.setValue("Sample field");
        // Add a form field to the form.
        PDPushButton pb = new PDPushButton(acroForm);
        pb.setPartialName("push");

        // add the field to the acroform
        acroForm.getFields().add(pb);

        // Specify the annotation associated with the field
        widget = pb.getWidgets().get(0);
        rect = new PDRectangle(250, 730, 100, 20);
        widget.setRectangle(rect);
        fieldAppearance = new PDAppearanceCharacteristicsDictionary(new
COSDictionary());
        blue = new PDColor(new float[] { 0, 0, 1 }, PDDeviceRGB.INSTANCE);
        fieldAppearance.setBorderColour(blue);
        fieldAppearance.setBackground(blue);
        fieldAppearance.setNormalCaption("teste");
        widget.setAppearanceCharacteristics(fieldAppearance);
        // Add the annotation to the page
        page.getAnnotations().add(widget);

        contents.close();
        doc.save(filename);


Clóvis

Re: PDPushButton Appearance

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 20.01.2016 um 23:01 schrieb clovis:
> As I said I know nothing about the PDF file format.
> So, this workaround will have to do. Now I have to make this button submit
> the form.
>
> I have a system where PDF designers make PDF templates. Some users fill
> copies of those template and submit. My program processes those submited
> PDF files and input the data into a database. This all works ok.
> But now I want some users to fill and send form data directly from Acrobat
> Reader without having to submit entire PDFs.
> To do this I need to pre-process each PDF template with PDFBox and insert a
> PushButton that submits form data to my site.
> So, I think your workaround will be good enough because it is just to
> create a temporary PDF for form submission.
>
> Is this feasible?

If the people are working with Adobe Reader, yes.


> I think now I just need to turn my Push Button into a Submit Button.
>
> I think I have to create  a javascript on the PDF and associate with the
> button, am I right?

I don't know enough about that one, I've never created a submit button 
PDF. Submit forms are explained in "12.7.5.2 Submit-Form Action" in the 
PDF specification. But feel free to ask specific questions if something 
is missing in the API or not working. (I'm already wondering whether the 
"Flags for submit-form actions" are included in PDFDebugger)

Tilman


>
> Clóvis
>
>
>
>
>
>
> 2016-01-20 16:10 GMT-02:00 Tilman Hausherr <TH...@t-online.de>:
>
>> Am 20.01.2016 um 18:43 schrieb clovis:
>>
>>> Thanks, it is working. Now I can see the button.
>>> I need to understand this. I am a developer (java, c, .net, etc.) but I
>>> do not know PDF format and I am new to PDFBox (used iText long ago).
>>>
>>>
>>> The Javadocs states:
>>>
>>>      public void setNeedAppearances(Boolean
>>>      <
>>> http://download.oracle.com/javase/1.6.0/docs/api/java/lang/Boolean.html?is-external=true>
>>> value)
>>>
>>>      Set the NeedAppearances value. *If this is false, *PDFBox will
>>>      create appearances for all field widget.
>>>
>>>
>> Not only the creation of appearance streams isn't supported, the javadoc
>> is wrong too :-(
>>
>>
>>> The javadoc does not say what happens if you set to TRUE.
>>> You set to TRUE and the widget is visible, then I don't understand what
>>> "appearances" is (and I think I don't understand differences between
>>> appearances and widgets).
>>> Also, the text field was already visibel (with border and background
>>> colors).
>>>
>> The PDF spec tells this: "A flag specifying whether to construct
>> appearance streams and appearance dictionaries for all widget annotations
>> in the document". So it is set to tell Adobe to do it.
>>
>> Of course this is really just a workaround. The real solution is to
>> construct the appearance stream yourself.
>>
>> Tilman
>>
>>
>>
>>> Thanks again.
>>>
>>> Clóvis
>>>
>>>
>>> 2016-01-20 14:12 GMT-02:00 Tilman Hausherr <THausherr@t-online.de
>>> <ma...@t-online.de>>:
>>>
>>>
>>>      I can see the button by adding this line:
>>>
>>>      acroForm.setNeedAppearances(true);
>>>
>>>      alternatively, set the appearance stream yourself. This is similar
>>>      to the code to which I posted the link earlier today (
>>>      http://justpaste.it/CreateRadioButtons ). The stream is also to be
>>>      in AP/N, here are the contents that Adobe sets after saving the file:
>>>
>>>      0 0 1 rg
>>>      0 0 100 20 re
>>>      f
>>>      0 0 1 RG
>>>      0.5 0.5 99 19 re
>>>      s
>>>      q
>>>        1 1 98 18 re
>>>        W
>>>        n
>>>        0 g
>>>        0 G
>>>        BT
>>>          /Helv 12.81 Tf
>>>          0 g
>>>          36.128 5.686 Td
>>>          (teste) Tj
>>>        ET
>>>      Q
>>>
>>>
>>>
>>>
>>>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: PDPushButton Appearance

Posted by clovis <cl...@gmail.com>.
As I said I know nothing about the PDF file format.
So, this workaround will have to do. Now I have to make this button submit
the form.

I have a system where PDF designers make PDF templates. Some users fill
copies of those template and submit. My program processes those submited
PDF files and input the data into a database. This all works ok.
But now I want some users to fill and send form data directly from Acrobat
Reader without having to submit entire PDFs.
To do this I need to pre-process each PDF template with PDFBox and insert a
PushButton that submits form data to my site.
So, I think your workaround will be good enough because it is just to
create a temporary PDF for form submission.

Is this feasible?

I think now I just need to turn my Push Button into a Submit Button.

I think I have to create  a javascript on the PDF and associate with the
button, am I right?

Clóvis






2016-01-20 16:10 GMT-02:00 Tilman Hausherr <TH...@t-online.de>:

> Am 20.01.2016 um 18:43 schrieb clovis:
>
>> Thanks, it is working. Now I can see the button.
>> I need to understand this. I am a developer (java, c, .net, etc.) but I
>> do not know PDF format and I am new to PDFBox (used iText long ago).
>>
>>
>> The Javadocs states:
>>
>>     public void setNeedAppearances(Boolean
>>     <
>> http://download.oracle.com/javase/1.6.0/docs/api/java/lang/Boolean.html?is-external=true>
>> value)
>>
>>     Set the NeedAppearances value. *If this is false, *PDFBox will
>>     create appearances for all field widget.
>>
>>
> Not only the creation of appearance streams isn't supported, the javadoc
> is wrong too :-(
>
>
>> The javadoc does not say what happens if you set to TRUE.
>> You set to TRUE and the widget is visible, then I don't understand what
>> "appearances" is (and I think I don't understand differences between
>> appearances and widgets).
>> Also, the text field was already visibel (with border and background
>> colors).
>>
>
> The PDF spec tells this: "A flag specifying whether to construct
> appearance streams and appearance dictionaries for all widget annotations
> in the document". So it is set to tell Adobe to do it.
>
> Of course this is really just a workaround. The real solution is to
> construct the appearance stream yourself.
>
> Tilman
>
>
>
>> Thanks again.
>>
>> Clóvis
>>
>>
>> 2016-01-20 14:12 GMT-02:00 Tilman Hausherr <THausherr@t-online.de
>> <ma...@t-online.de>>:
>>
>>
>>     I can see the button by adding this line:
>>
>>     acroForm.setNeedAppearances(true);
>>
>>     alternatively, set the appearance stream yourself. This is similar
>>     to the code to which I posted the link earlier today (
>>     http://justpaste.it/CreateRadioButtons ). The stream is also to be
>>     in AP/N, here are the contents that Adobe sets after saving the file:
>>
>>     0 0 1 rg
>>     0 0 100 20 re
>>     f
>>     0 0 1 RG
>>     0.5 0.5 99 19 re
>>     s
>>     q
>>       1 1 98 18 re
>>       W
>>       n
>>       0 g
>>       0 G
>>       BT
>>         /Helv 12.81 Tf
>>         0 g
>>         36.128 5.686 Td
>>         (teste) Tj
>>       ET
>>     Q
>>
>>
>>
>>
>>
>

Re: PDPushButton Appearance

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 20.01.2016 um 18:43 schrieb clovis:
> Thanks, it is working. Now I can see the button.
> I need to understand this. I am a developer (java, c, .net, etc.) but 
> I do not know PDF format and I am new to PDFBox (used iText long ago).
>
>
> The Javadocs states:
>
>     public void setNeedAppearances(Boolean
>     <http://download.oracle.com/javase/1.6.0/docs/api/java/lang/Boolean.html?is-external=true>  value)
>
>     Set the NeedAppearances value. *If this is false, *PDFBox will
>     create appearances for all field widget.
>

Not only the creation of appearance streams isn't supported, the javadoc 
is wrong too :-(

>
> The javadoc does not say what happens if you set to TRUE.
> You set to TRUE and the widget is visible, then I don't understand 
> what "appearances" is (and I think I don't understand differences 
> between appearances and widgets).
> Also, the text field was already visibel (with border and background 
> colors).

The PDF spec tells this: "A flag specifying whether to construct 
appearance streams and appearance dictionaries for all widget 
annotations in the document". So it is set to tell Adobe to do it.

Of course this is really just a workaround. The real solution is to 
construct the appearance stream yourself.

Tilman


>
> Thanks again.
>
> Clóvis
>
>
> 2016-01-20 14:12 GMT-02:00 Tilman Hausherr <THausherr@t-online.de 
> <ma...@t-online.de>>:
>
>     I can see the button by adding this line:
>
>     acroForm.setNeedAppearances(true);
>
>     alternatively, set the appearance stream yourself. This is similar
>     to the code to which I posted the link earlier today (
>     http://justpaste.it/CreateRadioButtons ). The stream is also to be
>     in AP/N, here are the contents that Adobe sets after saving the file:
>
>     0 0 1 rg
>     0 0 100 20 re
>     f
>     0 0 1 RG
>     0.5 0.5 99 19 re
>     s
>     q
>       1 1 98 18 re
>       W
>       n
>       0 g
>       0 G
>       BT
>         /Helv 12.81 Tf
>         0 g
>         36.128 5.686 Td
>         (teste) Tj
>       ET
>     Q
>
>
>
>


Re: PDPushButton Appearance

Posted by clovis <cl...@gmail.com>.
Thanks, it is working. Now I can see the button.
I need to understand this. I am a developer (java, c, .net, etc.) but I do
not know PDF format and I am new to PDFBox (used iText long ago).


The Javadocs states:

public void setNeedAppearances(Boolean
<http://download.oracle.com/javase/1.6.0/docs/api/java/lang/Boolean.html?is-external=true>
value)

Set the NeedAppearances value. *If this is false, *PDFBox will create
appearances for all field widget.


The javadoc does not say what happens if you set to TRUE.
You set to TRUE and the widget is visible, then I don't understand what
"appearances" is (and I think I don't understand differences between
appearances and widgets).
Also, the text field was already visibel (with border and background
colors).

Thanks again.

Clóvis


2016-01-20 14:12 GMT-02:00 Tilman Hausherr <TH...@t-online.de>:

> I can see the button by adding this line:
>
> acroForm.setNeedAppearances(true);
>
> alternatively, set the appearance stream yourself. This is similar to the
> code to which I posted the link earlier today (
> <http://justpaste.it/CreateRadioButtons>
> http://justpaste.it/CreateRadioButtons ). The stream is also to be in
> AP/N, here are the contents that Adobe sets after saving the file:
>
> 0 0 1 rg
> 0 0 100 20 re
> f
> 0 0 1 RG
> 0.5 0.5 99 19 re
> s
> q
>   1 1 98 18 re
>   W
>   n
>   0 g
>   0 G
>   BT
>     /Helv 12.81 Tf
>     0 g
>     36.128 5.686 Td
>     (teste) Tj
>   ET
> Q
>
>
>
>

Re: PDPushButton Appearance

Posted by Tilman Hausherr <TH...@t-online.de>.
I can see the button by adding this line:

acroForm.setNeedAppearances(true);

alternatively, set the appearance stream yourself. This is similar to 
the code to which I posted the link earlier today ( 
http://justpaste.it/CreateRadioButtons ). The stream is also to be in 
AP/N, here are the contents that Adobe sets after saving the file:

0 0 1 rg
0 0 100 20 re
f
0 0 1 RG
0.5 0.5 99 19 re
s
q
   1 1 98 18 re
   W
   n
   0 g
   0 G
   BT
     /Helv 12.81 Tf
     0 g
     36.128 5.686 Td
     (teste) Tj
   ET
Q