如何从PDFSharp PDF中提取FlateDecoded图片 [英] How to extract FlateDecoded Images from PDF with PDFSharp

查看:2546
本文介绍了如何从PDFSharp PDF中提取FlateDecoded图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何提取图像,这是FlateDecoded(例如像PNG)出来的PDF文件与PDFSharp?

how do I extract Images, which are FlateDecoded (such like PNG) out of a PDF-Document with PDFSharp?

我发现PDFSharp样本的评论

I found that comment in a Sample of PDFSharp:

// 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.
// [...]
// Take a look at the file
// PdfSharp.Pdf.Advanced/PdfImage.cs to see how we create the PDF image formats.



有没有人有这个问题的解决方案?

Does anyone have a solution for this problem?

感谢您的答复。

编辑:因为我没能在8小时内我自己回答的问题,我做的它这种方式:

Because I'm not able to answer on my own Question within 8 hours, I do it on that way:

感谢您的非常快的回复

我添加了一些代码的方法。 ExportAsPngImage,但我没有得到想要的结果。它只是提取了几个图像(PNG),他们没有合适的颜色和失真

I added some Code to the Method "ExportAsPngImage", but I didn't get the wanted results. It is just extracting a few more Images (png) and they don't have the right colors and are distorted.

下面是我的实际代码:

PdfSharp.Pdf.Filters.FlateDecode flate = new PdfSharp.Pdf.Filters.FlateDecode();
        byte[] decodedBytes = flate.Decode(bytes);

        System.Drawing.Imaging.PixelFormat pixelFormat;

        switch (bitsPerComponent)
        {
            case 1:
                pixelFormat = PixelFormat.Format1bppIndexed;
                break;
            case 8:
                pixelFormat = PixelFormat.Format8bppIndexed;
                break;
            case 24:
                pixelFormat = PixelFormat.Format24bppRgb;
                break;
            default:
                throw new Exception("Unknown pixel format " + bitsPerComponent);
        }

        Bitmap bmp = new Bitmap(width, height, pixelFormat);
        var bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, pixelFormat);
        int length = (int)Math.Ceiling(width * bitsPerComponent / 8.0);
        for (int i = 0; i < height; i++)
        {
            int offset = i * length;
            int scanOffset = i * bmpData.Stride;
            Marshal.Copy(decodedBytes, offset, new IntPtr(bmpData.Scan0.ToInt32() + scanOffset), length);
        }
        bmp.UnlockBits(bmpData);
        using (FileStream fs = new FileStream(@"C:\Export\PdfSharp\" + String.Format("Image{0}.png", count), FileMode.Create, FileAccess.Write))
        {
            bmp.Save(fs, System.Drawing.Imaging.ImageFormat.Png);
        }



时,正确的方法是什么?或者我应该选择另一种方式?非常感谢!

Is that the right way? Or should I choose another way? Thanks a lot!

推荐答案

要获得一个Windows BMP,你只需要创建一个Bitmap标题,然后将图像数据复制到该位图。 PDF图像是字节对齐(每一个新的行会开始一个字节边界上),而Windows BMP是DWORD对齐(每个新行一个DWORD边界开始(一个DWORD是4字节由于历史原因))。
你需要为位图头的所有信息可以在滤波器参数找到或可以计算的。

To get a Windows BMP, you just have to create a Bitmap header and then copy the image data into the bitmap. PDF images are byte aligned (every new line starts on a byte boundary) while Windows BMPs are DWORD aligned (every new line starts on a DWORD boundary (a DWORD is 4 bytes for historical reasons)). All information you need for the Bitmap header can be found in the filter parameters or can be calculated.

调色板中的PDF另一个FlateEncoded对象。你也复制到BMP。

The color palette is another FlateEncoded object in the PDF. You also copy that into the BMP.

这必须为多种格式来完成(每像素1位,8 BPP,24 BPP,32 BPP)。

This must be done for several formats (1 bit per pixel, 8 bpp, 24 bpp, 32 bpp).

这篇关于如何从PDFSharp PDF中提取FlateDecoded图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆