iText7图像透明度 [英] iText7 Image Transparency

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

问题描述

我想在现有的pdf文件中添加透明的png图像,以模糊文本的某些部分.我不明白如何应用透明度

I want to add a transparent png image to an existing pdf to blur some part of the text. I don't understand how to apply the transparency

我尝试了文档中的几个代码示例,但没有一个

I have tried several code examples found in the documentation, but none worked

// Read the pdf input
PdfReader pdfReader = new PdfReader(value);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PdfWriter pdfWriter = new PdfWriter(outputStream);

PdfDocument pdfDoc = new PdfDocument(pdfReader, pdfWriter);
Document document = new Document(pdfDoc);
// Creating an ImageData object
ImageData data = ImageDataFactory.create(fileName);
data.setTransparency(new int[] {0xF0, 0xFF });

for (int x = 1; x < 800; ) {
    for (int y = 1; y < 1000; ) {
        Image image = new Image(data);
        image.setFixedPosition(x , y);
        document.add(image);

        y = y + y1 + 40;
    }
    x = x + x1 + 40;
}

// The content has now been modified, return it as a stream
document.close();

当我使用setTransparency方法时,屏幕上没有任何显示,就好像它是完全透明的一样. 如果我注释掉setTransparency方法,则会添加模糊图像,但根本不会透明,从而使其无效.

When I use the setTransparency method, then nothing is shown on the screen as if it was fully transparent. If I comment out the setTransparency method, then the blur image is added, but not transparency at all making it ineffective.

我正在附上两个输出pdf的屏幕截图.第一个是调用setTransparency方法时.第二个是setTransparency方法被注释掉的时候.

I am attaching a screen shot of the two output pdfs. The first one is when setTransparency method is called. The second one is when the setTransparency method is commented out.

调用setTransparency时:

When setTransparency is called:

当方法setTransparency被注释掉时

When the method setTransparency is commented out

我希望能够设置Transparency并在pdf的顶部看到这些模糊的图像. 我正在使用此图像来模糊文本:

I am expecting to be able to setTransparency and see these blurred images on top on the pdf. I am using this image to blur the text:

推荐答案

您的透明图像"(

Your "transparent image" (54-544263_face-blur-png-face-blur-overlay-png-transparent.png) is not transparent at all! It might be a screenshot of how a transparent image looks in an image editor that supports transparent images and underlays it with a checkerboard pattern to visualize transparency.

在仔细研究了示例图像的来源之后:

After investigating the source of that example image a bit:

您似乎已从pngix.com下载了预览图像.这些预览图像不包含实际的透明度,而是说明该透明度的效果.要从pngix.com的当前用户界面中获得具有实际透明度的图像,您必须非常努力地寻找页面上的免费下载"按钮(对我而言,它夹在赞助搜索"之间),然后是在弹出窗口中下载PNG"按钮;这使您可以下载具有实际透明度的图像.

You appear to have downloaded a preview image from pngix.com. These preview images do not contain the actual transparency but instead illustrate the effect of that transparency. To get an image with actual transparency from pngix.com with its current UI, you have to look very hard and find the "free download" button on the page (for me it was sandwiched between "sponsored searches"), followed by the "Download PNG" button in the popup; this allows you to download an the image with actual transparency.

话虽如此,您可以使用免费的工具(例如,金普. pngix.com上的图像仅供个人使用,因此从理论上讲,如果您在没有额外许可的情况下专业使用图像,则可能必须处理法律问题.

That being said, you can easily create an image of such circular fade into transparency yourself using free tools, e.g. gimp. Images from pngix.com are for personal use only, so theoretically you may have to deal with legal issues if you use them professionally without extra license.

甚至更好的是,您实际上根本不需要位图图像,也可以使用使用矢量图形指令的本机PDF机制,即无需引入大图像即可获得结果嗨高分辨率!参见例如此答案底部的示例屏幕截图,该图像是使用testComplex创建的="https://github.com/mkl-public/testarea-itext5/blob/master/src/test/java/mkl/testarea/itext5/content/TestTransparency.java#L88" rel ="nofollow noreferrer"> TestTransparency 测试课程.

And even better, you actually don't need bitmap images for that at all, you can also use native PDF mechanisms which use vector graphics instructions, i.e. no need to introduce large images for results looking good in hi res! See e.g. the example screenshot at the bottom of this answer, that image was created using the test method testComplex from the TestTransparency test class.

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

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