Hi,

I had a need to add signature fields to an interactive PDF document and in the maillist archives I saw that iText can't do that. So I tried implementing support for adding signature fields to a PDF document and it didn't seem all that difficult to do. So I might just possibly be missing something important <wink>.

Below is an example of adding a signature field and attached are the resulting pdf file and diff against PdfAcroForm.java from the CVS.

Comments and suggestions would be very wellcome.

regards,
finn


import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

public class tst02 {
public static void main(String[] args) throws Exception {
Document doc = new Document();
PdfWriter writer = PdfWriter.getInstance(doc,
new FileOutputStream("tst02.pdf"));

PdfAcroForm acroForm = writer.getAcroForm();
doc.open();
doc.add(new Paragraph("Hello World"));
acroForm.addSignature("mysig", 73, 705, 149, 759);
doc.close();
}
}

Attachment: tst02.pdf
Description: Adobe PDF document

Index: PdfAcroForm.java
===================================================================
RCS file: /cvsroot/itext/src/com/lowagie/text/pdf/PdfAcroForm.java,v
retrieving revision 1.12
diff -u -r1.12 PdfAcroForm.java
--- PdfAcroForm.java    21 Jun 2002 06:58:24 -0000      1.12
+++ PdfAcroForm.java    21 Jan 2003 14:43:38 -0000
@@ -445,4 +445,45 @@
         field.setPage();
         field.setBorderStyle(new PdfBorderDictionary(2, 
PdfBorderDictionary.STYLE_SOLID));
     }
-}
\ No newline at end of file
+
+
+    public PdfFormField addSignature(String name, 
+                    float llx, float lly, float urx, float ury)
+    {
+        PdfFormField signature = PdfFormField.createSignature(writer);
+        setSignatureParams(signature, name, llx, lly, urx, ury);
+        drawSignatureAppearences(signature, llx, lly, urx, ury);
+        addFormField(signature);
+        return signature;
+    }
+    
+    public void setSignatureParams(PdfFormField field, String name,
+                    float llx, float lly, float urx, float ury) {
+        field.setWidget(new Rectangle(llx, lly, urx, ury), 
+PdfAnnotation.HIGHLIGHT_INVERT);
+        field.setFieldName(name);
+        field.setFlags(PdfAnnotation.FLAGS_PRINT);
+        field.setPage();
+        field.setMKBorderColor(java.awt.Color.black);
+        field.setMKBackgroundColor(java.awt.Color.white);
+    }
+
+    public void drawSignatureAppearences(PdfFormField field, 
+                    float llx, float lly, float urx, float ury)
+    {
+        PdfContentByte cb = writer.getDirectContent();
+        PdfAppearance tp = cb.createAppearance(urx - llx, ury - lly);
+        tp.setGrayFill(1.0f);
+        tp.rectangle(0, 0, urx - llx, ury - lly);
+        tp.fill();
+        tp.setGrayStroke(0);
+        tp.setLineWidth(1);
+        tp.rectangle(0.5f, 0.5f, urx - llx - 0.5f, ury - lly - 0.5f);
+        tp.closePathStroke();
+        tp.saveState();
+        tp.rectangle(1, 1, urx - llx - 2, ury - lly - 2);
+        tp.clip();
+        tp.newPath();
+        tp.restoreState();
+        field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);
+    }
+}


Reply via email to