【问题标题】:PDFSharp Export JPG - ASP.NETPDFSharp 导出 JPG - ASP.NET
【发布时间】:2014-10-16 14:54:51
【问题描述】:

我正在使用 ajaxfileupload 控件将 pdf 文件上传到服务器。在服务器端,我想将 pdf 转换为 jpg。使用PDFsharp Sample: Export Images 作为指导,我得到了以下信息:

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") 上爆炸。消息是:

在线路336未处理的异常,柱21在〜:46138 /的ScriptResource.axd d = LGq0ri4wlMGBKd-1vxLjtxNH_pd26HaruaEG_1eWx-epwPmhNKVpO8IpfHoIHzVj2Arxn5804quRprX3HtHb0OmkZFRocFIG-7A-SJYT_EwYUd - x9AHktpraSBgoZk4VJ1RMtFNwl1mULDLid5o5U9iBcuDi4EQpbpswgBn_oI1&T = ffffffffda74082d 0x800a139e - JavaScript 运行时错误:引发上传完成事件并开始新上传时出错

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

【问题讨论】:

    标签: vb.net ajaxcontroltoolkit pdfsharp


    【解决方案1】:

    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 图像。就这样。该示例并未涵盖所有可能的情况。

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

    【讨论】:

    • 该页面声明“[PDFsharp Samples] (pdfsharp.net/wiki/PDFsharpSamples.ashx) 展示了如何调用 Adob​​e Reader 或 Acrobat 来查看或打印 PDF 文件,以及如何调用 GhostScript 从 PDF 页面创建图像。”关于哪个示例展示了如何调用 GhostScript 从 PDF 页面创建图像的想法?
    • 有 Adob​​e Reader 和 GhostScript 的示例。我不记得它们是否包含在最新版本中。因此,还请下载较旧的源包(1.31、1.30、...),直到找到示例。如果您打算使用它,请注意 GhostScript 的许可条件。
    猜你喜欢
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    • 2020-02-13
    • 1970-01-01
    • 2023-04-03
    相关资源
    最近更新 更多