KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2003 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
48 package com.lowagie.text.pdf;
49
50 import java.io.File JavaDoc;
51 import java.io.FileInputStream JavaDoc;
52 import java.io.IOException JavaDoc;
53 import java.io.InputStream JavaDoc;
54 import java.net.URL JavaDoc;
55
56 import com.lowagie.text.pdf.collection.PdfCollectionItem;
57 /** Specifies a file or an URL. The file can be extern or embedded.
58  *
59  * @author Paulo Soares (psoares@consiste.pt)
60  */

61 public class PdfFileSpecification extends PdfDictionary {
62     protected PdfWriter writer;
63     protected PdfIndirectReference ref;
64     
65     /** Creates a new instance of PdfFileSpecification. The static methods are preferred. */
66     public PdfFileSpecification() {
67         super(PdfName.FILESPEC);
68     }
69     
70     /**
71      * Creates a file specification of type URL.
72      * @param writer the <CODE>PdfWriter</CODE>
73      * @param url the URL
74      * @return the file specification
75      */

76     public static PdfFileSpecification url(PdfWriter writer, String JavaDoc url) {
77         PdfFileSpecification fs = new PdfFileSpecification();
78         fs.writer = writer;
79         fs.put(PdfName.FS, PdfName.URL);
80         fs.put(PdfName.F, new PdfString(url));
81         return fs;
82     }
83
84     /**
85      * Creates a file specification with the file embedded. The file may
86      * come from the file system or from a byte array. The data is flate compressed.
87      * @param writer the <CODE>PdfWriter</CODE>
88      * @param filePath the file path
89      * @param fileDisplay the file information that is presented to the user
90      * @param fileStore the byte array with the file. If it is not <CODE>null</CODE>
91      * it takes precedence over <CODE>filePath</CODE>
92      * @throws IOException on error
93      * @return the file specification
94      */

95     public static PdfFileSpecification fileEmbedded(PdfWriter writer, String JavaDoc filePath, String JavaDoc fileDisplay, byte fileStore[]) throws IOException JavaDoc {
96         return fileEmbedded(writer, filePath, fileDisplay, fileStore, true);
97     }
98     
99     
100     /**
101      * Creates a file specification with the file embedded. The file may
102      * come from the file system or from a byte array.
103      * @param writer the <CODE>PdfWriter</CODE>
104      * @param filePath the file path
105      * @param fileDisplay the file information that is presented to the user
106      * @param fileStore the byte array with the file. If it is not <CODE>null</CODE>
107      * it takes precedence over <CODE>filePath</CODE>
108      * @param compress sets the compression on the data. Multimedia content will benefit little
109      * from compression
110      * @throws IOException on error
111      * @return the file specification
112      */

113     public static PdfFileSpecification fileEmbedded(PdfWriter writer, String JavaDoc filePath, String JavaDoc fileDisplay, byte fileStore[], boolean compress) throws IOException JavaDoc {
114         PdfFileSpecification fs = new PdfFileSpecification();
115         fs.writer = writer;
116         fs.put(PdfName.F, new PdfString(fileDisplay));
117         fs.setUnicodeFileName(fileDisplay, false);
118         PdfStream stream;
119         InputStream JavaDoc in = null;
120         PdfIndirectReference ref;
121         PdfIndirectReference refFileLength;
122         try {
123             refFileLength = writer.getPdfIndirectReference();
124             if (fileStore == null) {
125                 File JavaDoc file = new File JavaDoc(filePath);
126                 if (file.canRead()) {
127                     in = new FileInputStream JavaDoc(filePath);
128                 }
129                 else {
130                     if (filePath.startsWith("file:/") || filePath.startsWith("http://") || filePath.startsWith("https://") || filePath.startsWith("jar:")) {
131                         in = new URL JavaDoc(filePath).openStream();
132                     }
133                     else {
134                         in = BaseFont.getResourceStream(filePath);
135                         if (in == null)
136                             throw new IOException JavaDoc(filePath + " not found as file or resource.");
137                     }
138                 }
139                 stream = new PdfStream(in, writer);
140             }
141             else
142                 stream = new PdfStream(fileStore);
143             stream.put(PdfName.TYPE, PdfName.EMBEDDEDFILE);
144             if (compress)
145                 stream.flateCompress();
146             stream.put(PdfName.PARAMS, refFileLength);
147             ref = writer.addToBody(stream).getIndirectReference();
148             if (fileStore == null) {
149                 stream.writeLength();
150             }
151             PdfDictionary params = new PdfDictionary();
152             params.put(PdfName.SIZE, new PdfNumber(stream.getRawLength()));
153             writer.addToBody(params, refFileLength);
154         }
155         finally {
156             if (in != null)
157                 try{in.close();}catch(Exception JavaDoc e){}
158         }
159         PdfDictionary f = new PdfDictionary();
160         f.put(PdfName.F, ref);
161         f.put(PdfName.UF, ref);
162         fs.put(PdfName.EF, f);
163         return fs;
164     }
165     
166     /**
167      * Creates a file specification for an external file.
168      * @param writer the <CODE>PdfWriter</CODE>
169      * @param filePath the file path
170      * @return the file specification
171      */

172     public static PdfFileSpecification fileExtern(PdfWriter writer, String JavaDoc filePath) {
173         PdfFileSpecification fs = new PdfFileSpecification();
174         fs.writer = writer;
175         fs.put(PdfName.F, new PdfString(filePath));
176         fs.setUnicodeFileName(filePath, false);
177         return fs;
178     }
179     
180     /**
181      * Gets the indirect reference to this file specification.
182      * Multiple invocations will retrieve the same value.
183      * @throws IOException on error
184      * @return the indirect reference
185      */

186     public PdfIndirectReference getReference() throws IOException JavaDoc {
187         if (ref != null)
188             return ref;
189         ref = writer.addToBody(this).getIndirectReference();
190         return ref;
191     }
192     
193     /**
194      * Sets the file name (the key /F) string as an hex representation
195      * to support multi byte file names. The name must have the slash and
196      * backslash escaped according to the file specification rules
197      * @param fileName the file name as a byte array
198      */

199     public void setMultiByteFileName(byte fileName[]) {
200         put(PdfName.F, new PdfString(fileName).setHexWriting(true));
201     }
202     
203     /**
204      * Adds the unicode file name (the key /UF). This entry was introduced
205      * in PDF 1.7. The filename must have the slash and backslash escaped
206      * according to the file specification rules.
207      * @param filename the filename
208      * @param unicode if true, the filename is UTF-16BE encoded; otherwise PDFDocEncoding is used;
209      */

210     public void setUnicodeFileName(String JavaDoc filename, boolean unicode) {
211         put(PdfName.UF, new PdfString(filename, unicode ? PdfObject.TEXT_UNICODE : PdfObject.TEXT_PDFDOCENCODING));
212     }
213     
214     /**
215      * Sets a flag that indicates whether an external file referenced by the file
216      * specification is volatile. If the value is true, applications should never
217      * cache a copy of the file.
218      * @param volatile_file if true, the external file should not be cached
219      */

220     public void setVolatile(boolean volatile_file) {
221         put(PdfName.V, new PdfBoolean(volatile_file));
222     }
223     
224     /**
225      * Adds a description for the file that is specified here.
226      * @param description some text
227      * @param unicode if true, the text is added as a unicode string
228      */

229     public void addDescription(String JavaDoc description, boolean unicode) {
230         put(PdfName.DESC, new PdfString(description, unicode ? PdfObject.TEXT_UNICODE : PdfObject.TEXT_PDFDOCENCODING));
231     }
232     
233     /**
234      * Adds the Collection item dictionary.
235      */

236     public void addCollectionItem(PdfCollectionItem ci) {
237         put(PdfName.CI, ci);
238     }
239 }
Popular Tags