LibrePDF / OpenPDF

OpenPDF is a free Java library for creating and editing PDF files, with a LGPL and MPL open source license. OpenPDF is based on a fork of iText. We welcome contributions from other developers. Please feel free to submit pull-requests and bugreports to this GitHub repository.
Other
3.49k stars 581 forks source link

Support for Circular Shaped Images #1174

Open HemaSudha1498 opened 3 months ago

HemaSudha1498 commented 3 months ago

Exporting profile icons in a circular shape is a common requirement. However, I couldn't find an option to crop images into a circular shape using the Image class.

I tried several methods, including clip paths and Graphics2D objects. The challenge is that I need the cropped images inside a TableCell along with text. One approach that worked involves converting the image to BufferedImage, using a Graphics object, and then converting it back to byte[] to create a new Image instance. However, this process significantly increases the PDF generation time, taking minutes for small data sets and hours for larger ones.

Please enable an optimized way to draw images in circular shapes, or direct me to the relevant code if this functionality is already available.

Screenshot from 2024-05-31 10-45-06

Thank you

StevenStreasick commented 2 months ago

I do agree that this would be a nice feature to have. However, I was able to get a proof of concept for another method for creating circular shaped images, which is hopefully less taxing. I created a mask image, which was of a circular shape, and then I applied that overtop of my original image. This allows for a singular mask to apply to multiple images. While my proof of concept had poorly defined borders around the mask, I believe with a bit of effort, you could create a well defined border.

Image square = Image.getInstance(squareFilePath);
java.awt.Image maskImage = ImageIO.read(new File(maskFilePath));
Image mask = Image.getInstance(maskImage, null, true);
mask.makeMask();
square.setImageMask(mask);

Notice that the mask's original image should be black for when you want a pixel to display, and white for when you do not.