KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > PdfCopyFields


1 /*
2  * Copyright 2004 by Paulo Soares.
3  *
4  * The contents of this file are subject to the Mozilla Public License Version 1.1
5  * (the "License"); you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the License.
11  *
12  * The Original Code is 'iText, a free JAVA-PDF library'.
13  *
14  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
15  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
16  * All Rights Reserved.
17  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
18  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
19  *
20  * Contributor(s): all the names of the contributors are added in the source code
21  * where applicable.
22  *
23  * Alternatively, the contents of this file may be used under the terms of the
24  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
25  * provisions of LGPL are applicable instead of those above. If you wish to
26  * allow use of your version of this file only under the terms of the LGPL
27  * License and not to allow others to use your version of this file under
28  * the MPL, indicate your decision by deleting the provisions above and
29  * replace them with the notice and other provisions required by the LGPL.
30  * If you do not delete the provisions above, a recipient may use your version
31  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
32  *
33  * This library is free software; you can redistribute it and/or modify it
34  * under the terms of the MPL as stated above or under the terms of the GNU
35  * Library General Public License as published by the Free Software Foundation;
36  * either version 2 of the License, or any later version.
37  *
38  * This library is distributed in the hope that it will be useful, but WITHOUT
39  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
40  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
41  * details.
42  *
43  * If you didn't download this code from the following link, you should check if
44  * you aren't using an obsolete version:
45  * http://www.lowagie.com/iText/
46  */

47 package com.lowagie.text.pdf;
48
49 import java.io.OutputStream JavaDoc;
50 import java.security.cert.Certificate JavaDoc;
51 import java.util.List JavaDoc;
52
53 import com.lowagie.text.DocWriter;
54 import com.lowagie.text.DocumentException;
55 import com.lowagie.text.pdf.interfaces.PdfEncryptionSettings;
56 import com.lowagie.text.pdf.interfaces.PdfViewerPreferences;
57
58 /**
59  * Concatenates PDF documents including form fields. The rules for the form field
60  * concatenation are the same as in Acrobat. All the documents are kept in memory unlike
61  * PdfCopy.
62  * @author Paulo Soares (psoares@consiste.pt)
63  */

64 public class PdfCopyFields
65     implements PdfViewerPreferences, PdfEncryptionSettings {
66     
67     private PdfCopyFieldsImp fc;
68     
69     /**
70      * Creates a new instance.
71      * @param os the output stream
72      * @throws DocumentException on error
73      */

74     public PdfCopyFields(OutputStream JavaDoc os) throws DocumentException {
75         fc = new PdfCopyFieldsImp(os);
76     }
77     
78     /**
79      * Creates a new instance.
80      * @param os the output stream
81      * @param pdfVersion the pdf version the output will have
82      * @throws DocumentException on error
83      */

84     public PdfCopyFields(OutputStream JavaDoc os, char pdfVersion) throws DocumentException {
85         fc = new PdfCopyFieldsImp(os, pdfVersion);
86     }
87     
88     /**
89      * Concatenates a PDF document.
90      * @param reader the PDF document
91      * @throws DocumentException on error
92      */

93     public void addDocument(PdfReader reader) throws DocumentException {
94         fc.addDocument(reader);
95     }
96     
97     /**
98      * Concatenates a PDF document selecting the pages to keep. The pages are described as a
99      * <CODE>List</CODE> of <CODE>Integer</CODE>. The page ordering can be changed but
100      * no page repetitions are allowed.
101      * @param reader the PDF document
102      * @param pagesToKeep the pages to keep
103      * @throws DocumentException on error
104      */

105     public void addDocument(PdfReader reader, List JavaDoc pagesToKeep) throws DocumentException {
106         fc.addDocument(reader, pagesToKeep);
107     }
108
109     /**
110      * Concatenates a PDF document selecting the pages to keep. The pages are described as
111      * ranges. The page ordering can be changed but
112      * no page repetitions are allowed.
113      * @param reader the PDF document
114      * @param ranges the comma separated ranges as described in {@link SequenceList}
115      * @throws DocumentException on error
116      */

117     public void addDocument(PdfReader reader, String JavaDoc ranges) throws DocumentException {
118         fc.addDocument(reader, SequenceList.expand(ranges, reader.getNumberOfPages()));
119     }
120
121     /** Sets the encryption options for this document. The userPassword and the
122      * ownerPassword can be null or have zero length. In this case the ownerPassword
123      * is replaced by a random string. The open permissions for the document can be
124      * AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
125      * AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
126      * The permissions can be combined by ORing them.
127      * @param userPassword the user password. Can be null or empty
128      * @param ownerPassword the owner password. Can be null or empty
129      * @param permissions the user permissions
130      * @param strength128Bits <code>true</code> for 128 bit key length, <code>false</code> for 40 bit key length
131      * @throws DocumentException if the document is already open
132      */

133     public void setEncryption(byte userPassword[], byte ownerPassword[], int permissions, boolean strength128Bits) throws DocumentException {
134         fc.setEncryption(userPassword, ownerPassword, permissions, strength128Bits ? PdfWriter.STANDARD_ENCRYPTION_128 : PdfWriter.STANDARD_ENCRYPTION_40);
135     }
136     
137     /**
138      * Sets the encryption options for this document. The userPassword and the
139      * ownerPassword can be null or have zero length. In this case the ownerPassword
140      * is replaced by a random string. The open permissions for the document can be
141      * AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
142      * AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
143      * The permissions can be combined by ORing them.
144      * @param strength true for 128 bit key length. false for 40 bit key length
145      * @param userPassword the user password. Can be null or empty
146      * @param ownerPassword the owner password. Can be null or empty
147      * @param permissions the user permissions
148      * @throws DocumentException if the document is already open
149      */

150     public void setEncryption(boolean strength, String JavaDoc userPassword, String JavaDoc ownerPassword, int permissions) throws DocumentException {
151         setEncryption(DocWriter.getISOBytes(userPassword), DocWriter.getISOBytes(ownerPassword), permissions, strength);
152     }
153  
154     /**
155      * Closes the output document.
156      */

157     public void close() {
158         fc.close();
159     }
160
161     /**
162      * Opens the document. This is usually not needed as addDocument() will do it
163      * automatically.
164      */

165     public void open() {
166         fc.openDoc();
167     }
168
169     /**
170      * Adds JavaScript to the global document
171      * @param js the JavaScript
172      */

173     public void addJavaScript(String JavaDoc js) {
174         fc.addJavaScript(js, !PdfEncodings.isPdfDocEncoding(js));
175     }
176
177     /**
178      * Sets the bookmarks. The list structure is defined in
179      * <CODE>SimpleBookmark#</CODE>.
180      * @param outlines the bookmarks or <CODE>null</CODE> to remove any
181      */

182     public void setOutlines(List JavaDoc outlines) {
183         fc.setOutlines(outlines);
184     }
185     
186     /** Gets the underlying PdfWriter.
187      * @return the underlying PdfWriter
188      */

189     public PdfWriter getWriter() {
190         return fc;
191     }
192
193     /**
194      * Gets the 1.5 compression status.
195      * @return <code>true</code> if the 1.5 compression is on
196      */

197     public boolean isFullCompression() {
198         return fc.isFullCompression();
199     }
200     
201     /**
202      * Sets the document's compression to the new 1.5 mode with object streams and xref
203      * streams. It can be set at any time but once set it can't be unset.
204      * <p>
205      * If set before opening the document it will also set the pdf version to 1.5.
206      */

207     public void setFullCompression() {
208         fc.setFullCompression();
209     }
210
211     /**
212      * @see com.lowagie.text.pdf.interfaces.PdfEncryptionSettings#setEncryption(byte[], byte[], int, int)
213      */

214     public void setEncryption(byte[] userPassword, byte[] ownerPassword, int permissions, int encryptionType) throws DocumentException {
215         fc.setEncryption(userPassword, ownerPassword, permissions, encryptionType);
216     }
217
218     /**
219      * @see com.lowagie.text.pdf.interfaces.PdfViewerPreferences#addViewerPreference(com.lowagie.text.pdf.PdfName, com.lowagie.text.pdf.PdfObject)
220      */

221     public void addViewerPreference(PdfName key, PdfObject value) {
222         fc.addViewerPreference(key, value);
223     }
224
225     /**
226      * @see com.lowagie.text.pdf.interfaces.PdfViewerPreferences#setViewerPreferences(int)
227      */

228     public void setViewerPreferences(int preferences) {
229         fc.setViewerPreferences(preferences);
230     }
231
232     /**
233      * @see com.lowagie.text.pdf.interfaces.PdfEncryptionSettings#setEncryption(java.security.cert.Certificate[], int[], int)
234      */

235     public void setEncryption(Certificate JavaDoc[] certs, int[] permissions, int encryptionType) throws DocumentException {
236         fc.setEncryption(certs, permissions, encryptionType);
237     }
238 }
Popular Tags