// Create a new workbook Workbook workbook = new Workbook(); IWorksheet ws = workbook.getActiveSheet(); // The base64 string of the image. String base64Img = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCABtAG8DAREAAhEBAxEB/8QAHAAAAQUBAQEAAAAAAAAAAAAAAAECBAUGAwgH/8QAOhAAAQMCAwUGAggGAwAAAAAAAQACAwQRBRIhBhMxQVEHFCJhcYEysSNCUlNikZLBFRYkJTThM3Kh/8QAGwEAAQUBAQAAAAAAAAAAAAAAAAECAwQFBgf/xAAsEQACAgEEAQMEAgEFAAAAAAAAAQIDEQQSITEFEyJBBhQyURUkUiMzQmFx/9oADAMBAAIRAxEAPwD1SgAuEieegAlGUAlwk3LAESqrYafV7tfLis/UeRop/KRLGmc+kdIKqGZt45Gn3Umn19Ny9shJVSj2jsHC/FWXbD9jMMa+VjRdzwPVNs1Nday2KoSfSIf8Upu9CEyC9r5uSzl5bTOzbuJPQnjOCc17SLhwIWpXbGxZiyJprsddSCBdD4ALpE88oASgCAEJsNUmP0BX4liUVHu2vPjkNmjzWT5LysPHrGMsmrqcmUdZX1kxcN5u28g1chqfNajUv2vCNGrTQXJXxl8gIlcS/mSsa65t+55ZYcFHoe3eRmzZLn0sleougvYxPSTXJIjkmdGS+Qg30GY6re0ulvt07tlMrOMN205GSZ3xAW63usC6+2FmyUuCaNcUco2OLiXWN9B5J3qLP/ZJ7cE2GpngsY5HDy4q1V5TUaf8JcEU6YTLWjxlrnhlQMrjwcOC6jxv1ArWoWlC3TuPRbteHNuDcdV1MZJrP7KrWBwS9CCpQAlAEWtqmU0LnuPoOqoa/Vx0le5vkkrrlN8GPxEur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XmypvBOPxr1vTtSqi0YcliTyXqmGggAQAIAEAIeSBGZbFj/AHCX2+QXm/n3/aka+lXsId1ixxnksrs4YhSx11DUUst93NG6N1uNiLJ2mulVbuQdGXp8MxaGQ088DZ4mNAFQyQDPbqw8Cu10/mq9uJdk0bWidgmH1jMTkqKxgiijaWRsDg4vvbxm3uFk+V8nG5YQyc22aFc3HlNjfgQj5qWnsbPoudlD9HUj8Q+S9W0T/wBGJi3fkXytEYIAEACABADSkl0BlcU/z5fUfJea+el/bkbGmftIqxiwCXpCAkUfnIYE5oxnsXgcEc5wgY08E6t4Y2fRb7KaCpHO4/deq+PlmiODFu/I0CukYIAEACABADXaJsnhAZXE/wDOl9V5p5tp6yRsaZe0irKb5LAJMMARhgCMMUAUdCMOKWDy0JLottl9JKoeY/dep+Na+3iY135GgV8hBAAgAQAIAa7VI1lYFM1isEsVS+R7CWPddpaFxXlPEX3aiVkI8M0Kboxjhsgl4H1T+SzZeE1OeIkn3MP2JnHQ/kk/hdX/AIi/cQ/Ym8bb/SP4XV/4h9xD9hvG8/kj+F1f+IfcQ/YmdvIH8kv8Jqn3EHqIfsaHHNox35J8fBanj2iS1UGuy52YDt/Uktc0G3EWXdaOl1VKMuzNslullGhVsYCABAAUANSYDCFQ+EGBCLjVCwxOhu6YTqxp9kbEODcx/ds/Sk2oTIbiL7tn6QjagyG5j+7Z+lG1BkNzH92z9KNqDIu6Z9hv5IUUgHBoHAAegTgFQAIAEAf/2Q=="; // Alternative text that describes the image for accessibility. String altText = "altText"; // Customize the image size by using the height and width arguments. // 0 Fit the image in the cell and maintain its aspect ratio. // 1 Fill the cell with the image and ignore its aspect ratio. // 2 Maintain the original image size, which may exceed the cell boundary. // 3 Customize the image size by using the height and width arguments. int sizing = 3; // Custom height of the image in pixels. int height = 60; // Custom width of the image in pixels. int width = 60; // The Y-axis coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context. int clipY = 0; // The X-axis coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context. int clipX = 0; // The height of the sub-rectangle of the source image to draw into the destination context. int clipH = 50; // The width of the sub-rectangle of the source image to draw into the destination context. int clipW = 50; // This value indicates the vertical alignment of the image. 0 = top, 1 = center, 2 = bottom. int vAlign = 1; // This value indicates the horizontal alignment of the image. 0 = left, 1 = center, 2 = right. int hAlign = 1; ws.getRange("A1").setValue("Clipped image"); ws.getRange("B1").setValue("Original image"); ws.getRange("A2").setFormula( String.format("=IMAGE(\"%s\",\"%s\",%d,%d,%d,%d,%d,%d,%d,%d,%d)", base64Img, altText, sizing, height, width, clipY, clipX, clipH, clipW, vAlign, hAlign)); ws.getRange("B2").setFormula( String.format("=IMAGE(\"%s\",\"%s\",%d,%d,%d)", base64Img, altText, sizing, height, width)); ws.getRange("A2:B2").setRowHeightInPixel(120); ws.getRange("A2:B2").setColumnWidthInPixel(120); ws.getPageSetup().setPrintGridlines(true); // Save to a pdf file workbook.save("ImageSparkline.pdf");