具有透明度的iTextSharp图像提取 [英] iTextSharp Image Extraction with Transparency

查看:860
本文介绍了具有透明度的iTextSharp图像提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iTextSharp并尝试从PDF中提取透明图像。当我提取图像时,透明度变为纯黑色并丢失。我找到了多个图像提取的例子,但它们似乎都有同样的问题。我正在使用的代码位于

I am using iTextSharp and trying to extract images with transparency from a PDF. When I extract the image the transparency becomes solid black and is lost. I have found multiple examples of image extraction but all of them seem to have the same issue. The code that I am using is below

另一个例子来自itextpdf.com/examples/iia.php?id=284。此示例包括顶部结果部分中的图像。如果单击 Img7.png ,您将在图像中看到黑色边框,但在页面底部有一个指向原始图像的链接 info.png 以透明的方式显示透明度。这是我遇到的确切问题。任何帮助或想法将不胜感激

Another example is from itextpdf.com/examples/iia.php?id=284. This example includes images in the "results" section at the top. If you click Img7.png you will see the black border in the image, however at the bottom of the page there is a link to the original image info.png that shows the transparency the way it is supposed to look. This is the exact issue I am running into. Any help or ideas would be appreciated

public void ExtractImage(string pdfFile)
        {
            const int pageNumber = 1; //Page number to extract the image from
            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);

                    var fileType= imgRI.GetImage().GetFileType();
                    RenderImage(imgRI, imgPath + +imgRI.GetRef().Number + "_" + imgRI.GetRef().Generation + "test." + fileType);
                }
            }
            pdf.Close();
        }

        private void RenderImage(ImageRenderInfo renderInfo, string saveImageLocation)
        {
            PdfImageObject image = renderInfo.GetImage();

            using (var dotnetImg = image.GetDrawingImage())
            {
                if (dotnetImg != null)
                {
                    dotnetImg.Save(saveImageLocation);
                }
            }
        }


推荐答案

请阅读PDF规范(ISO-32000-1)。您假设,例如透明PNG,可以作为透明PNG存储在PDF中。这个假设是错误的。

Please read the PDF specification (ISO-32000-1). You are making the assumption that, for instance a transparent PNG, can be stored inside a PDF as a transparent PNG. That assumption is wrong.

PDF不支持图像类型PNG。将透明PNG添加到PDF文档时,会将其转换为两个压缩位图。一个位图是您所指的图像:据称​​失去透明度的图像。另一个位图,一个你没有告诉我们任何事情的图像,但那里,是这个图像的一个掩码。检查Image XObject时,您会注意到它具有对此掩码的引用。我的书籍在第10.3.2节中标题为屏蔽图像中对此进行了解释。

The image type PNG isn't supported in PDF. When a transparent PNG is added to a PDF document, it is converted into two compressed bitmaps. One bitmap is the image you're referring to: the image that allegedly lost its transparency. The other bitmap, an image you didn't tell us anything about, but that is there, is a mask for this image. When you examine the Image XObject, you'll notice that it has a reference to this mask. This is explained in my book in section 10.3.2, entitled "Masking images".

您对PDF文档中存储了透明图像的指控是错误的。相反,你有两个不透明的图像,其中一个图像是另一个图像的掩模,以实现透明度。您无法将这些图像提取为单个透明图像。您需要提取两个不透明图像并将它们合并为一个透明图像。这超出了iText(夏普)的范围。

Your allegation that you have a transparent image stored in your PDF documents is wrong. Instead, you have two opaque images of which one image is the mask of the other, in order to achieve transparency. You can't extract these images as a single transparent image. You need to extract both opaque images and merge them into a single transparent image. This is outside the scope of iText(Sharp).

这篇关于具有透明度的iTextSharp图像提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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