View Javadoc
1   /* 
2    * Copyright (C) 2016 Hobrasoft s.r.o.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package cz.hobrasoft.pdfmu.operation.signature;
18  
19  import com.itextpdf.text.pdf.PdfSignatureAppearance;
20  import com.itextpdf.text.pdf.PdfStamper;
21  import cz.hobrasoft.pdfmu.operation.args.ArgsConfiguration;
22  import java.util.Calendar;
23  import java.util.logging.Logger;
24  import net.sourceforge.argparse4j.inf.ArgumentGroup;
25  import net.sourceforge.argparse4j.inf.ArgumentParser;
26  import net.sourceforge.argparse4j.inf.Namespace;
27  
28  /**
29   *
30   * @author <a href="mailto:filip.bartek@hobrasoft.cz">Filip Bartek</a>
31   */
32  class SignatureAppearanceParameters implements ArgsConfiguration {
33  
34      public String reason = null;
35      public String location = null;
36      public String contact = null;
37      public Calendar signDate = null;
38      public int certificationLevel = PdfSignatureAppearance.NOT_CERTIFIED;
39  
40      private static final Logger logger = Logger.getLogger(SignatureAppearanceParameters.class.getName());
41  
42      @Override
43      public void addArguments(ArgumentParser parser) {
44          ArgumentGroup group = parser.addArgumentGroup("signature metadata");
45          // TODO: Add description
46  
47          // The fields are explained in the following documents:
48          // digitalsignatures20130304.pdf : Section 2.3.3
49          // http://www.adobe.com/devnet-docs/acrobatetk/tools/DigSig/appearances.html
50          // https://www.pdfill.com/document_sign.html
51          group.addArgument("--reason")
52                  .help("What was the reason for signing? (default: <none>)")
53                  .type(String.class);
54          group.addArgument("--location")
55                  .help("Where was the document signed? (default: <none>)")
56                  .type(String.class);
57          group.addArgument("--contact")
58                  .help("signer contact information (defualt: <none>)")
59                  .type(String.class);
60          // TODO: Unify the help strings
61      }
62  
63      @Override
64      public void setFromNamespace(Namespace namespace) {
65          reason = namespace.getString("reason");
66          location = namespace.getString("location");
67          contact = namespace.getString("contact");
68          // TODO?: Expose `signDate`
69          // Note: Argparse4j does not seem to support time
70          // (namely {@link Calendar}) natively.
71          // TODO?: Expose `certificationLevel`
72      }
73  
74      public void configureSignatureAppearance(PdfSignatureAppearance sap) {
75          assert sap != null;
76          // Configure signature metadata
77          if (reason != null) {
78              logger.info(String.format("Reason: %s", reason));
79              sap.setReason(reason);
80          }
81          if (location != null) {
82              logger.info(String.format("Location: %s", location));
83              sap.setLocation(location);
84          }
85          if (contact != null) {
86              logger.info(String.format("Contact: %s", contact));
87              sap.setContact(contact);
88          }
89          if (signDate != null) {
90              // `setSignDate(null)` crashes
91              logger.info(String.format("Date: %s", signDate));
92              sap.setSignDate(signDate);
93          }
94          sap.setCertificationLevel(certificationLevel);
95  
96          // TODO?: Set signer's name
97          // digitalsignatures20130304.pdf : Code sample 2.12
98      }
99  
100     public PdfSignatureAppearance getSignatureAppearance(PdfStamper stp) {
101         assert stp != null;
102 
103         // Initialize the signature appearance
104         PdfSignatureAppearance sap = stp.getSignatureAppearance();
105         configureSignatureAppearance(sap);
106 
107         return sap;
108     }
109 
110 }