简体   繁体   English

有什么方法可以在c#中获取pdf文件首页的图像吗?

[英]Is there any way to get the image of the first page of pdf file in c#?

我正在寻找一种使用c#获取pdf文件中第一页图像的方法任何解决方案??

iTextSharp should handle that. iTextSharp应该可以解决这个问题。 Exit on the first image 在第一个图像上退出

example here http://www.vbforums.com/showthread.php?t=530736 此处的示例http://www.vbforums.com/showthread.php?t=530736

Edit: 编辑:

Copied code from the thread by stanav stanav从线程复制的代码

Public Shared Function ExtractImages(ByVal sourcePdf As String) As List(Of Image)
    Dim imgList As New List(Of Image)

    Dim raf As iTextSharp.text.pdf.RandomAccessFileOrArray = Nothing
    Dim reader As iTextSharp.text.pdf.PdfReader = Nothing
    Dim pdfObj As iTextSharp.text.pdf.PdfObject = Nothing
    Dim pdfStrem As iTextSharp.text.pdf.PdfStream = Nothing

    Try
        raf = New iTextSharp.text.pdf.RandomAccessFileOrArray(sourcePdf)
        reader = New iTextSharp.text.pdf.PdfReader(raf, Nothing)

        For i As Integer = 0 To reader.XrefSize - 1
            pdfObj = reader.GetPdfObject(i)
            If Not IsNothing(pdfObj) AndAlso pdfObj.IsStream() Then
                pdfStrem = DirectCast(pdfObj, iTextSharp.text.pdf.PdfStream)
                Dim subtype As iTextSharp.text.pdf.PdfObject = pdfStrem.Get(iTextSharp.text.pdf.PdfName.SUBTYPE)
                If Not IsNothing(subtype) AndAlso subtype.ToString = iTextSharp.text.pdf.PdfName.IMAGE.ToString Then
                    Dim bytes() As Byte = iTextSharp.text.pdf.PdfReader.GetStreamBytesRaw(CType(pdfStrem, iTextSharp.text.pdf.PRStream))
                    If Not IsNothing(bytes) Then
                        Try
                            Using memStream As New System.IO.MemoryStream(bytes)
                                memStream.Position = 0
                                Dim img As Image = Image.FromStream(memStream)
                                imgList.Add(img)
                            End Using
                        Catch ex As Exception
                            'Most likely the image is in an unsupported format
                            'Do nothing
                            'You can add your own code to handle this exception if you want to
                        End Try
                    End If
                End If
            End If
        Next
        reader.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
    Return imgList
End Function 

You are probably trying to rasterize the pages of the PDF. 您可能正在尝试光栅化PDF页面。 If look for get image etc you will turn up other operations that you could perform on a PDF. 如果寻找图像等,您将打开其他可以在PDF上执行的操作。 There are a list of ways already posted. 有已经发布的方法列表 I've used ABCpdf to do this very easily. 我使用ABCpdf可以很容易地做到这一点。

Are you in a web, or native environment? 您是在Web还是本机环境中? It makes a huge difference. 它制造了巨大的差异。 What you want to is rasterize the PDF into an image. 您想要将PDF光栅化为图像。 This is easy enough to do in a native environment via GhostDoc or a similar tool. 这很容易通过GhostDoc或类似工具在本机环境中完成。 They all use a virtual printer driver to rasterize the PDF. 他们都使用虚拟打印机驱动程序来光栅化PDF。 This approach won't work in a web-environment where you will probably need to use something commercial as writing your own rasterizing engine is a massive undertaking. 这种方法在网络环境中行不通,因为您可能需要使用商业用途,因为编写自己的光栅化引擎是一项艰巨的任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在创建pdf新页面时使用itextsharp c#获取第一页数据 - while creating pdf new page get first page data using itextsharp c# C# Windows 表格显示 PDF 文件使用 Ppdfview4net (仅显示第一页) - C# Windows Form displaying PDF file use Ppdfview4net (Display the first page only) 如何在C#中保存除PDF文件的第一个页面之外的每一页 - How to save every page except the first one of a PDF file in C# 在PDF文件中打开特定页面C# - Open a specific page in a PDF file c# 在 C# 中将 PDF 的每一页都转换为图像(包含在 pdf 中) - Make every page of a PDF into an image (contained in a pdf) in C# 从Windows Phone 8.1的首页pdf文件获取图像封面 - Get image cover from first page pdf file on windows phone 8.1 Itext7 C# 如何获取页码以将图像添加到 PDF? - Itext7 C# How do I get page number to add image to PDF? 有什么方法可以使用C#读取PDF中表格的特定列 - Is there any way to read specific columns of a table in a PDF using C# 无法使用Imagemagick将PDF转换为C#中的任何图像格式 - Unable to convert PDF to any Image format in C# With Imagemagick 如何在ASP.NET C#中基于PDF或PPT / PPTX或DOC / DOCX的首页生成预览图像 - how do I generate preview image based on first page of a PDF or PPT/PPTX or DOC/DOCX in ASP.NET C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM