I am creating an xlsx file using Apache POI in which a logo is inserted at the top left side. The issue is that, when ever the size of the cell is increased, the image also expands. How do I freeze or protect the image's size and position so that when the cell size increases, the image remains the same (doesn't expand).
Following is the code to insert the image.
rowNo = 1;
row = sheet.createRow(rowNo);
//For Adding sample Logo
addImage(1, 1, 3, 4, appPath + "/images/sample_logo.png");
public boolean addImage(int col1, int row1, int col2, int row2, String FileName) {
try {
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(FileName);
} catch (FileNotFoundException e) {
return false;
}
ByteArrayOutputStream imgBytes = new ByteArrayOutputStream();
int b;
try {
while ((b = fileInputStream.read()) != -1) {
imgBytes.write(b);
}
fileInputStream.close();
} catch (IOException e) {
return false;
}
sheet.addMergedRegion(new CellRangeAddress(row1, row2 - 1, col1, col2 - 1));
// sheet.addMergedRegion(new CellRangeAddress(1, 3, 1, 2));
XSSFClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, col1, row1, col2, row2);
int index = workBook.addPicture(imgBytes.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG); //index = 4
XSSFDrawing patriarch = sheet.createDrawingPatriarch();
patriarch.createPicture(anchor, index);
anchor.setAnchorType(2);
} catch (Exception ex) {
LOGGER.error(ex);
}
return true;
}
Kindly help me out.