Search in sources :

Example 1 with HSSFClientAnchor

use of org.apache.poi.hssf.usermodel.HSSFClientAnchor in project poi by apache.

the class TestDrawingShapes method checkWorkbookBack.

private void checkWorkbookBack(HSSFWorkbook wb) throws IOException {
    HSSFWorkbook wbBack = HSSFTestDataSamples.writeOutAndReadBack(wb);
    assertNotNull(wbBack);
    HSSFSheet sheetBack = wbBack.getSheetAt(0);
    assertNotNull(sheetBack);
    HSSFPatriarch patriarchBack = sheetBack.getDrawingPatriarch();
    assertNotNull(patriarchBack);
    List<HSSFShape> children = patriarchBack.getChildren();
    assertEquals(4, children.size());
    HSSFShape hssfShape = children.get(0);
    assertTrue(hssfShape instanceof HSSFSimpleShape);
    HSSFAnchor anchor = hssfShape.getAnchor();
    assertTrue(anchor instanceof HSSFClientAnchor);
    assertEquals(0, anchor.getDx1());
    assertEquals(512, anchor.getDx2());
    assertEquals(0, anchor.getDy1());
    assertEquals(100, anchor.getDy2());
    HSSFClientAnchor cAnchor = (HSSFClientAnchor) anchor;
    assertEquals(1, cAnchor.getCol1());
    assertEquals(1, cAnchor.getCol2());
    assertEquals(1, cAnchor.getRow1());
    assertEquals(1, cAnchor.getRow2());
    hssfShape = children.get(1);
    assertTrue(hssfShape instanceof HSSFSimpleShape);
    anchor = hssfShape.getAnchor();
    assertTrue(anchor instanceof HSSFClientAnchor);
    assertEquals(512, anchor.getDx1());
    assertEquals(1024, anchor.getDx2());
    assertEquals(0, anchor.getDy1());
    assertEquals(100, anchor.getDy2());
    cAnchor = (HSSFClientAnchor) anchor;
    assertEquals(1, cAnchor.getCol1());
    assertEquals(1, cAnchor.getCol2());
    assertEquals(1, cAnchor.getRow1());
    assertEquals(1, cAnchor.getRow2());
    hssfShape = children.get(2);
    assertTrue(hssfShape instanceof HSSFSimpleShape);
    anchor = hssfShape.getAnchor();
    assertTrue(anchor instanceof HSSFClientAnchor);
    assertEquals(0, anchor.getDx1());
    assertEquals(512, anchor.getDx2());
    assertEquals(0, anchor.getDy1());
    assertEquals(100, anchor.getDy2());
    cAnchor = (HSSFClientAnchor) anchor;
    assertEquals(2, cAnchor.getCol1());
    assertEquals(2, cAnchor.getCol2());
    assertEquals(2, cAnchor.getRow1());
    assertEquals(2, cAnchor.getRow2());
    hssfShape = children.get(3);
    assertTrue(hssfShape instanceof HSSFSimpleShape);
    anchor = hssfShape.getAnchor();
    assertTrue(anchor instanceof HSSFClientAnchor);
    assertEquals(0, anchor.getDx1());
    assertEquals(512, anchor.getDx2());
    assertEquals(100, anchor.getDy1());
    assertEquals(200, anchor.getDy2());
    cAnchor = (HSSFClientAnchor) anchor;
    assertEquals(2, cAnchor.getCol1());
    assertEquals(2, cAnchor.getCol2());
    assertEquals(2, cAnchor.getRow1());
    assertEquals(2, cAnchor.getRow2());
    wbBack.close();
}
Also used : HSSFPatriarch(org.apache.poi.hssf.usermodel.HSSFPatriarch) HSSFSimpleShape(org.apache.poi.hssf.usermodel.HSSFSimpleShape) HSSFClientAnchor(org.apache.poi.hssf.usermodel.HSSFClientAnchor) HSSFAnchor(org.apache.poi.hssf.usermodel.HSSFAnchor) HSSFShape(org.apache.poi.hssf.usermodel.HSSFShape) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 2 with HSSFClientAnchor

use of org.apache.poi.hssf.usermodel.HSSFClientAnchor in project poi by apache.

the class TestDrawingShapes method testHSSFShapeCompatibility.

@Test
public void testHSSFShapeCompatibility() {
    HSSFSimpleShape shape = new HSSFSimpleShape(null, new HSSFClientAnchor());
    shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
    assertEquals(0x08000040, shape.getLineStyleColor());
    assertEquals(0x08000009, shape.getFillColor());
    assertEquals(HSSFShape.LINEWIDTH_DEFAULT, shape.getLineWidth());
    assertEquals(HSSFShape.LINESTYLE_SOLID, shape.getLineStyle());
    assertFalse(shape.isNoFill());
    EscherOptRecord opt = shape.getOptRecord();
    assertEquals(7, opt.getEscherProperties().size());
    assertTrue(((EscherBoolProperty) opt.lookup(EscherProperties.GROUPSHAPE__PRINT)).isTrue());
    assertTrue(((EscherBoolProperty) opt.lookup(EscherProperties.LINESTYLE__NOLINEDRAWDASH)).isTrue());
    assertEquals(0x00000004, ((EscherSimpleProperty) opt.lookup(EscherProperties.GEOMETRY__SHAPEPATH)).getPropertyValue());
    assertNull(opt.lookup(EscherProperties.TEXT__SIZE_TEXT_TO_FIT_SHAPE));
}
Also used : HSSFSimpleShape(org.apache.poi.hssf.usermodel.HSSFSimpleShape) HSSFClientAnchor(org.apache.poi.hssf.usermodel.HSSFClientAnchor) Test(org.junit.Test)

Example 3 with HSSFClientAnchor

use of org.apache.poi.hssf.usermodel.HSSFClientAnchor in project poi by apache.

the class ConvertAnchor method createAnchor.

public static EscherRecord createAnchor(HSSFAnchor userAnchor) {
    if (userAnchor instanceof HSSFClientAnchor) {
        HSSFClientAnchor a = (HSSFClientAnchor) userAnchor;
        EscherClientAnchorRecord anchor = new EscherClientAnchorRecord();
        anchor.setRecordId(EscherClientAnchorRecord.RECORD_ID);
        anchor.setOptions((short) 0x0000);
        anchor.setFlag(a.getAnchorType().value);
        anchor.setCol1((short) Math.min(a.getCol1(), a.getCol2()));
        anchor.setDx1((short) a.getDx1());
        anchor.setRow1((short) Math.min(a.getRow1(), a.getRow2()));
        anchor.setDy1((short) a.getDy1());
        anchor.setCol2((short) Math.max(a.getCol1(), a.getCol2()));
        anchor.setDx2((short) a.getDx2());
        anchor.setRow2((short) Math.max(a.getRow1(), a.getRow2()));
        anchor.setDy2((short) a.getDy2());
        return anchor;
    }
    HSSFChildAnchor a = (HSSFChildAnchor) userAnchor;
    EscherChildAnchorRecord anchor = new EscherChildAnchorRecord();
    anchor.setRecordId(EscherChildAnchorRecord.RECORD_ID);
    anchor.setOptions((short) 0x0000);
    anchor.setDx1((short) Math.min(a.getDx1(), a.getDx2()));
    anchor.setDy1((short) Math.min(a.getDy1(), a.getDy2()));
    anchor.setDx2((short) Math.max(a.getDx2(), a.getDx1()));
    anchor.setDy2((short) Math.max(a.getDy2(), a.getDy1()));
    return anchor;
}
Also used : EscherChildAnchorRecord(org.apache.poi.ddf.EscherChildAnchorRecord) HSSFClientAnchor(org.apache.poi.hssf.usermodel.HSSFClientAnchor) EscherClientAnchorRecord(org.apache.poi.ddf.EscherClientAnchorRecord) HSSFChildAnchor(org.apache.poi.hssf.usermodel.HSSFChildAnchor)

Example 4 with HSSFClientAnchor

use of org.apache.poi.hssf.usermodel.HSSFClientAnchor in project poi by apache.

the class ImageUtils method setPreferredSize.

/**
     * Calculate and set the preferred size (anchor) for this picture.
     *
     * @param scaleX the amount by which image width is multiplied relative to the original width.
     * @param scaleY the amount by which image height is multiplied relative to the original height.
     * @return the new Dimensions of the scaled picture in EMUs
     */
public static Dimension setPreferredSize(Picture picture, double scaleX, double scaleY) {
    ClientAnchor anchor = picture.getClientAnchor();
    boolean isHSSF = (anchor instanceof HSSFClientAnchor);
    PictureData data = picture.getPictureData();
    Sheet sheet = picture.getSheet();
    // in pixel
    Dimension imgSize = getImageDimension(new ByteArrayInputStream(data.getData()), data.getPictureType());
    // in emus
    Dimension anchorSize = ImageUtils.getDimensionFromAnchor(picture);
    final double scaledWidth = (scaleX == Double.MAX_VALUE) ? imgSize.getWidth() : anchorSize.getWidth() / EMU_PER_PIXEL * scaleX;
    final double scaledHeight = (scaleY == Double.MAX_VALUE) ? imgSize.getHeight() : anchorSize.getHeight() / EMU_PER_PIXEL * scaleY;
    double w = 0;
    int col2 = anchor.getCol1();
    int dx2 = 0;
    //space in the leftmost cell
    w = sheet.getColumnWidthInPixels(col2++);
    if (isHSSF) {
        w *= 1d - anchor.getDx1() / 1024d;
    } else {
        w -= anchor.getDx1() / (double) EMU_PER_PIXEL;
    }
    while (w < scaledWidth) {
        w += sheet.getColumnWidthInPixels(col2++);
    }
    if (w > scaledWidth) {
        //calculate dx2, offset in the rightmost cell
        double cw = sheet.getColumnWidthInPixels(--col2);
        double delta = w - scaledWidth;
        if (isHSSF) {
            dx2 = (int) ((cw - delta) / cw * 1024);
        } else {
            dx2 = (int) ((cw - delta) * EMU_PER_PIXEL);
        }
        if (dx2 < 0)
            dx2 = 0;
    }
    anchor.setCol2(col2);
    anchor.setDx2(dx2);
    double h = 0;
    int row2 = anchor.getRow1();
    int dy2 = 0;
    h = getRowHeightInPixels(sheet, row2++);
    if (isHSSF) {
        h *= 1 - anchor.getDy1() / 256d;
    } else {
        h -= anchor.getDy1() / (double) EMU_PER_PIXEL;
    }
    while (h < scaledHeight) {
        h += getRowHeightInPixels(sheet, row2++);
    }
    if (h > scaledHeight) {
        double ch = getRowHeightInPixels(sheet, --row2);
        double delta = h - scaledHeight;
        if (isHSSF) {
            dy2 = (int) ((ch - delta) / ch * 256);
        } else {
            dy2 = (int) ((ch - delta) * EMU_PER_PIXEL);
        }
        if (dy2 < 0)
            dy2 = 0;
    }
    anchor.setRow2(row2);
    anchor.setDy2(dy2);
    Dimension dim = new Dimension((int) Math.round(scaledWidth * EMU_PER_PIXEL), (int) Math.round(scaledHeight * EMU_PER_PIXEL));
    return dim;
}
Also used : ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) HSSFClientAnchor(org.apache.poi.hssf.usermodel.HSSFClientAnchor) HSSFClientAnchor(org.apache.poi.hssf.usermodel.HSSFClientAnchor) ByteArrayInputStream(java.io.ByteArrayInputStream) Dimension(java.awt.Dimension) Sheet(org.apache.poi.ss.usermodel.Sheet) PictureData(org.apache.poi.ss.usermodel.PictureData)

Example 5 with HSSFClientAnchor

use of org.apache.poi.hssf.usermodel.HSSFClientAnchor in project poi by apache.

the class ImageUtils method getDimensionFromAnchor.

/**
     * 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));
}
Also used : ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) HSSFClientAnchor(org.apache.poi.hssf.usermodel.HSSFClientAnchor) HSSFClientAnchor(org.apache.poi.hssf.usermodel.HSSFClientAnchor) Dimension(java.awt.Dimension) Sheet(org.apache.poi.ss.usermodel.Sheet)

Aggregations

HSSFClientAnchor (org.apache.poi.hssf.usermodel.HSSFClientAnchor)20 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)15 HSSFPatriarch (org.apache.poi.hssf.usermodel.HSSFPatriarch)14 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)14 Test (org.junit.Test)13 HSSFSimpleShape (org.apache.poi.hssf.usermodel.HSSFSimpleShape)8 HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)6 HSSFShape (org.apache.poi.hssf.usermodel.HSSFShape)4 EscherAggregate (org.apache.poi.hssf.record.EscherAggregate)3 HSSFPicture (org.apache.poi.hssf.usermodel.HSSFPicture)3 HSSFTextbox (org.apache.poi.hssf.usermodel.HSSFTextbox)3 Dimension (java.awt.Dimension)2 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)2 HSSFChildAnchor (org.apache.poi.hssf.usermodel.HSSFChildAnchor)2 HSSFComment (org.apache.poi.hssf.usermodel.HSSFComment)2 HSSFFont (org.apache.poi.hssf.usermodel.HSSFFont)2 ClientAnchor (org.apache.poi.ss.usermodel.ClientAnchor)2 PictureData (org.apache.poi.ss.usermodel.PictureData)2 Sheet (org.apache.poi.ss.usermodel.Sheet)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1