Search code examples
vb.netajaxcontroltoolkitpdfsharp

PDFSharp Export JPG - ASP.NET


I am using an ajaxfileupload control to upload a pdf file to the server. On the server side, I'd like to convert the pdf to jpg. Using the PDFsharp Sample: Export Images as a guide, I've got the following:

Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports PdfSharp.Pdf
Imports System.IO
Imports PdfSharp.Pdf.IO
Imports PdfSharp.Pdf.Advanced

Namespace Tools

Public Module ConvertImage

Public Sub pdf2JPG(pdfFile As String, jpgFile As String)

        pdfFile = System.Web.HttpContext.Current.Request.PhysicalApplicationPath & "upload\" & pdfFile

        Dim document As PdfSharp.Pdf.PdfDocument = PdfReader.Open(pdfFile)

        Dim imageCount As Integer = 0
        ' Iterate pages
        For Each page As PdfPage In document.Pages
            ' Get resources dictionary
            Dim resources As PdfDictionary = page.Elements.GetDictionary("/Resources")
            If resources IsNot Nothing Then
                ' Get external objects dictionary
                Dim xObjects As PdfDictionary = resources.Elements.GetDictionary("/XObject")
                If xObjects IsNot Nothing Then
                    Dim items As ICollection(Of PdfItem) = xObjects.Elements.Values
                    ' Iterate references to external objects
                    For Each item As PdfItem In items
                        Dim reference As PdfReference = TryCast(item, PdfReference)
                        If reference IsNot Nothing Then
                            Dim xObject As PdfDictionary = TryCast(reference.Value, PdfDictionary)
                            ' Is external object an image?
                            If xObject IsNot Nothing AndAlso xObject.Elements.GetString("/Subtype") = "/Image" Then
                                ExportImage(xObject, imageCount)
                            End If
                        End If
                    Next
                End If
            End If
        Next
    End Sub

    Private Sub ExportImage(image As PdfDictionary, ByRef count As Integer)
        Dim filter As String = image.Elements.GetName("/Filter")
        Select Case filter
            Case "/DCTDecode"
                ExportJpegImage(image, count)
                Exit Select

            Case "/FlateDecode"
                ExportAsPngImage(image, count)
                Exit Select
        End Select
    End Sub

    Private Sub ExportJpegImage(image As PdfDictionary, ByRef count As Integer)
        ' Fortunately JPEG has native support in PDF and exporting an image is just writing the stream to a file.
        Dim stream As Byte() = image.Stream.Value
        Dim fs As New FileStream([String].Format("Image{0}.jpeg", System.Math.Max(System.Threading.Interlocked.Increment(count), count - 1)), FileMode.Create, FileAccess.Write)
        Dim bw As New BinaryWriter(fs)
        bw.Write(stream)
        bw.Close()
    End Sub

    Private Sub ExportAsPngImage(image As PdfDictionary, ByRef count As Integer)
        Dim width As Integer = image.Elements.GetInteger(PdfImage.Keys.Width)
        Dim height As Integer = image.Elements.GetInteger(PdfImage.Keys.Height)
        Dim bitsPerComponent As Integer = image.Elements.GetInteger(PdfImage.Keys.BitsPerComponent)

        ' TODO: You can put the code here that converts vom PDF internal image format to a Windows bitmap
        ' and use GDI+ to save it in PNG format.
        ' It is the work of a day or two for the most important formats. Take a look at the file
        ' PdfSharp.Pdf.Advanced/PdfImage.cs to see how we create the PDF image formats.
        ' We don't need that feature at the moment and therefore will not implement it.
        ' If you write the code for exporting images I would be pleased to publish it in a future release
        ' of PDFsharp.
    End Sub

End Module

End Namespace

As I debug, it blows up on Dim filter As String = image.Elements.GetName("/Filter") in ExportImage. The message is:

Unhandled exception at line 336, column 21 in ~:46138/ScriptResource.axd?d=LGq0ri4wlMGBKd-1vxLjtxNH_pd26HaruaEG_1eWx-epwPmhNKVpO8IpfHoIHzVj2Arxn5804quRprX3HtHb0OmkZFRocFIG-7a-SJYT_EwYUd--x9AHktpraSBgoZk4VJ1RMtFNwl1mULDLid5o5U9iBcuDi4EQpbpswgBn_oI1&t=ffffffffda74082d 0x800a139e - JavaScript runtime error: error raising upload complete event and start new upload

Any thoughts on what the issue might be? It seems an issue with the ajaxfileupload control, but I don't understand why it would be barking here. It's neither here nor there, but I know I'm not using jpgFile yet.


Solution

  • PDFsharp cannot create JPEG Images from PDF pages:
    http://pdfsharp.net/wiki/PDFsharpFAQ.ashx#Can_PDFsharp_show_PDF_files_Print_PDF_files_Create_images_from_PDF_files_3

    The sample you refer to can extract JPEG Images that are included in PDF files. That's all. The sample does not cover all possible cases.

    Long story short: the code you are showing seems unrelated to the error message. And it seems unrelated to your intended goal.