Attach/Embed excel/word File in PDF using C# - Aspose.PDF for .NET

Hi, I want to embed the .xlsx or .docx file in pdf. I follow the below link using FileSpecification(Stream stream, string name) but resulted pdf got corrupted. Is there something has to be configure for excel or word to get attached in PDF file.

If possible plz share code snippet or step for this.

Link :

@GEPlalbihari

Would you kindly share your sample PDF document along with sample .xlsx or .docx file. We will test the scenario in our environment and address it accordingly.

Aspose.zip (5.3 KB)

Hi Asad,

Thanks for your time.

PFA of ConsoleApp, in which excel file are first reading from server than attach to pdf using FileSpecification.

Kindly Note, currently we are using 18.2.6.0 licences dll and demo are perform on latest 20.0.0.0 > version dll. Please share the sample for excel, word attachment code snippet.

@GEPlalbihari

Thanks for sharing sample console application.

Your application did not contain any sample files. However, we have tested the scenario in our environment and were unable to notice any issue. We also noticed that you were trying to add attachments in empty PDF (i.e. with no page). Please try to add at least one blank page inside PDF before adding attachment and in case you still face any issue, please let us know.

Attach Files in PDF Document using Aspose.PDF for .NET

Document pdfDocument = new Document();
pdfDocument.Pages.Add();
// Setup new file to be added as attachment
FileSpecification fileSpecification = new FileSpecification(dataDir + "test.xlsx", "Sample xlsx file");
// Add attachment to document's attachment collection
pdfDocument.EmbeddedFiles.Add(fileSpecification);
// Save new output
pdfDocument.Save("AddAttachment_out.pdf");

Hi @asad.ali,

Thanks for the support and your times.
Now I’m able to open the pdf with attachment in it.

@GEPlalbihari

It is good to know that you are able to achieve your requirements. Please keep using our API and in case you need further assistance, please feel free to create a new topic.