/** * Creates a file specification with the file embedded. The file may * come from the file system or from a byte array. * @param writer the <CODE>PdfWriter</CODE> * @param filePath the file path * @param fileDisplay the file information that is presented to the user * @param fileStore the byte array with the file. If it is not <CODE>null</CODE> * it takes precedence over <CODE>filePath</CODE> * @param mimeType the optional mimeType * @param fileParameter the optional extra file parameters such as the creation or modification date * @param compressionLevel the level of compression * @throws IOException on error * @return the file specification * @since 2.1.3 */ public static PdfFileSpecification FileEmbedded(PdfWriter writer, String filePath, String fileDisplay, byte[] fileStore, String mimeType, PdfDictionary fileParameter, int compressionLevel) { PdfFileSpecification fs = new PdfFileSpecification(); fs.writer = writer; fs.Put(PdfName.F, new PdfString(fileDisplay)); PdfEFStream stream; Stream inp = null; PdfIndirectReference refi; PdfIndirectReference refFileLength; try { refFileLength = writer.PdfIndirectReference; if (fileStore == null) { if (File.Exists(filePath)) { inp = new FileStream(filePath, FileMode.Open, FileAccess.Read); } else { if (filePath.StartsWith("file:/") || filePath.StartsWith("http://") || filePath.StartsWith("https://")) { WebRequest w = WebRequest.Create(filePath); inp = w.GetResponse().GetResponseStream(); } else { inp = BaseFont.GetResourceStream(filePath); if (inp == null) throw new IOException(filePath + " not found as file or resource."); } } stream = new PdfEFStream(inp, writer); } else stream = new PdfEFStream(fileStore); stream.Put(PdfName.TYPE, PdfName.EMBEDDEDFILE); stream.FlateCompress(compressionLevel); stream.Put(PdfName.PARAMS, refFileLength); if (mimeType != null) stream.Put(PdfName.SUBTYPE, new PdfName(mimeType)); refi = writer.AddToBody(stream).IndirectReference; if (fileStore == null) { stream.WriteLength(); } PdfDictionary param = new PdfDictionary(); if (fileParameter != null) param.Merge(fileParameter); param.Put(PdfName.SIZE, new PdfNumber(stream.RawLength)); writer.AddToBody(param, refFileLength); } finally { if (inp != null) try{inp.Close();}catch{} } PdfDictionary f = new PdfDictionary(); f.Put(PdfName.F, refi); fs.Put(PdfName.EF, f); return fs; }
/** * Creates a file specification with the file embedded. The file may * come from the file system or from a byte array. * @param writer the <CODE>PdfWriter</CODE> * @param filePath the file path * @param fileDisplay the file information that is presented to the user * @param fileStore the byte array with the file. If it is not <CODE>null</CODE> * it takes precedence over <CODE>filePath</CODE> * @param mimeType the optional mimeType * @param fileParameter the optional extra file parameters such as the creation or modification date * @param compressionLevel the level of compression * @throws IOException on error * @return the file specification * @since 2.1.3 */ public static PdfFileSpecification FileEmbedded(PdfWriter writer, String filePath, String fileDisplay, byte[] fileStore, String mimeType, PdfDictionary fileParameter, int compressionLevel) { PdfFileSpecification fs = new PdfFileSpecification(); fs.writer = writer; fs.Put(PdfName.F, new PdfString(fileDisplay)); PdfEFStream stream; Stream inp = null; PdfIndirectReference refi; PdfIndirectReference refFileLength; try { refFileLength = writer.PdfIndirectReference; if (fileStore == null) { if (File.Exists(filePath)) { inp = new FileStream(filePath, FileMode.Open, FileAccess.Read); } else { if (filePath.StartsWith("file:/") || filePath.StartsWith("http://") || filePath.StartsWith("https://")) { WebRequest w = WebRequest.Create(filePath); inp = w.GetResponse().GetResponseStream(); } else { inp = BaseFont.GetResourceStream(filePath); if (inp == null) { throw new IOException(filePath + " not found as file or resource."); } } } stream = new PdfEFStream(inp, writer); } else { stream = new PdfEFStream(fileStore); } stream.Put(PdfName.TYPE, PdfName.EMBEDDEDFILE); stream.FlateCompress(compressionLevel); stream.Put(PdfName.PARAMS, refFileLength); if (mimeType != null) { stream.Put(PdfName.SUBTYPE, new PdfName(mimeType)); } refi = writer.AddToBody(stream).IndirectReference; if (fileStore == null) { stream.WriteLength(); } PdfDictionary param = new PdfDictionary(); if (fileParameter != null) { param.Merge(fileParameter); } param.Put(PdfName.SIZE, new PdfNumber(stream.RawLength)); writer.AddToBody(param, refFileLength); } finally { if (inp != null) { try{ inp.Close(); }catch {} } } PdfDictionary f = new PdfDictionary(); f.Put(PdfName.F, refi); fs.Put(PdfName.EF, f); return(fs); }