Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Images not displayed when using Interop.Excel to convert excel to PDF

Tags:

excel

pdf

interop

I'm using Interop.Excel to convert excel (xlsx)(2010) to PDF for an application. On my development machine it works fine and the images are displayed correctly. However on the server when the excel is converted to PDF the images (some inserted via code and others in the template document) are not displayed in the PDF. The excel file is fine when viewed. Here is my code I use to convert to PDF:

Public Shared Function FromExcel(ByVal ExcelFileLocation As String, ByVal PDFFileLocation As String) As Boolean

    ' Load the new Excel file
    ' http://msdn.microsoft.com/en-us/library/bb407651(v=office.12).aspx
    Dim excelApplication As ApplicationClass = New ApplicationClass()
    Dim excelWorkbook As Workbook = Nothing

    Dim paramExportFormat As XlFixedFormatType = XlFixedFormatType.xlTypePDF
    Dim paramExportQuality As XlFixedFormatQuality = XlFixedFormatQuality.xlQualityStandard
    Dim paramOpenAfterPublish As Boolean = False
    Dim paramIncludeDocProps As Boolean = True
    Dim paramIgnorePrintAreas As Boolean = True
    Dim paramFromPage As Object = Type.Missing
    Dim paramToPage As Object = Type.Missing

    Try
        ' Open the source workbook.
        excelWorkbook = excelApplication.Workbooks.Open(ExcelFileLocation)

        ' Save it in the target format.
        If Not excelWorkbook Is Nothing Then
            excelWorkbook.ExportAsFixedFormat(paramExportFormat, _
                PDFFileLocation, paramExportQuality, _
                paramIncludeDocProps, paramIgnorePrintAreas, _
                paramFromPage, paramToPage, paramOpenAfterPublish)

            Return True

        Else
            Return False
        End If

    Catch ex As Exception
        Return False
    Finally
        ' Close the workbook object.
        If Not excelWorkbook Is Nothing Then
            excelWorkbook.Close(False)
            excelWorkbook = Nothing
        End If

        ' Quit Excel and release the ApplicationClass object.
        If Not excelApplication Is Nothing Then
            excelApplication.Quit()
            excelApplication = Nothing
        End If

        GC.Collect()
        GC.WaitForPendingFinalizers()
        GC.Collect()
        GC.WaitForPendingFinalizers()

    End Try

End Function

here is a screen shot of what the PDF looks like when it's converted. http://it.hlbsolutions.com/uploads/Untitled-1.jpg

Notice how the images at the top and bottom right are not being displayed. Any thoughts would be helpful.

Thanks

like image 629
Lee Harris Avatar asked Nov 13 '22 20:11

Lee Harris


1 Answers

I had a similar issue when populating cells and changin radio-buttons -- all the images would disappear.

I was able to get the images to not-delete by switching the service-account to Local System instead of "Local Service" as I had originally selected.

More details at this SO question.

like image 191
Michael Paulukonis Avatar answered Jan 07 '23 06:01

Michael Paulukonis