2014-10-16 119 views
2

我正在使用ajaxfileupload控件将pdf文件上传到服务器。在服务器端,我想将pdf转换为jpg。使用PDFsharp Sample: Export Images为指导,我有以下几点:PDFSharp导出JPG - ASP.NET

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 

正如我调试,它在ExportImage上Dim filter As String = image.Elements.GetName("/Filter")炸毁。消息是:

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

对这个问题可能有什么想法?这似乎是ajaxfileupload控件的问题,但我不明白为什么它会在这里叫。它既不在这里,也不在那里,但我知道我还没有使用jpg文件。

回答

0

PDFsharp不能从PDF页面中创建JPEG图片:
http://pdfsharp.net/wiki/PDFsharpFAQ.ashx#Can_PDFsharp_show_PDF_files_Print_PDF_files_Create_images_from_PDF_files_3

样品,你指的可以提取包括在PDF文件的JPEG图像。就这样。该示例没有涵盖所有可能的情况。

长话短说:您显示的代码似乎与错误消息无关。这似乎与你的预期目标无关。

+0

页面状态“The [PDFsharp Samples](http://pdfsharp.net/wiki/PDFsharpSamples.ashx)显示如何调用Adobe Reader或Acrobat查看或打印PDF文件以及如何调用GhostScript以创建图像PDF页面“。想知道哪些示例显示如何调用GhostScript从PDF页面创建图像? – user3799279 2014-10-16 18:20:20

+0

有Adobe Reader和GhostScript的示例。我不记得他们是否包含在最新版本中。因此,请下载较旧的源代码包(1.31,1.30,...),直到找到样本。如果您打算使用GhostScript,请注意GhostScript的许可条件。 – 2014-10-17 20:36:12