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;
}