3
votes

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.

1
(1.) by default, iText doesn't add any border to an image. If you see a border anyway, that border is already present in the image. Maybe you don't see it when looking at the image in another viewer, but it's there! (2) you're using iText 2.1.5. That version dates from March 2009. That's almost 7 years old. Please upgrade to avoid problems.Bruno Lowagie
but this is png file and i open image in paint but it is not showing any outer border even i try to crop and copy this image and paste in another editor but it is not showing any outer border but when i add through java in pdf then it is showing outer border.Hemant Agrawal
The image does have a border in spite of what you claim. However: the border is transparent which may explain why you were making the wrong assumption.Bruno Lowagie

1 Answers

3
votes

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:

enter image description here

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:

enter image description here

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:

enter image description here

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:

enter image description here