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.args;
18  
19  import com.itextpdf.text.pdf.PdfReader;
20  import com.itextpdf.text.pdf.PdfStamper;
21  import cz.hobrasoft.pdfmu.operation.OperationException;
22  import net.sourceforge.argparse4j.inf.ArgumentParser;
23  import net.sourceforge.argparse4j.inf.Namespace;
24  
25  // Handle unset output file (in-place change)
26  public class InOutPdfArgs implements ArgsConfiguration, AutoCloseable {
27  
28      private InPdfArgs in;
29      private OutPdfArgs out;
30  
31      public InOutPdfArgs() {
32          // Allow append by default
33          this(true);
34      }
35  
36      public InOutPdfArgs(boolean allowAppend) {
37          this("IN.pdf", allowAppend);
38      }
39  
40      public InOutPdfArgs(String metavarIn) {
41          // Allow append by default
42          this(metavarIn, true);
43      }
44  
45      public InOutPdfArgs(String metavarIn, boolean allowAppend) {
46          in = new InPdfArgs(metavarIn);
47          out = new OutPdfArgs(metavarIn, allowAppend);
48      }
49  
50      @Override
51      public void addArguments(ArgumentParser parser) {
52          in.addArguments(parser);
53          out.addArguments(parser);
54      }
55  
56      @Override
57      public void setFromNamespace(Namespace namespace) {
58          in.setFromNamespace(namespace);
59          out.setFromNamespace(namespace);
60          out.setDefaultFile(in.getFile());
61      }
62  
63      public void open() throws OperationException {
64          open(false);
65      }
66  
67      public void openSignature() throws OperationException {
68          open(true);
69      }
70  
71      public void open(boolean signature) throws OperationException {
72          open(signature, '\0');
73      }
74  
75      public void open(boolean signature, char pdfVersion) throws OperationException {
76          PdfReader reader = in.open();
77          out.open(reader, signature, pdfVersion);
78      }
79  
80      public PdfReader getPdfReader() {
81          return in.getPdfReader();
82      }
83  
84      public PdfStamper getPdfStamper() {
85          return out.getPdfStamper();
86      }
87  
88      @Override
89      public void close() throws OperationException {
90          close(false);
91      }
92  
93      public void close(boolean success) throws OperationException {
94          out.close(success);
95          in.close();
96      }
97  
98      public InPdfArgs getIn() {
99          return in;
100     }
101 
102     // :)
103     public OutPdfArgs getOut() {
104         return out;
105     }
106 
107 }