locked
Reg:Converting xls file to pdf RRS feed

  • Question

  • Hi,

    I am trying to convert a xls file to pdf format on the sharepoint site but facing a problem with it.What happens is that  the xls file gets uploaded but the response does not go back to the code for converting it to the pdf format.Please help with a solution.


    Thanks.

     Microsoft.Office.Interop.Excel.ApplicationClass excelApplication = new Microsoft.Office.Interop.Excel.ApplicationClass();
                Workbook excelWorkBook = null;

                string paramSourceBookPath = "";
                object paramMissing = Type.Missing;

                string paramExportFilePath = "";
                paramSourceBookPath = properties.WebUrl + "/" + properties.ListItem.Url;
             

                if (properties.ListItem.Name.Contains(".xlsx"))
                {
                    paramExportFilePath = paramSourceBookPath.Replace(".xlsx", ".pdf");
                }
                           

    XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypePDF;
                XlFixedFormatQuality paramExportQuality =
                XlFixedFormatQuality.xlQualityStandard;
                bool paramOpenAfterPublish = false;
                bool paramIncludeDocProps = true;
                bool paramIgnorePrintAreas = true;
                object paramFromPage = Type.Missing;
                object paramToPage = Type.Missing;

                try
                {

     

                    excelWorkBook = excelApplication.Workbooks.Open(paramSourceBookPath,
                paramMissing, paramMissing, paramMissing, paramMissing,
                paramMissing, paramMissing, paramMissing, paramMissing,
                paramMissing, paramMissing, paramMissing, paramMissing,
                paramMissing, paramMissing);

     

                    if (excelWorkBook != null)
                    {
                        excelWorkBook.ExportAsFixedFormat(paramExportFormat,
                            paramExportFilePath, paramExportQuality,
                            paramIncludeDocProps, paramIgnorePrintAreas, paramFromPage,
                            paramToPage, paramOpenAfterPublish,
                            paramMissing);

                    }

                }

                catch (Exception e)
                {
                    Console.WriteLine("Exception" + e.ToString());
                }
                finally
                {
                    // Close the workbook object.
                    if (excelWorkBook != null)
                    {
                        excelWorkBook.Close(false, paramMissing, paramMissing);
                        excelWorkBook = null;
                    }

                    // Quit Excel and release the ApplicationClass object.
                    if (excelApplication != null)
                    {
                        excelApplication.Quit();
                        excelApplication = null;
                    }

                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }


    • Edited by Rav19 Tuesday, October 11, 2011 12:23 PM
    Tuesday, October 11, 2011 12:18 PM

Answers

  • Although I can't help with the code, is there a reason why you're doing this in SharePoint?  Recent versions of Office will have an XPS printer, or a PDF converter inbuilt.


    Steven Andrews | SharePoint Professional | http://www.twitter.com/backpackerd00d | https://baron72.wordpress.com/
    Tuesday, October 11, 2011 1:19 PM
    Answerer

All replies

  • Although I can't help with the code, is there a reason why you're doing this in SharePoint?  Recent versions of Office will have an XPS printer, or a PDF converter inbuilt.


    Steven Andrews | SharePoint Professional | http://www.twitter.com/backpackerd00d | https://baron72.wordpress.com/
    Tuesday, October 11, 2011 1:19 PM
    Answerer
  • Not sure if your code runs on the client or the server, but you may want to consider a robust third party product for PDF Conversions in combination with SharePoint as you are letting yourself in for a world of hurt.

    Have a look at the PDF Converter for SharePoint. It can be used by end users, from workflows, or even from your own applications using the Web Services interface. 

    I have worked on this product so the usual disclaimers apply.

    Thursday, October 20, 2011 9:48 AM