问题 使用itextsharp从PDF中提取图像


我试图使用itextsharp从pdf中提取所有图像,但似乎无法克服这一个障碍。

该错误发生在该行上 System.Drawing.Image ImgPDF = System.Drawing.Image.FromStream(MS); 给出“参数无效”错误。

我认为它适用于图像是位图而不是任何其他格式的图像。

我有以下代码 - 抱歉长度;

    private void Form1_Load(object sender, EventArgs e)
    {
        FileStream fs = File.OpenRead(@"reader.pdf");
        byte[] data = new byte[fs.Length];
        fs.Read(data, 0, (int)fs.Length);

        List<System.Drawing.Image> ImgList = new List<System.Drawing.Image>();

        iTextSharp.text.pdf.RandomAccessFileOrArray RAFObj = null;
        iTextSharp.text.pdf.PdfReader PDFReaderObj = null;
        iTextSharp.text.pdf.PdfObject PDFObj = null;
        iTextSharp.text.pdf.PdfStream PDFStremObj = null;

        try
        {
            RAFObj = new iTextSharp.text.pdf.RandomAccessFileOrArray(data);
            PDFReaderObj = new iTextSharp.text.pdf.PdfReader(RAFObj, null);

            for (int i = 0; i <= PDFReaderObj.XrefSize - 1; i++)
            {
                PDFObj = PDFReaderObj.GetPdfObject(i);

                if ((PDFObj != null) && PDFObj.IsStream())
                {
                    PDFStremObj = (iTextSharp.text.pdf.PdfStream)PDFObj;
                    iTextSharp.text.pdf.PdfObject subtype = PDFStremObj.Get(iTextSharp.text.pdf.PdfName.SUBTYPE);

                    if ((subtype != null) && subtype.ToString() == iTextSharp.text.pdf.PdfName.IMAGE.ToString())
                    {
                        byte[] bytes = iTextSharp.text.pdf.PdfReader.GetStreamBytesRaw((iTextSharp.text.pdf.PRStream)PDFStremObj);

                        if ((bytes != null))
                        {
                            try
                            {
                                System.IO.MemoryStream MS = new System.IO.MemoryStream(bytes);

                                MS.Position = 0;
                                System.Drawing.Image ImgPDF = System.Drawing.Image.FromStream(MS);

                                ImgList.Add(ImgPDF);

                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                }
            }
            PDFReaderObj.Close();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }



    } //Form1_Load

11470
2018-05-10 04:42


起源

@dia,谢谢你的编辑 - griegs
不用客气!!! - Dulini Atapattu
也许图像是不受支持的格式,除了忽略该图像并继续之外,您无能为力。 - Humble Coder
stackoverflow.com/a/7995733/1266873 - Koray


答案:


我过去使用过这个库没有任何问题。

http://www.winnovative-software.com/PdfImgExtractor.aspx

private void btnExtractImages_Click(object sender, EventArgs e)
{
    if (pdfFileTextBox.Text.Trim().Equals(String.Empty))
    {
        MessageBox.Show("Please choose a source PDF file", "Choose PDF file", MessageBoxButtons.OK);
        return;
    }

    // the source pdf file
    string pdfFileName = pdfFileTextBox.Text.Trim();

    // start page number
    int startPageNumber = int.Parse(textBoxStartPage.Text.Trim());
    // end page number
    // when it is 0 the extraction will continue up to the end of document
    int endPageNumber = 0;
    if (textBoxEndPage.Text.Trim() != String.Empty)
        endPageNumber = int.Parse(textBoxEndPage.Text.Trim());

    // create the PDF images extractor object
    PdfImagesExtractor pdfImagesExtractor = new PdfImagesExtractor();

    pdfImagesExtractor.LicenseKey = "31FAUEJHUEBQRl5AUENBXkFCXklJSUlQQA==";

    // the demo output directory
    string outputDirectory = Path.Combine(Application.StartupPath, @"DemoFiles\Output");

    Cursor = Cursors.WaitCursor;

    // set the handler to be called when an image was extracted
    pdfImagesExtractor.ImageExtractedEvent += pdfImagesExtractor_ImageExtractedEvent;

    try
    {
        // start images counting
        imageIndex = 0;

        // call the images extractor to raise the ImageExtractedEvent event when an images is extracted from a PDF page
        // the pdfImagesExtractor_ImageExtractedEvent handler below will be executed for each extracted image
        pdfImagesExtractor.ExtractImagesInEvent(pdfFileName, startPageNumber, endPageNumber);

        // Alternatively you can use the ExtractImages() and ExtractImagesToFile() methods
        // to extracted the images from a PDF document in memory or to image files in a directory

        // uncomment the line below to extract the images to an array of ExtractedImage objects
        //ExtractedImage[] pdfPageImages = pdfImagesExtractor.ExtractImages(pdfFileName, startPageNumber, endPageNumber);

        // uncomment the lines below to extract the images to image files in a directory
        //string outputDirectory = System.IO.Path.Combine(Application.StartupPath, @"DemoFiles\Output");
        //pdfImagesExtractor.ExtractImagesToFile(pdfFileName, startPageNumber, endPageNumber, outputDirectory, "pdfimage");
    }
    catch (Exception ex)
    {
        // The extraction failed
        MessageBox.Show(String.Format("An error occurred. {0}", ex.Message), "Error");
        return;
    }
    finally
    {
        // uninstall the event handler
        pdfImagesExtractor.ImageExtractedEvent -= pdfImagesExtractor_ImageExtractedEvent;

        Cursor = Cursors.Arrow;
    }

    try
    {
        System.Diagnostics.Process.Start(outputDirectory);
    }
    catch (Exception ex)
    {
        MessageBox.Show(string.Format("Cannot open output folder. {0}", ex.Message));
        return;
    }
}

/// <summary>
/// The ImageExtractedEvent event handler called after an image was extracted from a PDF page.
/// The event is raised when the ExtractImagesInEvent() method is used
/// </summary>
/// <param name="args">The handler argument containing the extracted image and the PDF page number</param>
void pdfImagesExtractor_ImageExtractedEvent(ImageExtractedEventArgs args)
{
    // get the image object and page number from even handler argument
    Image pdfPageImageObj = args.ExtractedImage.ImageObject;
    int pageNumber = args.ExtractedImage.PageNumber;

    // save the extracted image to a PNG file
    string outputPageImage = Path.Combine(Application.StartupPath, @"DemoFiles\Output", 
        "pdfimage_" + pageNumber.ToString() + "_" + imageIndex++ + ".png");
    pdfPageImageObj.Save(outputPageImage, ImageFormat.Png);

    args.ExtractedImage.Dispose();
}

-2
2018-05-12 04:25



实际上,这是我们正在使用的那个。对大多数事情都很有效。 - griegs
这是非常愚蠢的,需要大量的时间:( - Sawan
@MohamedSakherSawan好的,这很好。 - fuzz


解决...

即使我得到了“参数无效”的相同例外,并且经过了大量工作 在der_chirurg提供的链接的帮助下工作 (http://kuujinbo.info/iTextSharp/CCITTFaxDecodeExtract.aspx)我解决了 以下是代码:

using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using iTextSharp.text.pdf.parser;
using Dotnet = System.Drawing.Image;
using iTextSharp.text.pdf;

namespace PDF_Parsing
{
    partial class PDF_ImgExtraction
    {
        string imgPath;
        private void ExtractImage(string pdfFile)
        {
            PdfReader pdfReader = new PdfReader(files[fileIndex]);
            for (int pageNumber = 1; pageNumber <= pdfReader.NumberOfPages; pageNumber++)
            {
                PdfReader pdf = new PdfReader(pdfFile);
                PdfDictionary pg = pdf.GetPageN(pageNumber);
                PdfDictionary res = (PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES));
                PdfDictionary xobj = (PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));
                foreach (PdfName name in xobj.Keys)
                {
                    PdfObject obj = xobj.Get(name);
                    if (obj.IsIndirect())
                    {
                        PdfDictionary tg = (PdfDictionary)PdfReader.GetPdfObject(obj);
                        string width = tg.Get(PdfName.WIDTH).ToString();
                        string height = tg.Get(PdfName.HEIGHT).ToString();
                        ImageRenderInfo imgRI = ImageRenderInfo.CreateForXObject(new Matrix(float.Parse(width), float.Parse(height)), (PRIndirectReference)obj, tg);
                        RenderImage(imgRI);
                    }
                }
            }
        }
        private void RenderImage(ImageRenderInfo renderInfo)
        {
            PdfImageObject image = renderInfo.GetImage();
            using (Dotnet dotnetImg = image.GetDrawingImage())
            {
                if (dotnetImg != null)
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        dotnetImg.Save(ms, ImageFormat.Tiff);
                        Bitmap d = new Bitmap(dotnetImg);
                        d.Save(imgPath);
                    }
                }
            }
        }
    }
}

5
2017-08-14 05:50



嗨,谢谢,你的解决方案帮助我,但在一个pdf xobj.Keys返回每页的所有页面的所有图像。你知道为什么吗? - Dragouf


您需要检查流的/过滤器以查看给定图像使用的图像格式。它可能是标准的图像格式:

  • DCTDecode(jpeg)
  • JPXDecode(jpeg 2000)
  • JBIG2Decode(jbig是仅限B&W的格式)
  • CCITTFaxDecode(传真格式,PDF支持第3组和第4组)

除此之外,您需要获取原始字节(就像您一样),并使用图像流的宽度,高度,每个组件的位数,颜色组件的数量(可能是CMYK,索引,RGB或某事物)构建图像奇怪的)和其他一些,如第8.9节所定义 ISO PDF规范 (免费提供)。

所以在某些情况下你的代码会起作用,但在其他情况下,它会因你提到的异常而失败。

PS:当你有异常时,请每次都包含堆栈跟踪。相当喜欢加糖吗?


4
2018-05-10 17:17



+1 @Mark Storer,抱歉StackTrace。下次会这样做。当一个简单的500美元的图书馆能够完成这项工作时,听起来很像是为了得到图像吗?我不是一个图形人,所以我想我会让公司买东西。谢谢你的回答。 - griegs


在较新版本的iTextSharp中,第1个参数 ImageRenderInfo.CreateForXObject 不是 Matrix 不过了 GraphicsState。 @ der_chirurg的方法应该有效。我使用以下链接中的信息测试自己并且它工作得非常好:

http://www.thevalvepage.com/swmonkey/2014/11/26/extract-images-from-pdf-files-using-itextsharp/


2
2017-08-02 22:55



您好,欢迎来到SO并感谢您的回答。由于链接可能会随着时间的推移而发生变化,您是否可以编辑答案以在此处包含相关信息?您仍然可以提供上下文链接。谢谢! - Tim Malone


要提取所有页面上的所有图像,不必实现不同的过滤器。 iTextSharp有一个图像渲染器,可以将所有图像保存为原始图像类型。

只需在此处找到以下内容: http://kuujinbo.info/iTextSharp/CCITTFaxDecodeExtract.aspx 你不需要实现HttpHandler ......


1
2018-04-11 09:48



我真的希望你已经包含了代码 - 这个链接已经死了。 - Brian MacKay
呃,不。它已经启动并运行了 不变 自2011-11-27。必须在糟糕的一天击中共享服务器。 - kuujinbo


我在github上添加了库,用PDF提取图像并压缩它们。

当您打算使用功能非常强大的库ITextSharp时,可能会很有用。

这里的链接: https://github.com/rock-walker/PdfCompression


1
2017-10-31 11:20



虽然此链接可能会回答这个问题,但最好在此处包含答案的基本部分并提供参考链接。如果链接页面发生更改,则仅链接答案可能会变为无效。 - 来自评论 - Jolta
我详细阐述了PDF压缩(主要是提取压缩图像的图像)2个月的任务。而且整个SO和其他资源都没有统一的例子,如何处理事情,如itextsharp,提取图像,压缩PDF。我在SO中汇集了很多小块数据,以获得最终的工作库。我真的相信,我的端到端的例子可以被认为是那些打算做同样事情的人的起点。 - rock_walker
我认为您的图书馆没有任何问题!只是Stack Overflow的政策明确地不鼓励只是链接到外部网站的答案。 SO的目的是人们应该学习;不仅他们会找到解决他们当前问题的方法。 - Jolta
啊,我不想与SO政策争论。根据我的经验,我从外部链接中学到了很多东西,只有少数几个被破坏了。有时候深入解决这个问题是件好事。好的,这是我的观点。就在我身边:) - rock_walker


这对我有用,我认为这是一个简单的解决方案:

编写自定义RenderListener并实现其RenderImage方法,如下所示

    public void RenderImage(ImageRenderInfo info)
    {
        PdfImageObject image = info.GetImage();
        Parser.Matrix matrix = info.GetImageCTM();
        var fileType = image.GetFileType();
        ImageFormat format;
        switch (fileType)
        {//you may add more types here
            case "jpg":
            case "jpeg":
                format = ImageFormat.Jpeg;
                break;
            case "pnt":
                format = ImageFormat.Png;
                break;
            case "bmp":
                format = ImageFormat.Bmp;
                break;
            case "tiff":
                format = ImageFormat.Tiff;
                break;
            case "gif":
                format = ImageFormat.Gif;
                break;
            default:
                format = ImageFormat.Jpeg;
                break;
        }

        var pic = image.GetDrawingImage();
        var x = matrix[Parser.Matrix.I31];
        var y = matrix[Parser.Matrix.I32];
        var width = matrix[Parser.Matrix.I11];
        var height = matrix[Parser.Matrix.I22];
        if (x < <some value> && y < <some value>)
        {
            return;//ignore these images
        }

        pic.Save(<path and name>, format);
}

0
2017-07-06 20:57