Creating signature field in iText PDF -
First need to add dependency
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.4.5</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.5</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.60</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.60</version>
<type>jar</type>
<!-- <scope>compile</scope>
<optional>true</optional> -->
</dependency>
For certified you pdf document by digital sign first you need to create signature field in the document.
*Note - Signature field name is very important we signature data on the basis of this field name
Code for create signature field-
public class SignatureField{
public static final String SRC = "hello.pdf";
public static final String SIGNAME = "Signature1";
public void createPdf( String filename )
throws IOException,
DocumentException,
KeyStoreException,
NoSuchAlgorithmException,
CertificateException
{
// step 1: Create a Document
Document document = new Document();
// step 2: Create a PdfWriter
PdfWriter writer = PdfWriter.getInstance( document, new FileOutputStream( filename ) );
// step 3: Open the Document
document.open();
// step 4: Add content
document.add( new Paragraph( "Hello World!" ) );
// create a signature form field
PdfFormField field = PdfFormField.createSignature( writer );
// set Signature field name
field.setFieldName( SIGNAME );
// set the widget properties
field.setPage();
// set signature field position on the page
field.setWidget( new Rectangle( 2, 732, 144, 780 ), PdfAnnotation.HIGHLIGHT_INVERT );
field.setFlags( PdfAnnotation.FLAGS_PRINT );
// add it as an annotation
writer.addAnnotation( field );
// maybe you want to define an appearance
PdfAppearance tp = PdfAppearance.createAppearance( writer, 72, 48 );
tp.setColorStroke( BaseColor.BLUE );
tp.setColorFill( BaseColor.LIGHT_GRAY );
tp.rectangle( 0.5f, 0.5f, 71.5f, 47.5f );
tp.fillStroke();
tp.setColorFill( BaseColor.BLUE );
ColumnText.showTextAligned( tp, Element.ALIGN_CENTER, new Phrase( "SIGN HERE" ), 36, 24, 25 );
field.setAppearance( PdfAnnotation.APPEARANCE_NORMAL, tp );
BouncyCastleProvider provider = new BouncyCastleProvider();
Security.addProvider( provider );
// step 5: Close the Document
document.close();
}
public static void main( String[] args )
throws GeneralSecurityException,s
IOException,
DocumentException
{
SignatureField app = new SignatureField();
app.createPdf( SRC );
}
}
Output -
Comments
Post a Comment