1 /*
2  * $Id$
3  *
4  * Copyright 2009 Holger Plankermann (inspired by Paulo Soares)
5  *
6  * The contents of this file are subject to the Mozilla Public License Version 1.1
7  * (the "License"); you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the License.
13  *
14  * The Original Code is 'iText, a free JAVA-PDF library'.
15  *
16  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17  * the Initial Developer are Copyright (C) 1999-2009 by Bruno Lowagie.
18  * All Rights Reserved.
19  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20  * are Copyright (C) 2000-2009 by Paulo Soares. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source code
23  * where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above.  If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34  *
35  * This library is free software; you can redistribute it and/or modify it
36  * under the terms of the MPL as stated above or under the terms of the GNU
37  * Library General Public License as published by the Free Software Foundation;
38  * either version 2 of the License, or any later version.
39  *
40  * This library is distributed in the hope that it will be useful, but WITHOUT
41  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43  * details.
44  *
45  * If you didn't download this code from the following link, you should check if
46  * you aren't using an obsolete version:
47  * http://www.lowagie.com/iText/
48  */
49 
50 package com.lowagie.text.pdf;
51 
52 import java.io.IOException;
53 import java.io.OutputStream;
54 import java.security.cert.Certificate;
55 import java.util.List;
56 
57 import com.lowagie.text.DocWriter;
58 import com.lowagie.text.DocumentException;
59 import com.lowagie.text.pdf.interfaces.PdfEncryptionSettings;
60 import com.lowagie.text.pdf.interfaces.PdfViewerPreferences;
61 
62 /**
63  * Allows you to add one (or more) existing PDF document(s) to
64  * create a new PDF and add the form of another PDF document to
65  * this new PDF.
66  * @since 2.1.5
67  */
68 public class PdfCopyForms
69 	implements PdfViewerPreferences, PdfEncryptionSettings {
70 
71 	/** The class with the actual implementations. */
72     private PdfCopyFormsImp fc;
73 
74     /**
75      * Creates a new instance.
76      * @param os the output stream
77      * @throws DocumentException on error
78      */
PdfCopyForms(OutputStream os)79     public PdfCopyForms(OutputStream os) throws DocumentException {
80         fc = new PdfCopyFormsImp(os);
81     }
82 
83     /**
84      * Concatenates a PDF document.
85      * @param reader the PDF document
86      * @throws DocumentException on error
87      */
addDocument(PdfReader reader)88     public void addDocument(PdfReader reader) throws DocumentException, IOException {
89         fc.addDocument(reader);
90     }
91 
92     /**
93      * Concatenates a PDF document selecting the pages to keep. The pages are described as a
94      * <CODE>List</CODE> of <CODE>Integer</CODE>. The page ordering can be changed but
95      * no page repetitions are allowed.
96      * @param reader the PDF document
97      * @param pagesToKeep the pages to keep
98      * @throws DocumentException on error
99      */
addDocument(PdfReader reader, List pagesToKeep)100     public void addDocument(PdfReader reader, List pagesToKeep) throws DocumentException, IOException {
101         fc.addDocument(reader, pagesToKeep);
102     }
103 
104     /**
105      * Concatenates a PDF document selecting the pages to keep. The pages are described as
106      * ranges. The page ordering can be changed but
107      * no page repetitions are allowed.
108      * @param reader the PDF document
109      * @param ranges the comma separated ranges as described in {@link SequenceList}
110      * @throws DocumentException on error
111      */
addDocument(PdfReader reader, String ranges)112     public void addDocument(PdfReader reader, String ranges) throws DocumentException, IOException {
113         fc.addDocument(reader, SequenceList.expand(ranges, reader.getNumberOfPages()));
114     }
115 
116     /**
117      *Copies the form fields of this PDFDocument onto the PDF-Document which was added
118      * @param reader the PDF document
119      * @throws DocumentException on error
120      */
copyDocumentFields(PdfReader reader)121     public void copyDocumentFields(PdfReader reader) throws DocumentException{
122         fc.copyDocumentFields(reader);
123     }
124 
125     /** Sets the encryption options for this document. The userPassword and the
126      *  ownerPassword can be null or have zero length. In this case the ownerPassword
127      *  is replaced by a random string. The open permissions for the document can be
128      *  AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
129      *  AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
130      *  The permissions can be combined by ORing them.
131      * @param userPassword the user password. Can be null or empty
132      * @param ownerPassword the owner password. Can be null or empty
133      * @param permissions the user permissions
134      * @param strength128Bits <code>true</code> for 128 bit key length, <code>false</code> for 40 bit key length
135      * @throws DocumentException if the document is already open
136      */
setEncryption(byte userPassword[], byte ownerPassword[], int permissions, boolean strength128Bits)137     public void setEncryption(byte userPassword[], byte ownerPassword[], int permissions, boolean strength128Bits) throws DocumentException {
138     	fc.setEncryption(userPassword, ownerPassword, permissions, strength128Bits ? PdfWriter.STANDARD_ENCRYPTION_128 : PdfWriter.STANDARD_ENCRYPTION_40);
139     }
140 
141     /**
142      * Sets the encryption options for this document. The userPassword and the
143      *  ownerPassword can be null or have zero length. In this case the ownerPassword
144      *  is replaced by a random string. The open permissions for the document can be
145      *  AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
146      *  AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
147      *  The permissions can be combined by ORing them.
148      * @param strength true for 128 bit key length. false for 40 bit key length
149      * @param userPassword the user password. Can be null or empty
150      * @param ownerPassword the owner password. Can be null or empty
151      * @param permissions the user permissions
152      * @throws DocumentException if the document is already open
153      */
setEncryption(boolean strength, String userPassword, String ownerPassword, int permissions)154     public void setEncryption(boolean strength, String userPassword, String ownerPassword, int permissions) throws DocumentException {
155         setEncryption(DocWriter.getISOBytes(userPassword), DocWriter.getISOBytes(ownerPassword), permissions, strength);
156     }
157 
158     /**
159      * Closes the output document.
160      */
close()161     public void close() {
162         fc.close();
163     }
164 
165     /**
166      * Opens the document. This is usually not needed as addDocument() will do it
167      * automatically.
168      */
open()169     public void open() {
170         fc.openDoc();
171     }
172 
173     /**
174      * Adds JavaScript to the global document
175      * @param js the JavaScript
176      */
addJavaScript(String js)177     public void addJavaScript(String js) {
178         fc.addJavaScript(js, !PdfEncodings.isPdfDocEncoding(js));
179     }
180 
181     /**
182      * Sets the bookmarks. The list structure is defined in
183      * <CODE>SimpleBookmark#</CODE>.
184      * @param outlines the bookmarks or <CODE>null</CODE> to remove any
185      */
setOutlines(List outlines)186     public void setOutlines(List outlines) {
187         fc.setOutlines(outlines);
188     }
189 
190     /** Gets the underlying PdfWriter.
191      * @return the underlying PdfWriter
192      */
getWriter()193     public PdfWriter getWriter() {
194         return fc;
195     }
196 
197     /**
198      * Gets the 1.5 compression status.
199      * @return <code>true</code> if the 1.5 compression is on
200      */
isFullCompression()201     public boolean isFullCompression() {
202         return fc.isFullCompression();
203     }
204 
205     /**
206      * Sets the document's compression to the new 1.5 mode with object streams and xref
207      * streams. It can be set at any time but once set it can't be unset.
208      * <p>
209      * If set before opening the document it will also set the pdf version to 1.5.
210      */
setFullCompression()211     public void setFullCompression() {
212         fc.setFullCompression();
213     }
214 
215 	/**
216 	 * @see com.lowagie.text.pdf.interfaces.PdfEncryptionSettings#setEncryption(byte[], byte[], int, int)
217 	 */
setEncryption(byte[] userPassword, byte[] ownerPassword, int permissions, int encryptionType)218 	public void setEncryption(byte[] userPassword, byte[] ownerPassword, int permissions, int encryptionType) throws DocumentException {
219 		fc.setEncryption(userPassword, ownerPassword, permissions, encryptionType);
220 	}
221 
222 	/**
223 	 * @see com.lowagie.text.pdf.interfaces.PdfViewerPreferences#addViewerPreference(com.lowagie.text.pdf.PdfName, com.lowagie.text.pdf.PdfObject)
224 	 */
addViewerPreference(PdfName key, PdfObject value)225 	public void addViewerPreference(PdfName key, PdfObject value) {
226 		fc.addViewerPreference(key, value);
227 	}
228 
229 	/**
230 	 * @see com.lowagie.text.pdf.interfaces.PdfViewerPreferences#setViewerPreferences(int)
231 	 */
setViewerPreferences(int preferences)232 	public void setViewerPreferences(int preferences) {
233 		fc.setViewerPreferences(preferences);
234 	}
235 
236 	/**
237 	 * @see com.lowagie.text.pdf.interfaces.PdfEncryptionSettings#setEncryption(java.security.cert.Certificate[], int[], int)
238 	 */
setEncryption(Certificate[] certs, int[] permissions, int encryptionType)239 	public void setEncryption(Certificate[] certs, int[] permissions, int encryptionType) throws DocumentException {
240 		fc.setEncryption(certs, permissions, encryptionType);
241 	}
242 }