1 47 48 package com.lowagie.text.pdf; 49 50 import java.io.File ; 51 import java.io.FileInputStream ; 52 import java.io.IOException ; 53 import java.io.InputStream ; 54 import java.net.URL ; 55 56 import com.lowagie.text.pdf.collection.PdfCollectionItem; 57 61 public class PdfFileSpecification extends PdfDictionary { 62 protected PdfWriter writer; 63 protected PdfIndirectReference ref; 64 65 66 public PdfFileSpecification() { 67 super(PdfName.FILESPEC); 68 } 69 70 76 public static PdfFileSpecification url(PdfWriter writer, String 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 95 public static PdfFileSpecification fileEmbedded(PdfWriter writer, String filePath, String fileDisplay, byte fileStore[]) throws IOException { 96 return fileEmbedded(writer, filePath, fileDisplay, fileStore, true); 97 } 98 99 100 113 public static PdfFileSpecification fileEmbedded(PdfWriter writer, String filePath, String fileDisplay, byte fileStore[], boolean compress) throws IOException { 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 in = null; 120 PdfIndirectReference ref; 121 PdfIndirectReference refFileLength; 122 try { 123 refFileLength = writer.getPdfIndirectReference(); 124 if (fileStore == null) { 125 File file = new File (filePath); 126 if (file.canRead()) { 127 in = new FileInputStream (filePath); 128 } 129 else { 130 if (filePath.startsWith("file:/") || filePath.startsWith("http://") || filePath.startsWith("https://") || filePath.startsWith("jar:")) { 131 in = new URL (filePath).openStream(); 132 } 133 else { 134 in = BaseFont.getResourceStream(filePath); 135 if (in == null) 136 throw new IOException (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 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 172 public static PdfFileSpecification fileExtern(PdfWriter writer, String 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 186 public PdfIndirectReference getReference() throws IOException { 187 if (ref != null) 188 return ref; 189 ref = writer.addToBody(this).getIndirectReference(); 190 return ref; 191 } 192 193 199 public void setMultiByteFileName(byte fileName[]) { 200 put(PdfName.F, new PdfString(fileName).setHexWriting(true)); 201 } 202 203 210 public void setUnicodeFileName(String filename, boolean unicode) { 211 put(PdfName.UF, new PdfString(filename, unicode ? PdfObject.TEXT_UNICODE : PdfObject.TEXT_PDFDOCENCODING)); 212 } 213 214 220 public void setVolatile(boolean volatile_file) { 221 put(PdfName.V, new PdfBoolean(volatile_file)); 222 } 223 224 229 public void addDescription(String description, boolean unicode) { 230 put(PdfName.DESC, new PdfString(description, unicode ? PdfObject.TEXT_UNICODE : PdfObject.TEXT_PDFDOCENCODING)); 231 } 232 233 236 public void addCollectionItem(PdfCollectionItem ci) { 237 put(PdfName.CI, ci); 238 } 239 } | Popular Tags |