itext - 添加没有边框的png图像 [英] itext - add png image with no border

查看:198
本文介绍了itext - 添加没有边框的png图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将以下png图片添加到我的pdf中:





我使用以下代码来执行此操作:

  Image img = PngImage.getImage(filename); 
img.setBorder(Image.NO_BORDER);
img.setAlignment(Element.ALIGN_CENTER);
img.scaleAbsolute(width,height);
document.add(img);

该图片包含一个没有外边框的条形图。当我将图像添加到我的pdf时,它会显示外边框,但仅适用于底边,左边和顶边:





我想删除pdf中的边框,但上面的代码并没有实现。



我使用的是iText-2.1.5。

解决方案

In评论,我声称你的原始图像确实有边框。你声称它没有边框。现在您已经分享了图片,我们可以查看事实,看看谁是对的。



事实证明,我是对的。当我在GIMP中打开图像时,我清楚地看到一个透明边框:





也许你没有看到它,因为你正在看Paint中的图像,或者你认为透明和白色是相同的颜色。显然这个假设是错误的。



我创建了一个PDF,其中包含您共享的图像以及当我使用



ISO-32000-1(又称PDF规范)不支持PNG,因此想要将PNG引入PDF文件的软件需要转换PNG到另一种格式。在iText的情况下,普通PNG转换为位图,过滤器 / FlateDecode



在你的例如,你有一个带透明度的PNG。在ISO-32000-1中,透明图像始终存储为两个图像:您有不透明图像(在我的屏幕截图中, / Img1 ,对象编号为2)和图像掩码(在我的屏幕截图中, / Img0 ,对象编号为1)。



如果仔细观察图像掩模(使不透明图像透明的图像),您会看到它是一个黑白图像,显示一个非常小的边框。此图像显示在右下方的面板中,其中显示Stream(这是渲染图像流的位置)。这个非常小的边框是我们也可以在GIMP(或其他支持透明图像的图像查看器)中看到的透明边框。



如果此边框是透明的,那么为什么呢?你在PDF查看器中看到它?好吧,这个边框被视为零宽度的线。在PDF查看器中,使用可用于查看PDF的设备上显示的最小宽度显示宽度为零的行。如果你放大PDF,你会注意到线条的宽度保持不变。



摘要:你声称你的图像没有没有任何边框,并且iText添加了边框。我证明你错了:图像 透明边框,而iText正确地将这个透明边框作为蒙版引入。 PDF查看器将此边框显示为符合ISO-32000-1的零宽度线。



您可以通过删除原始图像中的透明边框来解决问题。例如:我使用GIMP 展平图像。结果是这张图片:




I want to add the following png image to my pdf:

I use following code to do it:

Image img = PngImage.getImage(filename);
img.setBorder(Image.NO_BORDER);
img.setAlignment(Element.ALIGN_CENTER);
img.scaleAbsolute(width,height);
document.add(img);

The image contains a bar graph which has no outer border. When I add the image to my pdf, it shows an outer border, but only for the bottom, left and top sides:

I want to remove border in the pdf, but the above code, does not make that happen.

I am using iText-2.1.5.

解决方案

In the comments, I claimed that your original image does have a border. You claim it doesn't have a border. Now that you've shared the image, we can check the facts to see who is right.

As it turns out, I was right. When I open the image in GIMP, I clearly see a transparent border:

Maybe you don't see it, because you are looking at the image in Paint or maybe you consider "transparent" and "white" to be the same color. Obviously that assumption is wrong.

I created a PDF containing the image you shared and when I open this PDF using iText RUPS, I see something like this:

PNG is not supported in ISO-32000-1 (aka the PDF specification), hence software that wants to introduce a PNG into a PDF file needs to convert that PNG to another format. In the case of iText, "normal" PNGs are converted to a bitmap with filter /FlateDecode.

In your case, you have a PNG with tranparency. In ISO-32000-1, transparent images are always stored as two images: you have the opaque image (in my screen shot, /Img1 with object number 2) and the image mask (in my screen shot, /Img0 with object number 1).

If you look closely at the image mask (the image that makes the opaque image transparent), you see that it's a black and white image that shows a very small border. This image is shown in the lower-right panel where it says "Stream" (this is where the image stream is rendered). This very small border is the transparent border we can also see in the GIMP (or other image viewers that support transparent images).

If this border is transparent, then why do you see it in a PDF viewer? Well, this border is treated as a line with zero width. In PDF viewers, a line with zero width is shown using the smallest width that can be shown on the device that is being used to view the PDF. If you zoom into the PDF, you'll notice that the width of the line remains constant.

Summarized: you claimed that your image didn't have any border, and that a border was added by iText. I have proven you wrong: the image does have a transparent border and iText correctly introduces this transparent border as a mask. The PDF viewer shows this border as a zero-width line in accordance with ISO-32000-1.

You can solve your problem by removing the transparent border in the original image. For example: I flattened the image using the GIMP. The result is this image:

This image no longer has a transparent border and when you introduce it into a PDF, no border is shown, and no mask is added to the PDF:

这篇关于itext - 添加没有边框的png图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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