1
votes

I'm adding borders to various images in a .pdf document. The borders all have the same width, but in the .pdf the borders have different widths. It is more pronounced as the width of the border increases.

Also, is there a way to move the border outside of the image, so that it is not covering any of the image using the methods of the image class? I realize I can first put a filled rectangle and then add the image on top of the rectangle as an option. Just curious as to if this can be done with methods from the Image class.

Here is the code snippet

    magazine.open();

    canvas = pdfw.getDirectContent();

    image = Image.getInstance("a.JPG");

    image.setBorder(Rectangle.BOX);     
    image.scaleAbsolute(200,200);       
    image.setBorderWidth(50);
    image.setAbsolutePosition(50,10);
    //canvas.addImage(image);       
    magazine.add(image);


    image = Image.getInstance("b.jpg");

    image.setBorder(Rectangle.BOX);     
    image.scaleAbsolute(200,200);       
    image.setBorderWidth(50);
    image.setAbsolutePosition(50,230);
    //canvas.addImage(image);       
    magazine.add(image);
1

1 Answers

0
votes

I fear you'll have to work with the workaround you described.

There are two ways to define a border for an image:

image.setUseVariableBorders(false);

This is the default. This is what you have (even though you aren't calling the method explicitly).

In this case, the thickness of the border is distributed in a way that half of the line width is inside the rectangle and half of the line width is outside of the rectangle. Maybe that's what's causing the effect that the difference you notice is more pronounced as the border width increases.

Then there is:

image.setUseVariableBorders(true);

Now the borders will be drawn inside the area needed for the image. This is useful for tables (both PdfPCell and Image are subclasses of the Rectangle class where these methods are defined), but I fear it doesn't help you in the case of images.

So your best chance is to add the border using a workaround.