use of org.apache.poi.xssf.usermodel.XSSFClientAnchor in project poi by apache.
the class SXSSFPicture method getPreferredSize.
/**
* Calculate the preferred size for this picture.
*
* @param scale the amount by which image dimensions are multiplied relative to the original size.
* @return XSSFClientAnchor with the preferred size for this image
*/
public XSSFClientAnchor getPreferredSize(double scale) {
XSSFClientAnchor anchor = getClientAnchor();
XSSFPictureData data = getPictureData();
Dimension size = getImageDimension(data.getPackagePart(), data.getPictureType());
double scaledWidth = size.getWidth() * scale;
double scaledHeight = size.getHeight() * scale;
float w = 0;
int col2 = anchor.getCol1() - 1;
while (w <= scaledWidth) {
w += getColumnWidthInPixels(++col2);
}
assert (w > scaledWidth);
double cw = getColumnWidthInPixels(col2);
double deltaW = w - scaledWidth;
int dx2 = (int) (XSSFShape.EMU_PER_PIXEL * (cw - deltaW));
anchor.setCol2(col2);
anchor.setDx2(dx2);
double h = 0;
int row2 = anchor.getRow1() - 1;
while (h <= scaledHeight) {
h += getRowHeightInPixels(++row2);
}
assert (h > scaledHeight);
double ch = getRowHeightInPixels(row2);
double deltaH = h - scaledHeight;
int dy2 = (int) (XSSFShape.EMU_PER_PIXEL * (ch - deltaH));
anchor.setRow2(row2);
anchor.setDy2(dy2);
CTPositiveSize2D size2d = getCTPicture().getSpPr().getXfrm().getExt();
size2d.setCx((long) (scaledWidth * XSSFShape.EMU_PER_PIXEL));
size2d.setCy((long) (scaledHeight * XSSFShape.EMU_PER_PIXEL));
return anchor;
}
use of org.apache.poi.xssf.usermodel.XSSFClientAnchor in project poi by apache.
the class TestCommentsTable method writeRead.
@Test
public void writeRead() {
XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("WithVariousData.xlsx");
XSSFSheet sheet1 = workbook.getSheetAt(0);
XSSFSheet sheet2 = workbook.getSheetAt(1);
assertTrue(sheet1.hasComments());
assertFalse(sheet2.hasComments());
// Change on comment on sheet 1, and add another into
// sheet 2
Row r5 = sheet1.getRow(4);
Comment cc5 = r5.getCell(2).getCellComment();
cc5.setAuthor("Apache POI");
cc5.setString(new XSSFRichTextString("Hello!"));
Row r2s2 = sheet2.createRow(2);
Cell c1r2s2 = r2s2.createCell(1);
assertNull(c1r2s2.getCellComment());
Drawing<?> dg = sheet2.createDrawingPatriarch();
Comment cc2 = dg.createCellComment(new XSSFClientAnchor());
cc2.setAuthor("Also POI");
cc2.setString(new XSSFRichTextString("A new comment"));
c1r2s2.setCellComment(cc2);
// Save, and re-load the file
workbook = XSSFTestDataSamples.writeOutAndReadBack(workbook);
// Check we still have comments where we should do
sheet1 = workbook.getSheetAt(0);
sheet2 = workbook.getSheetAt(1);
assertNotNull(sheet1.getRow(4).getCell(2).getCellComment());
assertNotNull(sheet1.getRow(6).getCell(2).getCellComment());
assertNotNull(sheet2.getRow(2).getCell(1).getCellComment());
// And check they still have the contents they should do
assertEquals("Apache POI", sheet1.getRow(4).getCell(2).getCellComment().getAuthor());
assertEquals("Nick Burch", sheet1.getRow(6).getCell(2).getCellComment().getAuthor());
assertEquals("Also POI", sheet2.getRow(2).getCell(1).getCellComment().getAuthor());
assertEquals("Hello!", sheet1.getRow(4).getCell(2).getCellComment().getString().getString());
}
use of org.apache.poi.xssf.usermodel.XSSFClientAnchor in project bamboobsc by billchen198318.
the class SimpleUtils method setCellPicture.
public static void setCellPicture(XSSFWorkbook wb, XSSFSheet sh, byte[] iconBytes, int row, int col) throws Exception {
int myPictureId = wb.addPicture(iconBytes, XSSFWorkbook.PICTURE_TYPE_PNG);
XSSFDrawing drawing = sh.createDrawingPatriarch();
XSSFClientAnchor myAnchor = new XSSFClientAnchor();
myAnchor.setCol1(col);
myAnchor.setRow1(row);
XSSFPicture myPicture = drawing.createPicture(myAnchor, myPictureId);
myPicture.resize();
}
use of org.apache.poi.xssf.usermodel.XSSFClientAnchor in project Gargoyle by callakrsos.
the class ExcelUtil method addComment.
/**
* 특정셀에 코멘트를 추가한다.
*
* @param sheet
* @param cell
* @param commentText
* @return
*/
public static void addComment(Sheet sheet, Cell cell, String commentText) {
XSSFDrawing patr = (XSSFDrawing) sheet.createDrawingPatriarch();
Comment comment = patr.createCellComment(new XSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5));
comment.setString(new XSSFRichTextString(commentText));
cell.setCellComment(comment);
}
use of org.apache.poi.xssf.usermodel.XSSFClientAnchor in project poi by apache.
the class SXSSFPicture method resize.
/**
* Reset the image to the original size.
* <p>
* Please note, that this method works correctly only for workbooks
* with the default font size (Calibri 11pt for .xlsx).
* If the default font is changed the resized image can be streched vertically or horizontally.
* </p>
*
* @param scale the amount by which image dimensions are multiplied relative to the original size.
* <code>resize(1.0)</code> sets the original size, <code>resize(0.5)</code> resize to 50% of the original,
* <code>resize(2.0)</code> resizes to 200% of the original.
*/
@Override
public void resize(double scale) {
XSSFClientAnchor anchor = getClientAnchor();
XSSFClientAnchor pref = getPreferredSize(scale);
int row2 = anchor.getRow1() + (pref.getRow2() - pref.getRow1());
int col2 = anchor.getCol1() + (pref.getCol2() - pref.getCol1());
anchor.setCol2(col2);
anchor.setDx1(0);
anchor.setDx2(pref.getDx2());
anchor.setRow2(row2);
anchor.setDy1(0);
anchor.setDy2(pref.getDy2());
}
Aggregations