java 图片线条,在Java中使用apache poi对图片进行填充和线条格式化

I am inserting image in excel using apache poi 3.15 its working file but I want to add border line to image which can be done by right click on image --> Format Picture --> Fill & Line --> Line --> Solid Line in MS Office searched a lot on SO and apache documentation but have no clue how to achieve this using poi. Following my code

private void drawImageOnExcelSheetForGLOS(XSSFSheet sitePhotosSheet,

int row1, int row2, int col1, int col2, String fileName) {

try {

InputStream is = new FileInputStream(fileName);

byte[] bytes = IOUtils.toByteArray(is);

int pictureIdx = sitePhotosSheet.getWorkbook().addPicture(bytes,Workbook.PICTURE_TYPE_JPEG);

is.close();

CreationHelper helper = sitePhotosSheet.getWorkbook().getCreationHelper();

// Create the drawing patriarch. This is the top level container for

// all shapes.

Drawing drawing = sitePhotosSheet.createDrawingPatriarch();

// add a picture shape

ClientAnchor anchor = helper.createClientAnchor();

anchor.setAnchorType( ClientAnchor.MOVE_AND_RESIZE );

// set top-left corner of the picture,

// subsequent call of Picture#resize() will operate relative to it

anchor.setCol1(col1);

anchor.setCol2(col2);

anchor.setRow1(row1);

anchor.setRow2(row2);

drawing.createPicture(anchor, pictureIdx);

} catch(Exception e) {

}

}

I am able to draw line around image using XSSFSimpleShape but it is not what I wanted exactly and also tried with setBorderXXX() but these borders or lines do not move with image as when set using MS Office.

First image show what I am getting and second shows what I want

BwTlK.jpg

解决方案

This can be achieved by using XSSFPicture's setLineXXX() method as follows,

private void drawImageOnExcelSheetForGLOS(XSSFSheet sitePhotosSheet,

int row1, int row2, int col1, int col2, String fileName) {

try {

InputStream is = new FileInputStream(fileName);

byte[] bytes = IOUtils.toByteArray(is);

int pictureIdx = sitePhotosSheet.getWorkbook().addPicture(bytes,Workbook.PICTURE_TYPE_JPEG);

is.close();

CreationHelper helper = sitePhotosSheet.getWorkbook().getCreationHelper();

XSSFDrawing drawing = sitePhotosSheet.createDrawingPatriarch();

ClientAnchor anchor = helper.createClientAnchor();

anchor.setAnchorType( ClientAnchor.MOVE_AND_RESIZE );

anchor.setCol1(col1);

anchor.setCol2(col2);

anchor.setRow1(row1);

anchor.setRow2(row2);

// setLineXXX() methods can be used to set line border to image

XSSFPicture pic = drawing.createPicture(anchor, pictureIdx);

// 0 indicates solid line

pic.setLineStyle(0);

// rgb color code for black line

pic.setLineStyleColor(0, 0, 0);

// double number for line width

pic.setLineWidth(1.5);

} catch(Exception e) {

e.printStackTrace();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值