1+ import java .io .File ;
2+ import java .io .FileInputStream ;
3+ import java .io .FileNotFoundException ;
4+ import java .io .FileOutputStream ;
5+ import java .io .IOException ;
6+ import java .security .KeyStore ;
7+ import java .security .KeyStoreException ;
8+ import java .security .NoSuchAlgorithmException ;
9+ import java .security .PrivateKey ;
10+ import java .security .UnrecoverableKeyException ;
11+ import java .util .NoSuchElementException ;
12+ import java .security .cert .Certificate ;
13+ import java .security .cert .CertificateException ;
14+
15+ import com .lowagie .text .pdf .PdfReader ;
16+ import com .lowagie .text .pdf .PdfSignatureAppearance ;
17+ import com .lowagie .text .pdf .PdfStamper ;
18+
19+ import org .apache .commons .io .*;
20+
21+ public class BatchPDFSign {
22+
23+ private static PrivateKey privateKey ;
24+ private static Certificate [] certificateChain ;
25+
26+ private static String PRODUCTNAME = "BatchPDFSign" ;
27+ private static String VERSION = "version 1.0" ;
28+ private static String JAR_FILENAME = "JPdfSign.jar" ;
29+
30+ public static void main (String [] args ) {
31+
32+ if (args .length < 3 )
33+ showUsage ();
34+
35+ try {
36+
37+ boolean flgRename = true ;
38+
39+ String pkcs12FileName = args [0 ].trim ();
40+
41+ String PkcsPassword = args [1 ];
42+
43+ String pdfInputFileName = args [2 ];
44+ String pdfOutputFileName = pdfInputFileName + "_signed.pdf" ;
45+ if (args .length == 4 ) {
46+ pdfOutputFileName = args [3 ];
47+ flgRename = false ;
48+ }
49+ // PDF input file
50+ File InputFile = new File (pdfInputFileName );
51+ // Check PDF input file
52+ if (!InputFile .exists () || InputFile .isDirectory ()) {
53+ System .out .println ("" );
54+ System .out .println ("PDf file not found !" );
55+ showUsage ();
56+ }
57+
58+ readPrivateKeyFromPKCS12 (pkcs12FileName , PkcsPassword );
59+
60+ PdfReader reader = null ;
61+ try {
62+ reader = new PdfReader (pdfInputFileName );
63+ } catch (IOException e ) {
64+ System .err .println (
65+ "An unknown error accoured while opening the input PDF file: \" " + pdfInputFileName + "\" " );
66+ e .printStackTrace ();
67+ System .exit (-1 );
68+ }
69+ FileOutputStream fout = null ;
70+ try {
71+ fout = new FileOutputStream (pdfOutputFileName );
72+ } catch (FileNotFoundException e ) {
73+ System .err .println (
74+ "An unknown error accoured while opening the output PDF file: \" " + pdfOutputFileName + "\" " );
75+ e .printStackTrace ();
76+ System .exit (-1 );
77+ }
78+ PdfStamper stp = null ;
79+ try {
80+ stp = PdfStamper .createSignature (reader , fout , '\0' );
81+ PdfSignatureAppearance sap = stp .getSignatureAppearance ();
82+ sap .setCrypto (privateKey , certificateChain , null , PdfSignatureAppearance .WINCER_SIGNED );
83+ stp .close ();
84+ } catch (Exception e ) {
85+ System .err .println ("An unknown error accoured while signing the PDF file:" );
86+ e .printStackTrace ();
87+ System .exit (-1 );
88+ }
89+ InputFile .delete ();
90+ // Renaming signed PDF file
91+ if (flgRename ) {
92+ try {
93+ FileUtils .moveFile (
94+ FileUtils .getFile (pdfOutputFileName ),
95+ FileUtils .getFile (pdfInputFileName ));
96+ } catch (IOException e ) {
97+ // TODO Auto-generated catch block
98+ System .err .println ("Error renaming output file from " + pdfOutputFileName + " to " + pdfInputFileName );
99+ e .printStackTrace ();
100+
101+ }
102+ }
103+
104+ } catch (KeyStoreException kse ) {
105+ System .err .println ("An unknown error accoured while initializing the KeyStore instance:" );
106+ kse .printStackTrace ();
107+ System .exit (-1 );
108+ }
109+ }
110+ /**
111+ * Reads private Key
112+ * @param pkcs12FileName
113+ * @throws KeyStoreException
114+ */
115+ protected static void readPrivateKeyFromPKCS12 (String pkcs12FileName , String pkcs12Password ) throws KeyStoreException {
116+
117+ KeyStore ks = null ;
118+
119+ try {
120+ ks = KeyStore .getInstance ("pkcs12" );
121+ ks .load (new FileInputStream (pkcs12FileName ), pkcs12Password .toCharArray ());
122+ } catch (NoSuchAlgorithmException e ) {
123+ System .err .println ("An unknown error accoured while reading the PKCS#12 file (Password could be wrong):" );
124+ e .printStackTrace ();
125+ System .exit (-1 );
126+ } catch (CertificateException e ) {
127+ System .err .println ("An unknown error accoured while reading the PKCS#12 file:" );
128+ e .printStackTrace ();
129+ System .exit (-1 );
130+ } catch (FileNotFoundException e ) {
131+ System .err .println ("Unable to open the PKCS#12 keystore file \" " + pkcs12FileName + "\" :" );
132+ System .err .println ("The file does not exists or missing read permission." );
133+ System .exit (-1 );
134+ } catch (IOException e ) {
135+ System .err .println ("An unknown error accoured while reading the PKCS#12 file: IOException" );
136+ e .printStackTrace ();
137+ System .exit (-1 );
138+ }
139+ String alias = "" ;
140+ try {
141+ alias = (String ) ks .aliases ().nextElement ();
142+ privateKey = (PrivateKey ) ks .getKey (alias , pkcs12Password .toCharArray ());
143+ } catch (NoSuchElementException e ) {
144+ System .err .println ("An unknown error accoured while retrieving the private key:" );
145+ System .err .println ("The selected PKCS#12 file does not contain any private keys." );
146+ e .printStackTrace ();
147+ System .exit (-1 );
148+ } catch (NoSuchAlgorithmException e ) {
149+ System .err .println ("An unknown error accoured while retrieving the private key:" );
150+ e .printStackTrace ();
151+ System .exit (-1 );
152+ } catch (UnrecoverableKeyException e ) {
153+ System .err .println ("An unknown error accoured while retrieving the private key:" );
154+ e .printStackTrace ();
155+ System .exit (-1 );
156+ }
157+ certificateChain = ks .getCertificateChain (alias );
158+ }
159+ /**
160+ * Message shown when error in parameters
161+ */
162+ public static void showUsage () {
163+ System .out .println ("jPdfSign v" + VERSION + " \n " );
164+ System .out .println (PRODUCTNAME + " usage:" );
165+ System .out .println ("\n For using a PKCS#12 (.p12) file as signature certificate and private key source:" );
166+ System .out .print ("\t java -jar " + JAR_FILENAME );
167+ System .out .println (" <pkcs12FileName> <pkcsPassword> <pdfInputFileName> <pdfOutputFileName>" );
168+ System .out .println ("\n <pdfOutputFileName> is optional" );
169+ System .exit (0 );
170+ }
171+
172+ }
0 commit comments