Free Support Forum - aspose.com

Can we Embed Pdf documents in excel cells in the exported excel sheets?

Hi,

While exporting to excel can we embed pdf documents in the excel cells?

Please find out the attached snapshot of the needed requirement

please let me know whether this can be acheivable or not?

Regatds,

S.N.Prasad

Hi,

Please see the following sample code that explains how you can insert a pdf document as an ole object into Excel sheet:

//Instantiate a new Workbook.
Workbook workbook = new Workbook();
//Get the first worksheet.
Worksheet sheet = workbook.Worksheets[0];
//Define a string variable to store the image path.
string ImageUrl = @“e:\test\abc.jpg”;
//Get the picture into the streams.
FileStream fs = File.OpenRead(ImageUrl);
//Define a byte array.
byte[] imageData = new Byte[fs.Length];
//Obtain the picture into the array of bytes from streams.
fs.Read(imageData, 0, imageData.Length);
//Close the stream.
fs.Close();
//Get an excel file path in a variable.
string path = @“e:\test\TestFile.PDF”;
//Get the file into the streams.
fs = File.OpenRead(path);
//Define an array of bytes.
byte[] objectData = new Byte[fs.Length];
//Store the file from streams.
fs.Read(objectData, 0, objectData.Length);
//Close the stream.
fs.Close();
//Add an Ole object into the worksheet with the image
//shown in MS Excel.
sheet.OleObjects.Add(14, 3, 30, 30, mageData);
//Set embedded ole object data.
sheet.OleObjects[0].ObjectData = objectData;
sheet.OleObjects[0].DisplayAsIcon = true;
sheet.OleObjects[0].FileType = OleFileType.Pdf;
//Save the excel file
workbook.Save(@“e:\test\ouputBook.xls”);


Thank you.