001: /*
002: * Copyright 2004 by Paulo Soares.
003: *
004: * The contents of this file are subject to the Mozilla Public License Version 1.1
005: * (the "License"); you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the License.
011: *
012: * The Original Code is 'iText, a free JAVA-PDF library'.
013: *
014: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
015: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
016: * All Rights Reserved.
017: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
018: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
019: *
020: * Contributor(s): all the names of the contributors are added in the source code
021: * where applicable.
022: *
023: * Alternatively, the contents of this file may be used under the terms of the
024: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
025: * provisions of LGPL are applicable instead of those above. If you wish to
026: * allow use of your version of this file only under the terms of the LGPL
027: * License and not to allow others to use your version of this file under
028: * the MPL, indicate your decision by deleting the provisions above and
029: * replace them with the notice and other provisions required by the LGPL.
030: * If you do not delete the provisions above, a recipient may use your version
031: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
032: *
033: * This library is free software; you can redistribute it and/or modify it
034: * under the terms of the MPL as stated above or under the terms of the GNU
035: * Library General Public License as published by the Free Software Foundation;
036: * either version 2 of the License, or any later version.
037: *
038: * This library is distributed in the hope that it will be useful, but WITHOUT
039: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
040: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
041: * details.
042: *
043: * If you didn't download this code from the following link, you should check if
044: * you aren't using an obsolete version:
045: * http://www.lowagie.com/iText/
046: */
047: package com.lowagie.text.pdf;
048:
049: import java.io.OutputStream;
050: import java.security.cert.Certificate;
051: import java.util.List;
052:
053: import com.lowagie.text.DocWriter;
054: import com.lowagie.text.DocumentException;
055: import com.lowagie.text.pdf.interfaces.PdfEncryptionSettings;
056: import com.lowagie.text.pdf.interfaces.PdfViewerPreferences;
057:
058: /**
059: * Concatenates PDF documents including form fields. The rules for the form field
060: * concatenation are the same as in Acrobat. All the documents are kept in memory unlike
061: * PdfCopy.
062: * @author Paulo Soares (psoares@consiste.pt)
063: */
064: public class PdfCopyFields implements PdfViewerPreferences,
065: PdfEncryptionSettings {
066:
067: private PdfCopyFieldsImp fc;
068:
069: /**
070: * Creates a new instance.
071: * @param os the output stream
072: * @throws DocumentException on error
073: */
074: public PdfCopyFields(OutputStream os) throws DocumentException {
075: fc = new PdfCopyFieldsImp(os);
076: }
077:
078: /**
079: * Creates a new instance.
080: * @param os the output stream
081: * @param pdfVersion the pdf version the output will have
082: * @throws DocumentException on error
083: */
084: public PdfCopyFields(OutputStream os, char pdfVersion)
085: throws DocumentException {
086: fc = new PdfCopyFieldsImp(os, pdfVersion);
087: }
088:
089: /**
090: * Concatenates a PDF document.
091: * @param reader the PDF document
092: * @throws DocumentException on error
093: */
094: public void addDocument(PdfReader reader) throws DocumentException {
095: fc.addDocument(reader);
096: }
097:
098: /**
099: * Concatenates a PDF document selecting the pages to keep. The pages are described as a
100: * <CODE>List</CODE> of <CODE>Integer</CODE>. The page ordering can be changed but
101: * no page repetitions are allowed.
102: * @param reader the PDF document
103: * @param pagesToKeep the pages to keep
104: * @throws DocumentException on error
105: */
106: public void addDocument(PdfReader reader, List pagesToKeep)
107: throws DocumentException {
108: fc.addDocument(reader, pagesToKeep);
109: }
110:
111: /**
112: * Concatenates a PDF document selecting the pages to keep. The pages are described as
113: * ranges. The page ordering can be changed but
114: * no page repetitions are allowed.
115: * @param reader the PDF document
116: * @param ranges the comma separated ranges as described in {@link SequenceList}
117: * @throws DocumentException on error
118: */
119: public void addDocument(PdfReader reader, String ranges)
120: throws DocumentException {
121: fc.addDocument(reader, SequenceList.expand(ranges, reader
122: .getNumberOfPages()));
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: */
137: public void setEncryption(byte userPassword[],
138: byte ownerPassword[], int permissions,
139: boolean strength128Bits) throws DocumentException {
140: fc.setEncryption(userPassword, ownerPassword, permissions,
141: strength128Bits ? PdfWriter.STANDARD_ENCRYPTION_128
142: : PdfWriter.STANDARD_ENCRYPTION_40);
143: }
144:
145: /**
146: * Sets the encryption options for this document. The userPassword and the
147: * ownerPassword can be null or have zero length. In this case the ownerPassword
148: * is replaced by a random string. The open permissions for the document can be
149: * AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
150: * AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
151: * The permissions can be combined by ORing them.
152: * @param strength true for 128 bit key length. false for 40 bit key length
153: * @param userPassword the user password. Can be null or empty
154: * @param ownerPassword the owner password. Can be null or empty
155: * @param permissions the user permissions
156: * @throws DocumentException if the document is already open
157: */
158: public void setEncryption(boolean strength, String userPassword,
159: String ownerPassword, int permissions)
160: throws DocumentException {
161: setEncryption(DocWriter.getISOBytes(userPassword), DocWriter
162: .getISOBytes(ownerPassword), permissions, strength);
163: }
164:
165: /**
166: * Closes the output document.
167: */
168: public void close() {
169: fc.close();
170: }
171:
172: /**
173: * Opens the document. This is usually not needed as addDocument() will do it
174: * automatically.
175: */
176: public void open() {
177: fc.openDoc();
178: }
179:
180: /**
181: * Adds JavaScript to the global document
182: * @param js the JavaScript
183: */
184: public void addJavaScript(String js) {
185: fc.addJavaScript(js, !PdfEncodings.isPdfDocEncoding(js));
186: }
187:
188: /**
189: * Sets the bookmarks. The list structure is defined in
190: * <CODE>SimpleBookmark#</CODE>.
191: * @param outlines the bookmarks or <CODE>null</CODE> to remove any
192: */
193: public void setOutlines(List outlines) {
194: fc.setOutlines(outlines);
195: }
196:
197: /** Gets the underlying PdfWriter.
198: * @return the underlying PdfWriter
199: */
200: public PdfWriter getWriter() {
201: return fc;
202: }
203:
204: /**
205: * Gets the 1.5 compression status.
206: * @return <code>true</code> if the 1.5 compression is on
207: */
208: public boolean isFullCompression() {
209: return fc.isFullCompression();
210: }
211:
212: /**
213: * Sets the document's compression to the new 1.5 mode with object streams and xref
214: * streams. It can be set at any time but once set it can't be unset.
215: * <p>
216: * If set before opening the document it will also set the pdf version to 1.5.
217: */
218: public void setFullCompression() {
219: fc.setFullCompression();
220: }
221:
222: /**
223: * @see com.lowagie.text.pdf.interfaces.PdfEncryptionSettings#setEncryption(byte[], byte[], int, int)
224: */
225: public void setEncryption(byte[] userPassword,
226: byte[] ownerPassword, int permissions, int encryptionType)
227: throws DocumentException {
228: fc.setEncryption(userPassword, ownerPassword, permissions,
229: encryptionType);
230: }
231:
232: /**
233: * @see com.lowagie.text.pdf.interfaces.PdfViewerPreferences#addViewerPreference(com.lowagie.text.pdf.PdfName, com.lowagie.text.pdf.PdfObject)
234: */
235: public void addViewerPreference(PdfName key, PdfObject value) {
236: fc.addViewerPreference(key, value);
237: }
238:
239: /**
240: * @see com.lowagie.text.pdf.interfaces.PdfViewerPreferences#setViewerPreferences(int)
241: */
242: public void setViewerPreferences(int preferences) {
243: fc.setViewerPreferences(preferences);
244: }
245:
246: /**
247: * @see com.lowagie.text.pdf.interfaces.PdfEncryptionSettings#setEncryption(java.security.cert.Certificate[], int[], int)
248: */
249: public void setEncryption(Certificate[] certs, int[] permissions,
250: int encryptionType) throws DocumentException {
251: fc.setEncryption(certs, permissions, encryptionType);
252: }
253: }
|