Search in sources :

Example 1 with HSSFChildAnchor

use of org.apache.poi.hssf.usermodel.HSSFChildAnchor 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 2 with HSSFChildAnchor

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

the class TestDrawingShapes method testRemoveShapes.

@Test
public void testRemoveShapes() throws IOException {
    HSSFWorkbook wb1 = new HSSFWorkbook();
    HSSFSheet sheet = wb1.createSheet();
    HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
    HSSFSimpleShape rectangle = patriarch.createSimpleShape(new HSSFClientAnchor());
    rectangle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE);
    int idx = wb1.addPicture(new byte[] { 1, 2, 3 }, Workbook.PICTURE_TYPE_JPEG);
    patriarch.createPicture(new HSSFClientAnchor(), idx);
    patriarch.createCellComment(new HSSFClientAnchor());
    HSSFPolygon polygon = patriarch.createPolygon(new HSSFClientAnchor());
    polygon.setPoints(new int[] { 1, 2 }, new int[] { 2, 3 });
    patriarch.createTextbox(new HSSFClientAnchor());
    HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor());
    group.createTextbox(new HSSFChildAnchor());
    group.createPicture(new HSSFChildAnchor(), idx);
    assertEquals(patriarch.getChildren().size(), 6);
    assertEquals(group.getChildren().size(), 2);
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 12);
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);
    HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
    wb1.close();
    sheet = wb2.getSheetAt(0);
    patriarch = sheet.getDrawingPatriarch();
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 12);
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);
    assertEquals(patriarch.getChildren().size(), 6);
    group = (HSSFShapeGroup) patriarch.getChildren().get(5);
    group.removeShape(group.getChildren().get(0));
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 10);
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);
    HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2);
    wb2.close();
    sheet = wb3.getSheetAt(0);
    patriarch = sheet.getDrawingPatriarch();
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 10);
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);
    group = (HSSFShapeGroup) patriarch.getChildren().get(5);
    patriarch.removeShape(group);
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 8);
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);
    HSSFWorkbook wb4 = HSSFTestDataSamples.writeOutAndReadBack(wb3);
    wb3.close();
    sheet = wb4.getSheetAt(0);
    patriarch = sheet.getDrawingPatriarch();
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 8);
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);
    assertEquals(patriarch.getChildren().size(), 5);
    HSSFShape shape = patriarch.getChildren().get(0);
    patriarch.removeShape(shape);
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 6);
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);
    assertEquals(patriarch.getChildren().size(), 4);
    HSSFWorkbook wb5 = HSSFTestDataSamples.writeOutAndReadBack(wb4);
    wb4.close();
    sheet = wb5.getSheetAt(0);
    patriarch = sheet.getDrawingPatriarch();
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 6);
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);
    assertEquals(patriarch.getChildren().size(), 4);
    HSSFPicture picture = (HSSFPicture) patriarch.getChildren().get(0);
    patriarch.removeShape(picture);
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 5);
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);
    assertEquals(patriarch.getChildren().size(), 3);
    HSSFWorkbook wb6 = HSSFTestDataSamples.writeOutAndReadBack(wb5);
    wb5.close();
    sheet = wb6.getSheetAt(0);
    patriarch = sheet.getDrawingPatriarch();
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 5);
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1);
    assertEquals(patriarch.getChildren().size(), 3);
    HSSFComment comment = (HSSFComment) patriarch.getChildren().get(0);
    patriarch.removeShape(comment);
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 3);
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0);
    assertEquals(patriarch.getChildren().size(), 2);
    HSSFWorkbook wb7 = HSSFTestDataSamples.writeOutAndReadBack(wb6);
    wb6.close();
    sheet = wb7.getSheetAt(0);
    patriarch = sheet.getDrawingPatriarch();
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 3);
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0);
    assertEquals(patriarch.getChildren().size(), 2);
    polygon = (HSSFPolygon) patriarch.getChildren().get(0);
    patriarch.removeShape(polygon);
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 2);
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0);
    assertEquals(patriarch.getChildren().size(), 1);
    HSSFWorkbook wb8 = HSSFTestDataSamples.writeOutAndReadBack(wb7);
    wb7.close();
    sheet = wb8.getSheetAt(0);
    patriarch = sheet.getDrawingPatriarch();
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 2);
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0);
    assertEquals(patriarch.getChildren().size(), 1);
    HSSFTextbox textbox = (HSSFTextbox) patriarch.getChildren().get(0);
    patriarch.removeShape(textbox);
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 0);
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0);
    assertEquals(patriarch.getChildren().size(), 0);
    HSSFWorkbook wb9 = HSSFTestDataSamples.writeOutAndReadBack(wb8);
    wb8.close();
    sheet = wb9.getSheetAt(0);
    patriarch = sheet.getDrawingPatriarch();
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 0);
    assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0);
    assertEquals(patriarch.getChildren().size(), 0);
    wb9.close();
}
Also used : HSSFPatriarch(org.apache.poi.hssf.usermodel.HSSFPatriarch) HSSFShapeGroup(org.apache.poi.hssf.usermodel.HSSFShapeGroup) HSSFComment(org.apache.poi.hssf.usermodel.HSSFComment) HSSFSimpleShape(org.apache.poi.hssf.usermodel.HSSFSimpleShape) HSSFClientAnchor(org.apache.poi.hssf.usermodel.HSSFClientAnchor) HSSFPicture(org.apache.poi.hssf.usermodel.HSSFPicture) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFPolygon(org.apache.poi.hssf.usermodel.HSSFPolygon) HSSFChildAnchor(org.apache.poi.hssf.usermodel.HSSFChildAnchor) HSSFShape(org.apache.poi.hssf.usermodel.HSSFShape) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFTextbox(org.apache.poi.hssf.usermodel.HSSFTextbox) Test(org.junit.Test)

Aggregations

HSSFChildAnchor (org.apache.poi.hssf.usermodel.HSSFChildAnchor)2 HSSFClientAnchor (org.apache.poi.hssf.usermodel.HSSFClientAnchor)2 EscherChildAnchorRecord (org.apache.poi.ddf.EscherChildAnchorRecord)1 EscherClientAnchorRecord (org.apache.poi.ddf.EscherClientAnchorRecord)1 HSSFComment (org.apache.poi.hssf.usermodel.HSSFComment)1 HSSFPatriarch (org.apache.poi.hssf.usermodel.HSSFPatriarch)1 HSSFPicture (org.apache.poi.hssf.usermodel.HSSFPicture)1 HSSFPolygon (org.apache.poi.hssf.usermodel.HSSFPolygon)1 HSSFShape (org.apache.poi.hssf.usermodel.HSSFShape)1 HSSFShapeGroup (org.apache.poi.hssf.usermodel.HSSFShapeGroup)1 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)1 HSSFSimpleShape (org.apache.poi.hssf.usermodel.HSSFSimpleShape)1 HSSFTextbox (org.apache.poi.hssf.usermodel.HSSFTextbox)1 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)1 Test (org.junit.Test)1