Converting Excel docs to PDF in SharePoint 2010 using .NET event receivers and Aspose.Cells for .NET

Hi. Does anyone have a code sample that uses SharePoint 2010 event receivers to convert existing Excel documents to PDF? I would like to use the ‘ItemCheckedIn’ event, for example, to create a PDF copy of the existing Excel document in my SP2010 document library. I am looking for some clear instructions on how to achieve this basic functionality in SharePoint 2010.

So far I tried the following code with no success:

public override void ItemCheckedIn (SPItemEventProperties properties)
{
base.ItemUpdated(properties);
SPListItem item = properties.ListItem;
SPFile file = properties.ListItem.File;
Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(file.Name);
wb.Save(file.Name.Replace(".xls", “.pdf”), Aspose.Cells.SaveFormat.Pdf);
}

Thanks!!

Hi,

Thanks for your posting and using Aspose.Cells Family Products.

As far as your conversion to pdf is concerned, you are using correct code. For more help, please visit this document.

Converting to PDF Files

For your Aspsose.Cells for SharePoint related question, I have logged it in our database.

Development team will look into your issue and we will give you advice asap.

This issue has been logged as SHAREPOINT-40008

Please let me know as soon as you have a solution. This is a critical issue.





Hi,

Sure, we will let you know once we will get some update/solution relating to this issue.

We have also logged your comment in our database against the issue id: SHAREPOINT-40008

Hi,

We could not directly access the file for the issue of FileIOPermission.

And please set CellsHelper.FontDir property, otherwise we could not get the font settings.

Please try the following code:


private void ConvertFile(SPFile sourceFile, SPFolder destFolder, string destFileName, SaveFormat destFileFormat, bool overwrite)
{

try
{

Stream inputStream = sourceFile.OpenBinaryStream();

Stream outputStream = new MemoryStream();

string sourceExt = Path.GetExtension(sourceFile.Name).ToLower();


try
{

Workbook book = new Workbook(inputStream);

book.Save(outputStream, SaveFormat.Pdf);

}
catch (Exception e)
{

throw new ApplicationException("Convertion Failed. " + e.Message, e);

}

outputStream.Seek(0, SeekOrigin.Begin);

destFolder.Files.Add(destFolder.Url + “/” + destFileName, outputStream, overwrite);

}
catch (ArgumentException)
{
throw;

}
catch
{
throw;
}


}


BTW, you can try Aspose.Cells for SharePoint :
( https://docs.aspose.com/display/cellssharepoint/Home )