I am trying to convert a JPanel to Image and will be writing it to a PDF using iText. I have searched ways on how to convert JPanel to Image and found two "Working" solutions.
private BufferedImage createImage(JPanel panel) {
int w = panel.getWidth();
int h = panel.getHeight();
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
panel.print(g);
return bi;
}
public static java.awt.Image getImageFromPanel(JPanel component) {
BufferedImage image = new BufferedImage(component.getWidth(),
component.getHeight(), BufferedImage.TYPE_INT_ARGB);
component.paint(image.getGraphics());
return image;
}
As you can see, I already used "ARGB" to try to convert it to image with a transparent or white background but it doesnt work. See attached image. Is there a way to convert it to image and print it to PDF with white or transparent background?
Below is the image converted from the JPanel using codes above Image written on PDF
Bottom is the JPanel I want to convert to Image JPanel I want to convert