001: /*
002: * Copyright 2003 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:
048: package com.lowagie.text.pdf;
049:
050: import java.io.File;
051: import java.io.FileInputStream;
052: import java.io.IOException;
053: import java.io.InputStream;
054: import java.net.URL;
055:
056: import com.lowagie.text.pdf.collection.PdfCollectionItem;
057:
058: /** Specifies a file or an URL. The file can be extern or embedded.
059: *
060: * @author Paulo Soares (psoares@consiste.pt)
061: */
062: public class PdfFileSpecification extends PdfDictionary {
063: protected PdfWriter writer;
064: protected PdfIndirectReference ref;
065:
066: /** Creates a new instance of PdfFileSpecification. The static methods are preferred. */
067: public PdfFileSpecification() {
068: super (PdfName.FILESPEC);
069: }
070:
071: /**
072: * Creates a file specification of type URL.
073: * @param writer the <CODE>PdfWriter</CODE>
074: * @param url the URL
075: * @return the file specification
076: */
077: public static PdfFileSpecification url(PdfWriter writer, String url) {
078: PdfFileSpecification fs = new PdfFileSpecification();
079: fs.writer = writer;
080: fs.put(PdfName.FS, PdfName.URL);
081: fs.put(PdfName.F, new PdfString(url));
082: return fs;
083: }
084:
085: /**
086: * Creates a file specification with the file embedded. The file may
087: * come from the file system or from a byte array. The data is flate compressed.
088: * @param writer the <CODE>PdfWriter</CODE>
089: * @param filePath the file path
090: * @param fileDisplay the file information that is presented to the user
091: * @param fileStore the byte array with the file. If it is not <CODE>null</CODE>
092: * it takes precedence over <CODE>filePath</CODE>
093: * @throws IOException on error
094: * @return the file specification
095: */
096: public static PdfFileSpecification fileEmbedded(PdfWriter writer,
097: String filePath, String fileDisplay, byte fileStore[])
098: throws IOException {
099: return fileEmbedded(writer, filePath, fileDisplay, fileStore,
100: true);
101: }
102:
103: /**
104: * Creates a file specification with the file embedded. The file may
105: * come from the file system or from a byte array.
106: * @param writer the <CODE>PdfWriter</CODE>
107: * @param filePath the file path
108: * @param fileDisplay the file information that is presented to the user
109: * @param fileStore the byte array with the file. If it is not <CODE>null</CODE>
110: * it takes precedence over <CODE>filePath</CODE>
111: * @param compress sets the compression on the data. Multimedia content will benefit little
112: * from compression
113: * @throws IOException on error
114: * @return the file specification
115: */
116: public static PdfFileSpecification fileEmbedded(PdfWriter writer,
117: String filePath, String fileDisplay, byte fileStore[],
118: boolean compress) throws IOException {
119: return fileEmbedded(writer, filePath, fileDisplay, fileStore,
120: compress, null, null);
121: }
122:
123: /**
124: * Creates a file specification with the file embedded. The file may
125: * come from the file system or from a byte array.
126: * @param writer the <CODE>PdfWriter</CODE>
127: * @param filePath the file path
128: * @param fileDisplay the file information that is presented to the user
129: * @param fileStore the byte array with the file. If it is not <CODE>null</CODE>
130: * it takes precedence over <CODE>filePath</CODE>
131: * @param compress sets the compression on the data. Multimedia content will benefit little
132: * from compression
133: * @param mimeType the optional mimeType
134: * @param fileParameter the optional extra file parameters such as the creation or modification date
135: * @throws IOException on error
136: * @return the file specification
137: */
138: public static PdfFileSpecification fileEmbedded(PdfWriter writer,
139: String filePath, String fileDisplay, byte fileStore[],
140: boolean compress, String mimeType,
141: PdfDictionary fileParameter) throws IOException {
142: PdfFileSpecification fs = new PdfFileSpecification();
143: fs.writer = writer;
144: fs.put(PdfName.F, new PdfString(fileDisplay));
145: fs.setUnicodeFileName(fileDisplay, false);
146: PdfStream stream;
147: InputStream in = null;
148: PdfIndirectReference ref;
149: PdfIndirectReference refFileLength;
150: try {
151: refFileLength = writer.getPdfIndirectReference();
152: if (fileStore == null) {
153: File file = new File(filePath);
154: if (file.canRead()) {
155: in = new FileInputStream(filePath);
156: } else {
157: if (filePath.startsWith("file:/")
158: || filePath.startsWith("http://")
159: || filePath.startsWith("https://")
160: || filePath.startsWith("jar:")) {
161: in = new URL(filePath).openStream();
162: } else {
163: in = BaseFont.getResourceStream(filePath);
164: if (in == null)
165: throw new IOException(filePath
166: + " not found as file or resource.");
167: }
168: }
169: stream = new PdfStream(in, writer);
170: } else
171: stream = new PdfStream(fileStore);
172: stream.put(PdfName.TYPE, PdfName.EMBEDDEDFILE);
173: if (compress)
174: stream.flateCompress();
175: stream.put(PdfName.PARAMS, refFileLength);
176: if (mimeType != null)
177: stream.put(PdfName.SUBTYPE, new PdfName(mimeType));
178: ref = writer.addToBody(stream).getIndirectReference();
179: if (fileStore == null) {
180: stream.writeLength();
181: }
182: PdfDictionary params = new PdfDictionary();
183: if (fileParameter != null)
184: params.merge(fileParameter);
185: params.put(PdfName.SIZE, new PdfNumber(stream
186: .getRawLength()));
187: writer.addToBody(params, refFileLength);
188: } finally {
189: if (in != null)
190: try {
191: in.close();
192: } catch (Exception e) {
193: }
194: }
195: PdfDictionary f = new PdfDictionary();
196: f.put(PdfName.F, ref);
197: f.put(PdfName.UF, ref);
198: fs.put(PdfName.EF, f);
199: return fs;
200: }
201:
202: /**
203: * Creates a file specification for an external file.
204: * @param writer the <CODE>PdfWriter</CODE>
205: * @param filePath the file path
206: * @return the file specification
207: */
208: public static PdfFileSpecification fileExtern(PdfWriter writer,
209: String filePath) {
210: PdfFileSpecification fs = new PdfFileSpecification();
211: fs.writer = writer;
212: fs.put(PdfName.F, new PdfString(filePath));
213: fs.setUnicodeFileName(filePath, false);
214: return fs;
215: }
216:
217: /**
218: * Gets the indirect reference to this file specification.
219: * Multiple invocations will retrieve the same value.
220: * @throws IOException on error
221: * @return the indirect reference
222: */
223: public PdfIndirectReference getReference() throws IOException {
224: if (ref != null)
225: return ref;
226: ref = writer.addToBody(this ).getIndirectReference();
227: return ref;
228: }
229:
230: /**
231: * Sets the file name (the key /F) string as an hex representation
232: * to support multi byte file names. The name must have the slash and
233: * backslash escaped according to the file specification rules
234: * @param fileName the file name as a byte array
235: */
236: public void setMultiByteFileName(byte fileName[]) {
237: put(PdfName.F, new PdfString(fileName).setHexWriting(true));
238: }
239:
240: /**
241: * Adds the unicode file name (the key /UF). This entry was introduced
242: * in PDF 1.7. The filename must have the slash and backslash escaped
243: * according to the file specification rules.
244: * @param filename the filename
245: * @param unicode if true, the filename is UTF-16BE encoded; otherwise PDFDocEncoding is used;
246: */
247: public void setUnicodeFileName(String filename, boolean unicode) {
248: put(PdfName.UF, new PdfString(filename,
249: unicode ? PdfObject.TEXT_UNICODE
250: : PdfObject.TEXT_PDFDOCENCODING));
251: }
252:
253: /**
254: * Sets a flag that indicates whether an external file referenced by the file
255: * specification is volatile. If the value is true, applications should never
256: * cache a copy of the file.
257: * @param volatile_file if true, the external file should not be cached
258: */
259: public void setVolatile(boolean volatile_file) {
260: put(PdfName.V, new PdfBoolean(volatile_file));
261: }
262:
263: /**
264: * Adds a description for the file that is specified here.
265: * @param description some text
266: * @param unicode if true, the text is added as a unicode string
267: */
268: public void addDescription(String description, boolean unicode) {
269: put(PdfName.DESC, new PdfString(description,
270: unicode ? PdfObject.TEXT_UNICODE
271: : PdfObject.TEXT_PDFDOCENCODING));
272: }
273:
274: /**
275: * Adds the Collection item dictionary.
276: */
277: public void addCollectionItem(PdfCollectionItem ci) {
278: put(PdfName.CI, ci);
279: }
280: }
|