We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Issue in adding document attachment

Hi Support,
I am unable to add the attached file as document attachment using Aspose.PDF version 20.7.

My add attachment code snippet is following:

using (Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(new 
MemoryStream(_documentBytes, true)))
using (MemoryStream msDoc = new MemoryStream(fileAttachement.Bytes))
using (Aspose.Pdf.FileSpecification fileSpecification = new 
Aspose.Pdf.FileSpecification(msDoc, fileAttachement.Name))
   {
       pdfDocument.EmbeddedFiles.Add(fileSpecification);
       using (MemoryStream msOutput = new MemoryStream())
       {
            pdfDocument.Save(msOutput);
            fileAttachement.DocBytes = msOutput.ToArray();
            _documentBytes = fileAttachement.DocBytes;
        }
   }

File to add as attachment.zip (4.0 KB)

Regards

@Wahaj_Khan

Would you kindly share complete code snippet and error details that you are facing while adding the attachment. There are some variable in your code snippet which definition is missing. Furthermore, please also share a sample PDF document that you are using. We tested using following code snippet and were unable to notice any issue:

Document pdfDocument = new Document();
pdfDocument.Pages.Add();
FileSpecification fileSpecification = new FileSpecification(dataDir + "Koropi Press.joboptions", "Sample file");
pdfDocument.EmbeddedFiles.Add(fileSpecification);
dataDir = dataDir + "AddAttachment_out.pdf";
pdfDocument.Save(dataDir);

AddAttachment_out.pdf (5.8 KB)

Hi @asad.ali
I have tried with the code provided in last comment but I am unable to add any attachment in the attached document.
Attachment document issue.zip (2.2 MB)

The code snippet shared earlier is complete whereas _documentBytes are the document bytes in which attachment is being added and fileAttachment.Bytes are the bytes of attachment being added in the document.

pdfDocument.EmbeddedFiles.Add(fileSpecification);
Above line throws following error:
Exception on adding attachment.png (50.2 KB)

Could you look into the document and check the issue.
Regards

@Wahaj_Khan

We were able to replicate the issue in our environment with your shared PDF file. We have logged it as PDFNET-48546 in our issue tracking system and will inform you as soon as it is resolved. Please be patient and spare us some time.

We are sorry for the inconvenience.