i searched into documentation about how to add attachments to existing pdf document with a related and linked annotation. I found different ways to add attachments but it seems there is only a way to add attachment with annotation: using PdfContentEditor with the CreateFileAttachment method, is it right? Or there is another way?
Such a method works fine, but it can manage only FileStream streams, and this is a great limitation. I received attachment by memory and so in order to use the CreateFileAttachment CreateFileAttachment method i have to temporary save the file to disk.
I think it would be a great feature to change the method signature in order to accept a generic stream instead of a FileStream.
There are following approaches to add files as attachments in PDF documents.
Using method public void AddDocumentAttachment(Stream,string,string); of PdfContentEditor in Aspose.Pdf.Facades Namespace.
Or you may try using Document.EmbeddedFiles.Add(fileSpecification); to add attachment. The FileSpecification object takes string as well as Stream as an argument. Please take a look over following code snippet in which I have tried adding an image file as an attachment to existing PDF document. For more information, please visit Add Attachment In a PDF Document
[C#]
//open document Document pdfDocument = new Document(“d:/pdftest/TableMarginIssue.pdf”); // load BitMap image Bitmap bmp = new Bitmap(“d:/pdftest/sample.jpg”); MemoryStream ms = new MemoryStream(); using (ms= new MemoryStream()) { // Convert Image to byte[] bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] imageBytes = ms.ToArray(); // create FileSepcification object while using MemoryStream object FileSpecification fileSpecification = new FileSpecification(ms, “Sample Attachment”); //add attachment to document’s attachment collection pdfDocument.EmbeddedFiles.Add(fileSpecification); } //save new output pdfDocument.Save(“d:/pdftest/AttachmentTest.pdf”); ms.Dispose();
However concerning to your requirement on passing MemorySteam object as an argument to CreateFileAttachment method, I have logged this requirement as PDFNEWNET-30082 in our issue tracking system. We will further look into the details of this requirement and will keep you updated on the status of correction. Please be patient and spare us little time. We apologize for your inconvenience.
I already seen the link you suggest me and every way works fine in order to add an attachment to an existing pdf both from file and memory stream.
But what indeed i need is a little more complex, because i have to add an attachment by a memory stream and linking it into pdf by an annotation with an icon like a graph or so on.
The CreateFileAttachment method do exactly what i need, the only limitation is that the method accept only a FileStream. So in order to use it i have to temporary save the pdf memory stream to file and then retriving it again from file. This is tricky and would like to avoid it.
As I have stated in my earlier post, we have added the requirement to introduce an new overloaded method for CreateFileAttachment(…) which can accept Stream object as an argument. It has been logged as PDFNEWNET-30082. We are looking into the details of this requirement and will keep you updated on the status of correction. Your patience and comprehension is greatly appreciated in this regard. We apologize for your inconvenience.