Using below code I am able to create a pdf doc with an image but I would like to place the image on top of an background colour, I tried bit on my side but not able to achieve it can any one help me in achieving this:
public class SimpleTable {
public static void main(String args[]) throws Exception {
//Loading an existing document
PDDocument doc = new PDDocument();
PDPage my_page = new PDPage();
//Retrieving the page
doc.addPage(my_page);
//Creating PDImageXObject object
PDImageXObject pdImage = PDImageXObject.createFromFile("D:\\QRCode.png",doc);
//creating the PDPageContentStream object
PDPageContentStream contents = new PDPageContentStream(doc, my_page);
PDImageXObject
//Drawing the image in the PDF document
contents.drawImage(pdImage, 70, 250);
contents.setNotStrokingColoar(Color.RED);
System.out.println("Image inserted");
//Closing the PDPageContentStream object
contents.close();
//Saving the document
doc.save("D:\\QRCode.pdf");
//Closing the document
doc.close();
}
}
Note: the background-colour occupancy will be same as image size
Put this before drawing your image and remove the fill that is in your current code:
contents.setNotStrokingColoar(Color.RED);
contents.addRect(70, 250, pdImage.getWidth(), pdImage.getHeight());
contents.fill();
Be aware that a background color will make it more difficult to scan the QR code because there'll be less contrast.