001/* 002 * Copyright (C) 2016 Hobrasoft s.r.o. 003 * 004 * This program is free software: you can redistribute it and/or modify 005 * it under the terms of the GNU Affero General Public License as published by 006 * the Free Software Foundation, either version 3 of the License, or 007 * (at your option) any later version. 008 * 009 * This program is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 * GNU Affero General Public License for more details. 013 * 014 * You should have received a copy of the GNU Affero General Public License 015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 016 */ 017package cz.hobrasoft.pdfmu.operation.args; 018 019import com.itextpdf.text.pdf.PdfReader; 020import com.itextpdf.text.pdf.PdfStamper; 021import cz.hobrasoft.pdfmu.operation.OperationException; 022import net.sourceforge.argparse4j.inf.ArgumentParser; 023import net.sourceforge.argparse4j.inf.Namespace; 024 025// Handle unset output file (in-place change) 026public class InOutPdfArgs implements ArgsConfiguration, AutoCloseable { 027 028 private InPdfArgs in; 029 private OutPdfArgs out; 030 031 public InOutPdfArgs() { 032 // Allow append by default 033 this(true); 034 } 035 036 public InOutPdfArgs(boolean allowAppend) { 037 this("IN.pdf", allowAppend); 038 } 039 040 public InOutPdfArgs(String metavarIn) { 041 // Allow append by default 042 this(metavarIn, true); 043 } 044 045 public InOutPdfArgs(String metavarIn, boolean allowAppend) { 046 in = new InPdfArgs(metavarIn); 047 out = new OutPdfArgs(metavarIn, allowAppend); 048 } 049 050 @Override 051 public void addArguments(ArgumentParser parser) { 052 in.addArguments(parser); 053 out.addArguments(parser); 054 } 055 056 @Override 057 public void setFromNamespace(Namespace namespace) { 058 in.setFromNamespace(namespace); 059 out.setFromNamespace(namespace); 060 out.setDefaultFile(in.getFile()); 061 } 062 063 public void open() throws OperationException { 064 open(false); 065 } 066 067 public void openSignature() throws OperationException { 068 open(true); 069 } 070 071 public void open(boolean signature) throws OperationException { 072 open(signature, '\0'); 073 } 074 075 public void open(boolean signature, char pdfVersion) throws OperationException { 076 PdfReader reader = in.open(); 077 out.open(reader, signature, pdfVersion); 078 } 079 080 public PdfReader getPdfReader() { 081 return in.getPdfReader(); 082 } 083 084 public PdfStamper getPdfStamper() { 085 return out.getPdfStamper(); 086 } 087 088 @Override 089 public void close() throws OperationException { 090 close(false); 091 } 092 093 public void close(boolean success) throws OperationException { 094 out.close(success); 095 in.close(); 096 } 097 098 public InPdfArgs getIn() { 099 return in; 100 } 101 102 // :) 103 public OutPdfArgs getOut() { 104 return out; 105 } 106 107}