Berthou.comA french developper blog | |
DescriptionRecently I had to research a solution to sign a PDF document. I think I am not the one to have this sort of problem, so I put here the result of my tests for the community.With this you can create and sign a PDF document with a PKCS12 certificat. When you signed a document you include in this document your numeric signature. It add automaticly this sort of pictures in PDF document and moreover to signed it. So it forbid any modifications by saying the transmitter (the signatory of the document).
Tools used
Sample (source code)Comme vous pouvez le voir le code java pour réaliser cela est très simple il est divisé en deux parties :
package com.berthou.test.pdf ; import java.io.*; import java.security.*; import java.security.cert.Certificate; import com.lowagie.text.*; import com.lowagie.text.pdf.*; public class sign_pdf { /** * Nom du document PDF généré non signé */ static String fname = "D:\\HelloWorld.pdf" ; /** * Nom du document PDF généré signé */ static String fnameS = "D:\\HelloWorld_sign.pdf" ; public static void main(String[] args) { try { sign_pdf.buildPDF() ; sign_pdf.signPdf() ; } catch(Exception e) { } } /** * Création d'un simple document PDF "Hello World" */ public static void buildPDF() { // Creation du document Document document = new Document(); try { // Creation du "writer" vers le doc // directement vers un fichier PdfWriter.getInstance(document, new FileOutputStream(fname)); // Ouverture du document document.open(); // Ecriture des datas document.add(new Paragraph("Hello World")); } catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } // Fermeture du document document.close(); } /** * Signature du document */ public static final boolean signPdf() throws IOException, DocumentException, Exception { // Vous devez preciser ici le chemin d'acces a votre clef pkcs12 String fileKey = "C:\\MonRep\\MaClef.p12" ; // et ici sa "passPhrase" String fileKeyPassword = "MonPassword" ; try { // Creation d'un KeyStore KeyStore ks = KeyStore.getInstance("pkcs12"); // Chargement du certificat p12 dans el magasin ks.load(new FileInputStream(fileKey), fileKeyPassword.toCharArray()); String alias = (String)ks.aliases().nextElement(); // Recupération de la clef privée PrivateKey key = (PrivateKey)ks.getKey(alias, fileKeyPassword.toCharArray()); // et de la chaine de certificats Certificate[] chain = ks.getCertificateChain(alias); // Lecture du document source PdfReader pdfReader = new PdfReader((new File(fname)).getAbsolutePath()); File outputFile = new File(fnameS); // Creation du tampon de signature PdfStamper pdfStamper; pdfStamper = PdfStamper.createSignature(pdfReader, null, '\0', outputFile); PdfSignatureAppearance sap = pdfStamper.getSignatureAppearance(); sap.setCrypto(key, chain, null, PdfSignatureAppearance.SELF_SIGNED); sap.setReason("Test SignPDF berthou.mc"); sap.setLocation(""); // Position du tampon sur la page (ici en bas a gauche page 1) sap.setVisibleSignature(new Rectangle(10, 10, 50, 30), 1, "sign_rbl"); pdfStamper.setFormFlattening(true); pdfStamper.close(); return true; } catch (Exception key) { throw new Exception(key); } } } Sample PDF generateMost Commented Posts |
CategoriesTagsRecent PostsRecent Comments
|
|
| |
4 Responses pour"Sign a PDF document"
Hi!
I must do something like this.
But I have one question.
Can I sing more than one time the same document? (sign by more than one person)
Thanks
hi u r code is very useful for i need code for verification on signed pdf please post it
Hi,
Thanks for posting such a useful topic. I followed the process described here and the pdf is signed. But while opening the file it’s showing
“At least one signature has problems”
Please help me to remove this error.
You might also want to look at this page: http://itextpdf.sourceforge.net/howtosign.html. It shows multiple examples of using iText (and iTextSharp) for signing PDF documents.
Ajouter une réponse