Attach file (based on byte[] / MemoryStream) into .pdf without roundtrip to temporary file?

117 views
Skip to first unread message

Support

unread,
Nov 23, 2012, 8:04:43 PM11/23/12
to pdfne...@googlegroups.com
Q:
 
We are using PdfNet SDK's functionality to place certain file attachments into .pdfs and currently the code uses a roundtrip via temporary file(s) because the only way I found out was to use something like this:
 

'attachmentFilePath' is a physically existing file on the file system.

using (var pdfDoc = new PDFDoc(pdfFile, pdfFile.Length)) {

  pdfDoc.InitSecurityHandler();
  pdfDoc.AddFileAttachment("iRMA Creation .xml File", FileSpec.Create(pdfDoc, attachmentFilePath, true));
  buffer = new byte[1];
  var bufferSize = 1;
  pdfDoc.Save(ref buffer, ref bufferSize, Base.PdfSaveOptions);
}

... Now is there a more elegant / in-memory way to do this? I have all the attachments there only in memory anyway and would rather not write to the filesystem but the .AddFileAttachment(..) method of the PDFDoc class requires a FileSpec, which again seems to only take strings pointing to existing files.

-----------
A:
 

Yup, you can create a FileSpec from a memory buffer as follows:

 

FileSpec CreateMyFileSpec(PDFDoc doc, string path, bye[] data) {

  Obj fs = doc.CreateIndirectDict();

  fs.PutName("Type", "Filespec");

  fs.PutString("F", path); 

  fs.PutText("UF", path);

  fs.PutDict("EF").Put("F", doc.CreateIndirectStream(data));

  return new FileSpec(fs);

}

 

Then add the file stream as before:

 

pdfDoc.AddFileAttachment("iRMA Creation .xml File", CreateMyFileSpec(pdfDoc, "foo", data) );

 

 

Reply all
Reply to author
Forward
0 new messages