You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by np...@hjf.org on 2016/05/06 15:41:54 UTC

Interactive signature

All,

I am new to this forum, not sure if this has been asked before.

I got fillable  PDF working with interactive forms examples. I also have 
added a button in the PDF, I want to add the signature image (as in 
signature example) on click of that button (see attached). Once signature 
is in place, I want to remove the button and save the PDF. Is this 
something possible? Does anyone have working example of the same?

Thanks

Naresh



Re: Interactive signature

Posted by np...@hjf.org.
It worked!! Thank you Tilman!!



From:   Tilman Hausherr <TH...@t-online.de>
To:     users@pdfbox.apache.org
Date:   05/09/2016 05:09 PM
Subject:        Re: Interactive signature



Am 09.05.2016 um 22:54 schrieb npunjabi@hjf.org:
> Sorry to bother again. I couldn't find the source of the PDF link below.
> How can I add similar "Signature field" to my own PDF? I was looking for
> an example but couldn't find it.

Yeah, I noticed that my answer was only half of what you'd need and 
wondered why you didn't ask. Here's some code to create an empty 
signature field. Tell me if it works for you. If it does, then I should 
add it to our example collection.


         PDSignatureField sigField = new PDSignatureField(acroForm);
         PDAnnotationWidget widget = sigField.getWidgets().get(0);
         PDRectangle rect = new PDRectangle(50, 650, 200, 50);
         widget.setRectangle(rect);
         widget.setPage(page);
         page.getAnnotations().add(widget);

I tried that code in the CreateSimpleForm example. Here's the full code, 
remove the parts about the other field (textbox):


/*
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
  * this work for additional information regarding copyright ownership.
  * The ASF licenses this file to You under the Apache License, Version 
2.0
  * (the "License"); you may not use this file except in compliance with
  * the License.  You may obtain a copy of the License at
  *
  *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */

package org.apache.pdfbox.examples.interactive.form;

import java.io.IOException;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
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.PDType1Font;
import 
org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDSignatureField;
import org.apache.pdfbox.pdmodel.interactive.form.PDTextField;

/**
  * An example of creating an AcroForm and a form field from scratch.
  *
  * The form field is created with properties similar to creating
  * a form with default settings in Adobe Acrobat.
  *
  */
public final class CreateSimpleForm
{
     private CreateSimpleForm()
     {
     }

     public static void main(String[] args) throws IOException
     {
         // Create a new document with an empty page.
         PDDocument document = new PDDocument();
         PDPage page = new PDPage(PDRectangle.A4);
         document.addPage(page);

         // Adobe Acrobat uses Helvetica as a default font and
         // stores that under the name '/Helv' in the resources dictionary
         PDFont font = PDType1Font.HELVETICA;
         PDResources resources = new PDResources();
         resources.put(COSName.getPDFName("Helv"), font);

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

         // Add and set the resources and default appearance at the form 
level
         acroForm.setDefaultResources(resources);

         // Acrobat sets the font size on the form level to be
         // auto sized as default. This is done by setting the font size 
to '0'
         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, 200, 50);
         widget.setRectangle(rect);
         widget.setPage(page);

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

         // set the field value
         textBox.setValue("Sample field");

         /////// NEW - empty signature field
         PDSignatureField sigField = new PDSignatureField(acroForm);
         widget = sigField.getWidgets().get(0);
         rect = new PDRectangle(50, 650, 200, 50);
         widget.setRectangle(rect);
         widget.setPage(page);
         page.getAnnotations().add(widget);


         document.save("target/SimpleForm.pdf");
         document.close();
     }
}




Tilman

>
> Thanks again for your help.
>
> Naresh
>
>
>
> From:   Tilman Hausherr <TH...@t-online.de>
> To:     users@pdfbox.apache.org
> Date:   05/06/2016 03:35 PM
> Subject:        Re: Interactive signature
>
>
>
> Am 06.05.2016 um 17:41 schrieb npunjabi@hjf.org:
>> All,
>>
>> I am new to this forum, not sure if this has been asked before.
>>
>> I got fillable  PDF working with interactive forms examples. I also
>> have added a button in the PDF, I want to add the signature image (as
>> in signature example) on click of that button (see attached). Once
>> signature is in place, I want to remove the button and save the PDF.
>> Is this something possible? Does anyone have working example of the
> same?
>
> You don't need such a button. Please have look at this file
>
> 
https://issues.apache.org/jira/secure/attachment/12663611/documentSignable.pdf_signaturePAdESVisualSignatureDemo.pdf

>
>
> and open it with Adobe Reader, then click on a field.
>
> Tilman
>
>
>


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




Re: Interactive signature

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 09.05.2016 um 22:54 schrieb npunjabi@hjf.org:
> Sorry to bother again. I couldn't find the source of the PDF link below.
> How can I add similar "Signature field" to my own PDF? I was looking for
> an example but couldn't find it.

Yeah, I noticed that my answer was only half of what you'd need and 
wondered why you didn't ask. Here's some code to create an empty 
signature field. Tell me if it works for you. If it does, then I should 
add it to our example collection.


         PDSignatureField sigField = new PDSignatureField(acroForm);
         PDAnnotationWidget widget = sigField.getWidgets().get(0);
         PDRectangle rect = new PDRectangle(50, 650, 200, 50);
         widget.setRectangle(rect);
         widget.setPage(page);
         page.getAnnotations().add(widget);

I tried that code in the CreateSimpleForm example. Here's the full code, 
remove the parts about the other field (textbox):


/*
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
  * this work for additional information regarding copyright ownership.
  * The ASF licenses this file to You under the Apache License, Version 2.0
  * (the "License"); you may not use this file except in compliance with
  * the License.  You may obtain a copy of the License at
  *
  *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */

package org.apache.pdfbox.examples.interactive.form;

import java.io.IOException;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
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.PDType1Font;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDSignatureField;
import org.apache.pdfbox.pdmodel.interactive.form.PDTextField;

/**
  * An example of creating an AcroForm and a form field from scratch.
  *
  * The form field is created with properties similar to creating
  * a form with default settings in Adobe Acrobat.
  *
  */
public final class CreateSimpleForm
{
     private CreateSimpleForm()
     {
     }

     public static void main(String[] args) throws IOException
     {
         // Create a new document with an empty page.
         PDDocument document = new PDDocument();
         PDPage page = new PDPage(PDRectangle.A4);
         document.addPage(page);

         // Adobe Acrobat uses Helvetica as a default font and
         // stores that under the name '/Helv' in the resources dictionary
         PDFont font = PDType1Font.HELVETICA;
         PDResources resources = new PDResources();
         resources.put(COSName.getPDFName("Helv"), font);

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

         // Add and set the resources and default appearance at the form 
level
         acroForm.setDefaultResources(resources);

         // Acrobat sets the font size on the form level to be
         // auto sized as default. This is done by setting the font size 
to '0'
         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, 200, 50);
         widget.setRectangle(rect);
         widget.setPage(page);

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

         // set the field value
         textBox.setValue("Sample field");

         /////// NEW - empty signature field
         PDSignatureField sigField = new PDSignatureField(acroForm);
         widget = sigField.getWidgets().get(0);
         rect = new PDRectangle(50, 650, 200, 50);
         widget.setRectangle(rect);
         widget.setPage(page);
         page.getAnnotations().add(widget);


         document.save("target/SimpleForm.pdf");
         document.close();
     }
}




Tilman

>
> Thanks again for your help.
>
> Naresh
>
>
>
> From:   Tilman Hausherr <TH...@t-online.de>
> To:     users@pdfbox.apache.org
> Date:   05/06/2016 03:35 PM
> Subject:        Re: Interactive signature
>
>
>
> Am 06.05.2016 um 17:41 schrieb npunjabi@hjf.org:
>> All,
>>
>> I am new to this forum, not sure if this has been asked before.
>>
>> I got fillable  PDF working with interactive forms examples. I also
>> have added a button in the PDF, I want to add the signature image (as
>> in signature example) on click of that button (see attached). Once
>> signature is in place, I want to remove the button and save the PDF.
>> Is this something possible? Does anyone have working example of the
> same?
>
> You don't need such a button. Please have look at this file
>
> https://issues.apache.org/jira/secure/attachment/12663611/documentSignable.pdf_signaturePAdESVisualSignatureDemo.pdf
>
>
> and open it with Adobe Reader, then click on a field.
>
> Tilman
>
>
>


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


Re: Interactive signature

Posted by np...@hjf.org.
Sorry to bother again. I couldn't find the source of the PDF link below. 
How can I add similar "Signature field" to my own PDF? I was looking for 
an example but couldn't find it.

Thanks again for your help.

Naresh



From:   Tilman Hausherr <TH...@t-online.de>
To:     users@pdfbox.apache.org
Date:   05/06/2016 03:35 PM
Subject:        Re: Interactive signature



Am 06.05.2016 um 17:41 schrieb npunjabi@hjf.org:
> All,
>
> I am new to this forum, not sure if this has been asked before.
>
> I got fillable  PDF working with interactive forms examples. I also 
> have added a button in the PDF, I want to add the signature image (as 
> in signature example) on click of that button (see attached). Once 
> signature is in place, I want to remove the button and save the PDF. 
> Is this something possible? Does anyone have working example of the 
same? 

You don't need such a button. Please have look at this file

https://issues.apache.org/jira/secure/attachment/12663611/documentSignable.pdf_signaturePAdESVisualSignatureDemo.pdf


and open it with Adobe Reader, then click on a field.

Tilman



Re: Interactive signature

Posted by np...@hjf.org.
Thank you Tilman!



From:   Tilman Hausherr <TH...@t-online.de>
To:     users@pdfbox.apache.org
Date:   05/06/2016 03:35 PM
Subject:        Re: Interactive signature



Am 06.05.2016 um 17:41 schrieb npunjabi@hjf.org:
> All,
>
> I am new to this forum, not sure if this has been asked before.
>
> I got fillable  PDF working with interactive forms examples. I also 
> have added a button in the PDF, I want to add the signature image (as 
> in signature example) on click of that button (see attached). Once 
> signature is in place, I want to remove the button and save the PDF. 
> Is this something possible? Does anyone have working example of the 
same? 

You don't need such a button. Please have look at this file

https://issues.apache.org/jira/secure/attachment/12663611/documentSignable.pdf_signaturePAdESVisualSignatureDemo.pdf


and open it with Adobe Reader, then click on a field.

Tilman



Re: Interactive signature

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 06.05.2016 um 17:41 schrieb npunjabi@hjf.org:
> All,
>
> I am new to this forum, not sure if this has been asked before.
>
> I got fillable  PDF working with interactive forms examples. I also 
> have added a button in the PDF, I want to add the signature image (as 
> in signature example) on click of that button (see attached). Once 
> signature is in place, I want to remove the button and save the PDF. 
> Is this something possible? Does anyone have working example of the same? 

You don't need such a button. Please have look at this file

https://issues.apache.org/jira/secure/attachment/12663611/documentSignable.pdf_signaturePAdESVisualSignatureDemo.pdf

and open it with Adobe Reader, then click on a field.

Tilman