Freeze/lock an image on xlsx sheet using apache.poi

381 Views Asked by At

I'm creating an xlsx using Apache.POI and inserting a logo at the top,how do I freeze or lock the images so that it cant be changed once the xlsx is downloaded.

      int numberOfSheets = wb.getNumberOfSheets();
            for(int i=0;i<numberOfSheets;i++)
            {
                XSSFSheet sheet = wb.getSheetAt(i);

                try {
                    //insert a logo
                    String location = "C:/Git/cc/src/main/resources/logo/VCC_logo.jpg"; // hard coded for testing
                    InputStream is = new FileInputStream(location);
                    byte[] bytes = IOUtils.toByteArray(is);
                    int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
                    is.close();
                    // Create Winner sheet

                    CreationHelper creationHelper = wb.getCreationHelper();
                    Drawing drawing = sheet.createDrawingPatriarch();


                    ClientAnchor anchor = 
                    creationHelper.createClientAnchor();
                    anchor.setCol1(0);
                    anchor.setRow1(0);
                    Picture pict = drawing.createPicture(anchor, pictureIdx);
                    pict.resize(2.5,2.5);
                    pict.getImageDimension().setSize(3.0,3.0);
                    sheet.createFreezePane(0,3);
                } catch (FileNotFoundException e) {
                    throw  new nException("Problem in reading VCC_logo.jpg");
                } catch (IOException io) {
                    throw  new Exception("Problem in reading VCC_logo.jpg");
                }
1

There are 1 best solutions below

3
SomeDude On

If your logo starts at row i and column j. You could use the API createFreezePane like :

Sheet.createFreezePane( i, j );