org.apache.poi.ss.usermodel.ClientAnchor Java Examples
The following examples show how to use
org.apache.poi.ss.usermodel.ClientAnchor.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractExcelWriteExecutor.java From easyexcel with Apache License 2.0 | 7 votes |
private void setImageValue(CellData cellData, Cell cell) { Sheet sheet = cell.getSheet(); int index = sheet.getWorkbook().addPicture(cellData.getImageValue(), HSSFWorkbook.PICTURE_TYPE_PNG); Drawing drawing = sheet.getDrawingPatriarch(); if (drawing == null) { drawing = sheet.createDrawingPatriarch(); } CreationHelper helper = sheet.getWorkbook().getCreationHelper(); ClientAnchor anchor = helper.createClientAnchor(); anchor.setDx1(0); anchor.setDx2(0); anchor.setDy1(0); anchor.setDy2(0); anchor.setCol1(cell.getColumnIndex()); anchor.setCol2(cell.getColumnIndex() + 1); anchor.setRow1(cell.getRowIndex()); anchor.setRow2(cell.getRowIndex() + 1); anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE); drawing.createPicture(anchor, index); }
Example #2
Source File: SpreadsheetTab.java From taro with MIT License | 6 votes |
public void addPicture(int row, int col, byte[] bytes, int pictureType) { if (drawing == null) { drawing = sheet.createDrawingPatriarch(); } int pictureIndex = workbook.getPoiWorkbook().addPicture(bytes, pictureType); //add a picture shape ClientAnchor anchor = workbook.getPoiWorkbook().getCreationHelper().createClientAnchor(); //set top-left corner of the picture, //subsequent call of Picture#resize() will operate relative to it anchor.setCol1(col); anchor.setRow1(row); Picture pict = drawing.createPicture(anchor, pictureIndex); //auto-size picture relative to its top-left corner pict.resize(); }
Example #3
Source File: Prd3278IT.java From pentaho-reporting with GNU Lesser General Public License v2.1 | 6 votes |
@Test public void testImageAligningMiddleXLSX() { TableRectangle rect = new TableRectangle(); rect.setRect( 1, 1, 2, 2 ); StrictBounds b = new StrictBounds( StrictGeomUtility.toInternalValue( 200 ), StrictGeomUtility.toInternalValue( 200 ), StrictGeomUtility.toInternalValue( 200 ), StrictGeomUtility.toInternalValue( 200 ) ); ClientAnchor clientAnchor = xlsxImageHandler.computeClientAnchor( sheetLayout, rect, b ); Assert.assertEquals( 1, clientAnchor.getCol1() ); Assert.assertEquals( 1, clientAnchor.getCol2() ); Assert.assertEquals( 1, clientAnchor.getRow1() ); Assert.assertEquals( 1, clientAnchor.getRow2() ); Assert.assertEquals( 100 * XSSFShape.EMU_PER_POINT, clientAnchor.getDx1() ); Assert.assertEquals( 300 * XSSFShape.EMU_PER_POINT, clientAnchor.getDx2() ); Assert.assertEquals( 100 * XSSFShape.EMU_PER_POINT, clientAnchor.getDy1() ); Assert.assertEquals( 300 * XSSFShape.EMU_PER_POINT, clientAnchor.getDy2() ); }
Example #4
Source File: Prd3278IT.java From pentaho-reporting with GNU Lesser General Public License v2.1 | 6 votes |
@Test public void testImageAligningLeftAndTopXLSX() { TableRectangle rect = new TableRectangle(); rect.setRect( 1, 1, 2, 2 ); StrictBounds b = new StrictBounds( StrictGeomUtility.toInternalValue( 100 ), StrictGeomUtility.toInternalValue( 100 ), StrictGeomUtility.toInternalValue( 300 ), StrictGeomUtility.toInternalValue( 300 ) ); ClientAnchor clientAnchor = xlsxImageHandler.computeClientAnchor( sheetLayout, rect, b ); Assert.assertEquals( 1, clientAnchor.getCol1() ); Assert.assertEquals( 1, clientAnchor.getCol2() ); Assert.assertEquals( 1, clientAnchor.getRow1() ); Assert.assertEquals( 1, clientAnchor.getRow2() ); Assert.assertEquals( 0, clientAnchor.getDx1() ); Assert.assertEquals( 300 * XSSFShape.EMU_PER_POINT, clientAnchor.getDx2() ); Assert.assertEquals( 0, clientAnchor.getDy1() ); Assert.assertEquals( 300 * XSSFShape.EMU_PER_POINT, clientAnchor.getDy2() ); }
Example #5
Source File: Prd3278IT.java From pentaho-reporting with GNU Lesser General Public License v2.1 | 6 votes |
@Test public void testImageAligningToCellsXLSX() { TableRectangle rect = new TableRectangle(); rect.setRect( 1, 1, 2, 2 ); StrictBounds b = new StrictBounds( StrictGeomUtility.toInternalValue( 100 ), StrictGeomUtility.toInternalValue( 100 ), StrictGeomUtility.toInternalValue( 400 ), StrictGeomUtility.toInternalValue( 400 ) ); ClientAnchor clientAnchor = xlsxImageHandler.computeClientAnchor( sheetLayout, rect, b ); Assert.assertEquals( 1, clientAnchor.getCol1() ); Assert.assertEquals( 1, clientAnchor.getCol2() ); Assert.assertEquals( 1, clientAnchor.getRow1() ); Assert.assertEquals( 1, clientAnchor.getRow2() ); Assert.assertEquals( 0, clientAnchor.getDx1() ); Assert.assertEquals( 400 * XSSFShape.EMU_PER_POINT, clientAnchor.getDx2() ); Assert.assertEquals( 0, clientAnchor.getDy1() ); Assert.assertEquals( 400 * XSSFShape.EMU_PER_POINT, clientAnchor.getDy2() ); }
Example #6
Source File: Prd3278IT.java From pentaho-reporting with GNU Lesser General Public License v2.1 | 6 votes |
@Test public void testImageAligningMiddle() { TableRectangle rect = new TableRectangle(); rect.setRect( 1, 1, 2, 2 ); StrictBounds b = new StrictBounds( StrictGeomUtility.toInternalValue( 200 ), StrictGeomUtility.toInternalValue( 200 ), StrictGeomUtility.toInternalValue( 200 ), StrictGeomUtility.toInternalValue( 200 ) ); ClientAnchor clientAnchor = imageHandler.computeClientAnchor( sheetLayout, rect, b ); Assert.assertEquals( 1, clientAnchor.getCol1() ); Assert.assertEquals( 1, clientAnchor.getCol2() ); Assert.assertEquals( 1, clientAnchor.getRow1() ); Assert.assertEquals( 1, clientAnchor.getRow2() ); Assert.assertEquals( 255, clientAnchor.getDx1() ); Assert.assertEquals( 1023 * 3 / 4, clientAnchor.getDx2() ); Assert.assertEquals( 63, clientAnchor.getDy1() ); Assert.assertEquals( 255 * 3 / 4, clientAnchor.getDy2() ); }
Example #7
Source File: Prd3278IT.java From pentaho-reporting with GNU Lesser General Public License v2.1 | 6 votes |
@Test public void testImageAligningLeftAndTop() { TableRectangle rect = new TableRectangle(); rect.setRect( 1, 1, 2, 2 ); StrictBounds b = new StrictBounds( StrictGeomUtility.toInternalValue( 100 ), StrictGeomUtility.toInternalValue( 100 ), StrictGeomUtility.toInternalValue( 300 ), StrictGeomUtility.toInternalValue( 300 ) ); ClientAnchor clientAnchor = imageHandler.computeClientAnchor( sheetLayout, rect, b ); Assert.assertEquals( 1, clientAnchor.getCol1() ); Assert.assertEquals( 1, clientAnchor.getCol2() ); Assert.assertEquals( 1, clientAnchor.getRow1() ); Assert.assertEquals( 1, clientAnchor.getRow2() ); Assert.assertEquals( 0, clientAnchor.getDx1() ); Assert.assertEquals( 1023 * 3 / 4, clientAnchor.getDx2() ); Assert.assertEquals( 0, clientAnchor.getDy1() ); Assert.assertEquals( 255 * 3 / 4, clientAnchor.getDy2() ); }
Example #8
Source File: Prd3278IT.java From pentaho-reporting with GNU Lesser General Public License v2.1 | 6 votes |
@Test public void testImageAligningToCells() { TableRectangle rect = new TableRectangle(); rect.setRect( 1, 1, 2, 2 ); StrictBounds b = new StrictBounds( StrictGeomUtility.toInternalValue( 100 ), StrictGeomUtility.toInternalValue( 100 ), StrictGeomUtility.toInternalValue( 400 ), StrictGeomUtility.toInternalValue( 400 ) ); ClientAnchor clientAnchor = imageHandler.computeClientAnchor( sheetLayout, rect, b ); Assert.assertEquals( 1, clientAnchor.getCol1() ); Assert.assertEquals( 1, clientAnchor.getCol2() ); Assert.assertEquals( 1, clientAnchor.getRow1() ); Assert.assertEquals( 1, clientAnchor.getRow2() ); Assert.assertEquals( 0, clientAnchor.getDx1() ); Assert.assertEquals( 1023, clientAnchor.getDx2() ); Assert.assertEquals( 0, clientAnchor.getDy1() ); Assert.assertEquals( 255, clientAnchor.getDy2() ); }
Example #9
Source File: ExcelWriterStep.java From pentaho-kettle with Apache License 2.0 | 5 votes |
private Comment createCellComment( String author, String comment ) { // comments only supported for XLSX if ( data.sheet instanceof XSSFSheet ) { CreationHelper factory = data.wb.getCreationHelper(); Drawing drawing = data.sheet.createDrawingPatriarch(); ClientAnchor anchor = factory.createClientAnchor(); Comment cmt = drawing.createCellComment( anchor ); RichTextString str = factory.createRichTextString( comment ); cmt.setString( str ); cmt.setAuthor( author ); return cmt; } return null; }
Example #10
Source File: ExcelWriterTransform.java From hop with Apache License 2.0 | 5 votes |
private Comment createCellComment( String author, String comment ) { // comments only supported for XLSX if ( data.sheet instanceof XSSFSheet ) { CreationHelper factory = data.wb.getCreationHelper(); Drawing drawing = data.sheet.createDrawingPatriarch(); ClientAnchor anchor = factory.createClientAnchor(); Comment cmt = drawing.createCellComment( anchor ); RichTextString str = factory.createRichTextString( comment ); cmt.setString( str ); cmt.setAuthor( author ); return cmt; } return null; }
Example #11
Source File: ExcelImageHandler.java From pentaho-reporting with GNU Lesser General Public License v2.1 | 5 votes |
protected ClientAnchor computeExcel2003ClientAnchor( final SlimSheetLayout currentLayout, final TableRectangle rectangle, final StrictBounds cb ) { final int cell1x = rectangle.getX1(); final int cell1y = rectangle.getY1(); final int cell2x = Math.max( cell1x, rectangle.getX2() - 1 ); final int cell2y = Math.max( cell1y, rectangle.getY2() - 1 ); final long cell1xPos = currentLayout.getXPosition( cell1x ); final long cell1yPos = currentLayout.getYPosition( cell1y ); final long cell2xPos = currentLayout.getXPosition( cell2x ); final long cell2yPos = currentLayout.getYPosition( cell2y ); final int dx1 = (int) StrictGeomUtility.toExternalValue( ( cb.getX() - cell1xPos ) * XSSFShape.EMU_PER_POINT ); final int dy1 = (int) StrictGeomUtility.toExternalValue( ( cb.getY() - cell1yPos ) * XSSFShape.EMU_PER_POINT ); final int dx2 = (int) Math.max( 0, StrictGeomUtility.toExternalValue( ( cb.getX() + cb.getWidth() - cell2xPos ) * XSSFShape.EMU_PER_POINT ) ); final int dy2 = (int) Math.max( 0, StrictGeomUtility.toExternalValue( ( cb.getY() + cb.getHeight() - cell2yPos ) * XSSFShape.EMU_PER_POINT ) ); final ClientAnchor anchor = printerBase.getWorkbook().getCreationHelper().createClientAnchor(); anchor.setDx1( dx1 ); anchor.setDy1( dy1 ); anchor.setDx2( dx2 ); anchor.setDy2( dy2 ); anchor.setCol1( cell1x ); anchor.setRow1( cell1y ); anchor.setCol2( cell2x ); anchor.setRow2( cell2y ); anchor.setAnchorType( ClientAnchor.AnchorType.MOVE_DONT_RESIZE ); return anchor; }
Example #12
Source File: ExcelImageHandler.java From pentaho-reporting with GNU Lesser General Public License v2.1 | 5 votes |
protected ClientAnchor computeExcel97ClientAnchor( final SlimSheetLayout currentLayout, final TableRectangle rectangle, final StrictBounds cb ) { final int cell1x = rectangle.getX1(); final int cell1y = rectangle.getY1(); final int cell2x = Math.max( cell1x, rectangle.getX2() - 1 ); final int cell2y = Math.max( cell1y, rectangle.getY2() - 1 ); final long cell1width = currentLayout.getCellWidth( cell1x ); final long cell1height = currentLayout.getRowHeight( cell1y ); final long cell2width = currentLayout.getCellWidth( cell2x ); final long cell2height = currentLayout.getRowHeight( cell2y ); final long cell1xPos = currentLayout.getXPosition( cell1x ); final long cell1yPos = currentLayout.getYPosition( cell1y ); final long cell2xPos = currentLayout.getXPosition( cell2x ); final long cell2yPos = currentLayout.getYPosition( cell2y ); final int dx1 = (int) ( 1023 * ( ( cb.getX() - cell1xPos ) / (double) cell1width ) ); final int dy1 = (int) ( 255 * ( ( cb.getY() - cell1yPos ) / (double) cell1height ) ); final int dx2 = (int) ( 1023 * ( ( cb.getX() + cb.getWidth() - cell2xPos ) / (double) cell2width ) ); final int dy2 = (int) ( 255 * ( ( cb.getY() + cb.getHeight() - cell2yPos ) / (double) cell2height ) ); final ClientAnchor anchor = printerBase.getWorkbook().getCreationHelper().createClientAnchor(); anchor.setDx1( dx1 ); anchor.setDy1( dy1 ); anchor.setDx2( dx2 ); anchor.setDy2( dy2 ); anchor.setCol1( cell1x ); anchor.setRow1( cell1y ); anchor.setCol2( cell2x ); anchor.setRow2( cell2y ); anchor.setAnchorType( ClientAnchor.AnchorType.MOVE_DONT_RESIZE ); return anchor; }
Example #13
Source File: ExcelImageHandler.java From pentaho-reporting with GNU Lesser General Public License v2.1 | 5 votes |
protected ClientAnchor computeClientAnchor( final SlimSheetLayout currentLayout, final TableRectangle rectangle, final StrictBounds cb ) { if ( printerBase.isUseXlsxFormat() ) { return computeExcel2003ClientAnchor( currentLayout, rectangle, cb ); } else { return computeExcel97ClientAnchor( currentLayout, rectangle, cb ); } }
Example #14
Source File: AbstractInliner.java From yarg with Apache License 2.0 | 5 votes |
@Override public void inlineToXls(HSSFPatriarch patriarch, HSSFCell resultCell, Object paramValue, Matcher paramsMatcher) { try { Image image = new Image(paramValue, paramsMatcher); if (image.isValid()) { HSSFSheet sheet = resultCell.getSheet(); HSSFWorkbook workbook = sheet.getWorkbook(); int pictureIdx = workbook.addPicture(image.imageContent, Workbook.PICTURE_TYPE_JPEG); CreationHelper helper = workbook.getCreationHelper(); ClientAnchor anchor = helper.createClientAnchor(); anchor.setCol1(resultCell.getColumnIndex()); anchor.setRow1(resultCell.getRowIndex()); anchor.setCol2(resultCell.getColumnIndex()); anchor.setRow2(resultCell.getRowIndex()); if (patriarch == null) { throw new IllegalArgumentException(String.format("No HSSFPatriarch object provided. Charts on this sheet could cause this effect. Please check sheet %s", resultCell.getSheet().getSheetName())); } HSSFPicture picture = patriarch.createPicture(anchor, pictureIdx); Dimension size = ImageUtils.getDimensionFromAnchor(picture); double actualHeight = size.getHeight() / EMU_PER_PIXEL; double actualWidth = size.getWidth() / EMU_PER_PIXEL; picture.resize((double) image.width / actualWidth, (double) image.height / actualHeight); } } catch (IllegalArgumentException e) { throw new ReportFormattingException("An error occurred while inserting bitmap to xls file", e); } }
Example #15
Source File: WatermarkExcelTests.java From kbase-doc with Apache License 2.0 | 5 votes |
@Test public void test2() throws IOException { //create a new workbook XSSFWorkbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); String imgPath = "D:\\Xiaoi\\logo\\logo.png"; //add picture data to this workbook. InputStream is = new FileInputStream(imgPath); byte[] bytes = IOUtils.toByteArray(is); int pictureIdx = wb.addPicture(bytes, XSSFWorkbook.PICTURE_TYPE_PNG); is.close(); CreationHelper helper = wb.getCreationHelper(); //create sheet Sheet sheet = wb.createSheet(); // Create the drawing patriarch. This is the top level container for all shapes. Drawing drawing = sheet.createDrawingPatriarch(); //add a picture shape ClientAnchor anchor = helper.createClientAnchor(); //set top-left corner of the picture, //subsequent call of Picture#resize() will operate relative to it anchor.setCol1(3); anchor.setRow1(2); anchor.setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE); Picture pict = drawing.createPicture(anchor, pictureIdx); //auto-size picture relative to its top-left corner pict.resize(); //save workbook String file = "E:\\ConvertTester\\excel\\picture.xls"; if(wb instanceof XSSFWorkbook) file += "x"; try (OutputStream fileOut = new FileOutputStream(file)) { wb.write(fileOut); } }
Example #16
Source File: HSSFComment.java From lams with GNU General Public License v2.0 | 5 votes |
@Override public ClientAnchor getClientAnchor() { HSSFAnchor ha = super.getAnchor(); if (ha instanceof ClientAnchor) { return (ClientAnchor) ha; } throw new IllegalStateException("Anchor can not be changed in " + ClientAnchor.class.getSimpleName()); }
Example #17
Source File: ImageAreaInfo.java From hy.common.report with Apache License 2.0 | 5 votes |
public void setAnchor(ClientAnchor anchor) { this.anchor = anchor; }
Example #18
Source File: JRXlsExporter.java From jasperreports with GNU Lesser General Public License v3.0 | 5 votes |
public static ClientAnchor.AnchorType getAnchorType(ImageAnchorTypeEnum anchorType) { switch (anchorType) { case MOVE_SIZE: return ClientAnchor.AnchorType.MOVE_AND_RESIZE; case NO_MOVE_NO_SIZE: return ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE; case MOVE_NO_SIZE: default: return ClientAnchor.AnchorType.MOVE_DONT_RESIZE; } }
Example #19
Source File: ImageAreaInfo.java From hy.common.report with Apache License 2.0 | 4 votes |
public ClientAnchor getAnchor() { return anchor; }
Example #20
Source File: ImageUtils.java From lams with GNU General Public License v2.0 | 4 votes |
/** * Calculates the dimensions in EMUs for the anchor of the given picture * * @param picture the picture containing the anchor * @return the dimensions in EMUs */ public static Dimension getDimensionFromAnchor(Picture picture) { ClientAnchor anchor = picture.getClientAnchor(); boolean isHSSF = (anchor instanceof HSSFClientAnchor); Sheet sheet = picture.getSheet(); double w = 0; int col2 = anchor.getCol1(); //space in the leftmost cell w = sheet.getColumnWidthInPixels(col2++); if (isHSSF) { w *= 1 - anchor.getDx1()/1024d; } else { w -= anchor.getDx1()/(double)EMU_PER_PIXEL; } while(col2 < anchor.getCol2()){ w += sheet.getColumnWidthInPixels(col2++); } if (isHSSF) { w += sheet.getColumnWidthInPixels(col2) * anchor.getDx2()/1024d; } else { w += anchor.getDx2()/(double)EMU_PER_PIXEL; } double h = 0; int row2 = anchor.getRow1(); h = getRowHeightInPixels(sheet,row2++); if (isHSSF) { h *= 1 - anchor.getDy1()/256d; } else { h -= anchor.getDy1()/(double)EMU_PER_PIXEL; } while(row2 < anchor.getRow2()){ h += getRowHeightInPixels(sheet,row2++); } if (isHSSF) { h += getRowHeightInPixels(sheet,row2) * anchor.getDy2()/256; } else { h += anchor.getDy2()/(double)EMU_PER_PIXEL; } w *= EMU_PER_PIXEL; h *= EMU_PER_PIXEL; return new Dimension((int)Math.rint(w), (int)Math.rint(h)); }
Example #21
Source File: HSSFPatriarch.java From lams with GNU General Public License v2.0 | 4 votes |
@Override public HSSFComment createCellComment(ClientAnchor anchor) { return createComment((HSSFAnchor) anchor); }
Example #22
Source File: HSSFPatriarch.java From lams with GNU General Public License v2.0 | 4 votes |
@Override @NotImplemented public Chart createChart(ClientAnchor anchor) { throw new RuntimeException("NotImplemented"); }
Example #23
Source File: AbstractExcelFactory.java From myexcel with Apache License 2.0 | 4 votes |
private void setImage(Td td, Sheet sheet) { if (td.getFile() == null) { return; } try { if (createHelper == null) { createHelper = workbook.getCreationHelper(); } byte[] bytes = Files.readAllBytes(td.getFile().toPath()); String fileName = td.getFile().getName(); int format; String suffix = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(); switch (suffix) { case "jpg": case "jpeg": format = Workbook.PICTURE_TYPE_JPEG; break; case "png": format = Workbook.PICTURE_TYPE_PNG; break; case "dib": format = Workbook.PICTURE_TYPE_DIB; break; case "emf": format = Workbook.PICTURE_TYPE_EMF; break; case "pict": format = Workbook.PICTURE_TYPE_PICT; break; case "wmf": format = Workbook.PICTURE_TYPE_WMF; break; default: throw new IllegalArgumentException("Invalid image type"); } int pictureIdx = workbook.addPicture(bytes, format); Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = createHelper.createClientAnchor(); anchor.setCol1(td.getCol()); anchor.setRow1(td.getRow()); Picture pict = drawing.createPicture(anchor, pictureIdx); pict.resize(1, 1); } catch (IOException e) { throw new RuntimeException(e); } }
Example #24
Source File: ImageAreaInfo.java From hy.common.report with Apache License 2.0 | 4 votes |
public ImageAreaInfo(ClientAnchor i_Anchor ,PictureData i_PictureData) { this.anchor = i_Anchor; this.pictureData = i_PictureData; }
Example #25
Source File: Prd3278IT.java From pentaho-reporting with GNU Lesser General Public License v2.1 | 4 votes |
public ClientAnchor computeClientAnchor( final SlimSheetLayout currentLayout, final TableRectangle rectangle, final StrictBounds cb ) { return super.computeClientAnchor( currentLayout, rectangle, cb ); }
Example #26
Source File: POIImage.java From excel2pdf with Apache License 2.0 | 4 votes |
public ClientAnchor getAnchor() { return anchor; }
Example #27
Source File: POIImage.java From excel2pdf with Apache License 2.0 | 4 votes |
public void setAnchor(ClientAnchor anchor) { this.anchor = anchor; }
Example #28
Source File: DefaultCellCommentHandler.java From xlsmapper with Apache License 2.0 | 4 votes |
/** * コメントの位置、サイズを作成する。 * @param drawing * @param text 書き込むコメント * @param cell 書込み対象のセル * @param commentOption コメントのオプション * @return コメントの表示位置 */ protected ClientAnchor createAnchor(final Drawing<?> drawing, final String text, final Cell cell, final Optional<XlsCommentOption> commentOption) { final CellPosition address = CellPosition.of(cell); // コメントを開業で分割し、最長の行を取得する。 String[] split = text.split("\r\n|\r|\n"); int maxLength = Arrays.stream(split) .mapToInt(str -> str.getBytes(Charset.forName("Windows-31j")).length) .max().orElse(0); /* * コメントの横サイズ。文字数(バイト数)をもとに決定。 * ・1セルの文字数を元に出す。 * ・columnWidthは、1文字の幅を1/256にしたものが単位となる。 * ・最大3列分とする。 */ int charPerColumn = cell.getSheet().getColumnWidth(cell.getColumnIndex())/256; int commentColumnSize = (int)Math.ceil(maxLength*1.0 / charPerColumn); int columnSize = commentColumnSize; int lineWrappingCount = 0; if(commentColumnSize > maxHorizontalSize) { columnSize = maxHorizontalSize; // 行の折り返し回数を計算する lineWrappingCount = commentColumnSize / maxHorizontalSize; } if(commentOption.isPresent() && commentOption.get().horizontalSize() > 0) { // 直接指定されている場合 columnSize = commentOption.get().horizontalSize(); // 行の折り返し回数を計算する lineWrappingCount = columnSize / maxHorizontalSize; } // コメントの縦サイズ。行数をもとに決定。 int rowSize = split.length + lineWrappingCount > maxVerticalSize ? maxVerticalSize : split.length + lineWrappingCount; if(commentOption.isPresent() && commentOption.get().verticalSize() > 0) { // 直接指定されている場合 rowSize = commentOption.get().verticalSize(); } return drawing.createAnchor( 0, 0, 0, 0, address.getColumn() + horizontalPrefix, address.getRow() + vertialPrefix, address.getColumn() + horizontalPrefix + columnSize, address.getRow() + vertialPrefix + rowSize); }
Example #29
Source File: DefaultCellCommentHandler.java From xlsmapper with Apache License 2.0 | 4 votes |
@Override public void handleSave(final Cell cell, final Optional<String> text, final Optional<XlsCommentOption> commentOption) { if(!text.isPresent()) { // コメントが空のとき commentOption.ifPresent(option -> { if(option.removeIfEmpty()) { // コメントが空のとき既存のコメントを削除する cell.removeCellComment(); } }); return; } final Sheet sheet = cell.getSheet(); final CreationHelper helper = sheet.getWorkbook().getCreationHelper(); final Drawing<?> drawing = sheet.createDrawingPatriarch(); final Comment comment; RichTextString richText = helper.createRichTextString(text.get()); if(cell.getCellComment() == null) { ClientAnchor anchor = createAnchor(drawing, text.get(), cell, commentOption); comment = drawing.createCellComment(anchor); applyCommentFormat(richText, cell); } else { // 既存のコメントが存在する場合は、書式やサイズをコピーして使用する。 comment = cell.getCellComment(); RichTextString orgText = comment.getString(); if(orgText.numFormattingRuns() > 0) { copyCommentFormat(richText, orgText); } else { applyCommentFormat(richText, cell); } } comment.setString(richText); // コメントの表示状態の更新 commentOption.ifPresent(option -> comment.setVisible(option.visible())); cell.setCellComment(comment); }
Example #30
Source File: PageHandler.java From birt with Eclipse Public License 1.0 | 4 votes |
/** * <p> * Process a CellImage from the images list and place the image on the sheet. * </p><p> * This involves changing the row height as necesssary and determining the column spread of the image. * </p> * @param cellImage * The image to be placed on the sheet. */ private void processCellImage( HandlerState state, Drawing drawing, CellImage cellImage ) { Coordinate location = cellImage.location; Cell cell = state.currentSheet.getRow( location.getRow() ).getCell( location.getCol() ); IImageContent image = cellImage.image; StyleManagerUtils smu = state.getSmu(); float ptHeight = cell.getRow().getHeightInPoints(); if( image.getHeight() != null ) { ptHeight = smu.fontSizeInPoints( image.getHeight().toString() ); } // Get image width int endCol = cell.getColumnIndex(); double lastColWidth = ClientAnchorConversions.widthUnits2Millimetres( (short)state.currentSheet.getColumnWidth( endCol ) ) + 2.0; int dx = smu.anchorDxFromMM( lastColWidth, lastColWidth ); double mmWidth = 0.0; if( smu.isAbsolute(image.getWidth())) { mmWidth = image.getWidth().convertTo(DimensionType.UNITS_MM); } else if(smu.isPixels(image.getWidth())) { mmWidth = ClientAnchorConversions.pixels2Millimetres( image.getWidth().getMeasure() ); } // Allow image to span multiple columns CellRangeAddress mergedRegion = getMergedRegionBegunBy( state.currentSheet, location.getRow(), location.getCol() ); if( (cellImage.spanColumns) || ( mergedRegion != null ) ) { log.debug( "Image size: ", image.getWidth(), " translates as mmWidth = ", mmWidth ); if( mmWidth > 0) { double mmAccumulatedWidth = 0; int endColLimit = cellImage.spanColumns ? 256 : mergedRegion.getLastColumn(); for( endCol = cell.getColumnIndex(); mmAccumulatedWidth < mmWidth && endCol < endColLimit; ++ endCol ) { lastColWidth = ClientAnchorConversions.widthUnits2Millimetres( (short)state.currentSheet.getColumnWidth( endCol ) ) + 2.0; mmAccumulatedWidth += lastColWidth; log.debug( "lastColWidth = ", lastColWidth, "; mmAccumulatedWidth = ", mmAccumulatedWidth); } if( mmAccumulatedWidth > mmWidth ) { mmAccumulatedWidth -= lastColWidth; --endCol; double mmShort = mmWidth - mmAccumulatedWidth; dx = smu.anchorDxFromMM( mmShort, lastColWidth ); } } } else { float widthRatio = (float)(mmWidth / lastColWidth); ptHeight = ptHeight / widthRatio; } int rowsSpanned = state.findRowsSpanned( cell.getRowIndex(), cell.getColumnIndex() ); float neededRowHeightPoints = ptHeight; for( int i = 0; i < rowsSpanned; ++i ) { int rowIndex = cell.getRowIndex() + 1 + i; neededRowHeightPoints -= state.currentSheet.getRow(rowIndex).getHeightInPoints(); } if( neededRowHeightPoints > cell.getRow().getHeightInPoints()) { cell.getRow().setHeightInPoints( neededRowHeightPoints ); } // ClientAnchor anchor = wb.getCreationHelper().createClientAnchor(); ClientAnchor anchor = state.getWb().getCreationHelper().createClientAnchor(); anchor.setCol1(cell.getColumnIndex()); anchor.setRow1(cell.getRowIndex()); anchor.setCol2(endCol); anchor.setRow2(cell.getRowIndex() + rowsSpanned); anchor.setDx2(dx); anchor.setDy2( smu.anchorDyFromPoints( ptHeight, cell.getRow().getHeightInPoints() ) ); anchor.setAnchorType(ClientAnchor.MOVE_DONT_RESIZE); drawing.createPicture(anchor, cellImage.imageIdx); }