Search in sources :

Example 16 with HSSFPatriarch

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

the class TestDrawingShapes method testDrawingGroups.

/**
     * HSSFShape tree bust be built correctly
     * Check file with such records structure:
     * -patriarch
     * --shape
     * --group
     * ---group
     * ----shape
     * ----shape
     * ---shape
     * ---group
     * ----shape
     * ----shape
     */
@Test
public void testDrawingGroups() throws IOException {
    HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls");
    HSSFSheet sheet = wb.getSheet("groups");
    HSSFPatriarch patriarch = sheet.getDrawingPatriarch();
    assertEquals(patriarch.getChildren().size(), 2);
    HSSFShapeGroup group = (HSSFShapeGroup) patriarch.getChildren().get(1);
    assertEquals(3, group.getChildren().size());
    HSSFShapeGroup group1 = (HSSFShapeGroup) group.getChildren().get(0);
    assertEquals(2, group1.getChildren().size());
    group1 = (HSSFShapeGroup) group.getChildren().get(2);
    assertEquals(2, group1.getChildren().size());
    wb.close();
}
Also used : HSSFPatriarch(org.apache.poi.hssf.usermodel.HSSFPatriarch) HSSFShapeGroup(org.apache.poi.hssf.usermodel.HSSFShapeGroup) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 17 with HSSFPatriarch

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

the class TestDrawingShapes method testShapeFlip.

@Test
public void testShapeFlip() 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);
    assertEquals(rectangle.isFlipVertical(), false);
    assertEquals(rectangle.isFlipHorizontal(), false);
    rectangle.setFlipVertical(true);
    assertEquals(rectangle.isFlipVertical(), true);
    rectangle.setFlipHorizontal(true);
    assertEquals(rectangle.isFlipHorizontal(), true);
    HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
    wb1.close();
    sheet = wb2.getSheetAt(0);
    patriarch = sheet.getDrawingPatriarch();
    rectangle = (HSSFSimpleShape) patriarch.getChildren().get(0);
    assertEquals(rectangle.isFlipHorizontal(), true);
    rectangle.setFlipHorizontal(false);
    assertEquals(rectangle.isFlipHorizontal(), false);
    assertEquals(rectangle.isFlipVertical(), true);
    rectangle.setFlipVertical(false);
    assertEquals(rectangle.isFlipVertical(), false);
    HSSFWorkbook wb3 = HSSFTestDataSamples.writeOutAndReadBack(wb2);
    wb2.close();
    sheet = wb3.getSheetAt(0);
    patriarch = sheet.getDrawingPatriarch();
    rectangle = (HSSFSimpleShape) patriarch.getChildren().get(0);
    assertEquals(rectangle.isFlipVertical(), false);
    assertEquals(rectangle.isFlipHorizontal(), false);
    wb3.close();
}
Also used : HSSFPatriarch(org.apache.poi.hssf.usermodel.HSSFPatriarch) HSSFSimpleShape(org.apache.poi.hssf.usermodel.HSSFSimpleShape) HSSFClientAnchor(org.apache.poi.hssf.usermodel.HSSFClientAnchor) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 18 with HSSFPatriarch

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

the class TestDrawingShapes method testShapeIds.

@Test
public void testShapeIds() throws IOException {
    HSSFWorkbook wb1 = new HSSFWorkbook();
    HSSFSheet sheet1 = wb1.createSheet();
    HSSFPatriarch patriarch1 = sheet1.createDrawingPatriarch();
    for (int i = 0; i < 2; i++) {
        patriarch1.createSimpleShape(new HSSFClientAnchor());
    }
    HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
    wb1.close();
    sheet1 = wb2.getSheetAt(0);
    patriarch1 = sheet1.getDrawingPatriarch();
    EscherAggregate agg1 = HSSFTestHelper.getEscherAggregate(patriarch1);
    // last shape ID cached in EscherDgRecord
    EscherDgRecord dg1 = agg1.getEscherContainer().getChildById(EscherDgRecord.RECORD_ID);
    assertEquals(1026, dg1.getLastMSOSPID());
    // iterate over shapes and check shapeId
    EscherContainerRecord spgrContainer = agg1.getEscherContainer().getChildContainers().get(0);
    // root spContainer + 2 spContainers for shapes
    assertEquals(3, spgrContainer.getChildRecords().size());
    EscherSpRecord sp0 = ((EscherContainerRecord) spgrContainer.getChild(0)).getChildById(EscherSpRecord.RECORD_ID);
    assertEquals(1024, sp0.getShapeId());
    EscherSpRecord sp1 = ((EscherContainerRecord) spgrContainer.getChild(1)).getChildById(EscherSpRecord.RECORD_ID);
    assertEquals(1025, sp1.getShapeId());
    EscherSpRecord sp2 = ((EscherContainerRecord) spgrContainer.getChild(2)).getChildById(EscherSpRecord.RECORD_ID);
    assertEquals(1026, sp2.getShapeId());
    wb2.close();
}
Also used : HSSFPatriarch(org.apache.poi.hssf.usermodel.HSSFPatriarch) EscherAggregate(org.apache.poi.hssf.record.EscherAggregate) HSSFClientAnchor(org.apache.poi.hssf.usermodel.HSSFClientAnchor) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 19 with HSSFPatriarch

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

the class TestDrawingAggregate method assertWriteAndReadBack.

/**
     * iterate over all sheets, aggregate drawing records (if there are any)
     * and remember information about the aggregated data.
     * Then serialize the workbook, read back and assert that the aggregated data is preserved.
     *
     * The assertion is strict meaning that the drawing data before and after save must be equal.
     */
private static void assertWriteAndReadBack(HSSFWorkbook wb) throws IOException {
    // map aggregate info by sheet index
    Map<Integer, DrawingAggregateInfo> aggs = new HashMap<Integer, DrawingAggregateInfo>();
    for (int i = 0; i < wb.getNumberOfSheets(); i++) {
        HSSFSheet sheet = wb.getSheetAt(i);
        DrawingAggregateInfo info = DrawingAggregateInfo.get(sheet);
        if (info != null) {
            aggs.put(i, info);
            HSSFPatriarch p = sheet.getDrawingPatriarch();
            // compare aggregate.serialize() with raw bytes from the record stream
            EscherAggregate agg = HSSFTestHelper.getEscherAggregate(p);
            byte[] dgBytes1 = info.getRawBytes();
            byte[] dgBytes2 = agg.serialize();
            assertEquals("different size of raw data ande aggregate.serialize()", dgBytes1.length, dgBytes2.length);
            assertTrue("raw drawing data (" + dgBytes1.length + " bytes) and aggregate.serialize() are different.", Arrays.equals(dgBytes1, dgBytes2));
        }
    }
    if (aggs.size() != 0) {
        HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb);
        for (int i = 0; i < wb2.getNumberOfSheets(); i++) {
            DrawingAggregateInfo info1 = aggs.get(i);
            if (info1 != null) {
                HSSFSheet sheet2 = wb2.getSheetAt(i);
                DrawingAggregateInfo info2 = DrawingAggregateInfo.get(sheet2);
                byte[] dgBytes1 = info1.getRawBytes();
                byte[] dgBytes2 = info2.getRawBytes();
                assertEquals("different size of drawing data before and after save", dgBytes1.length, dgBytes2.length);
                assertTrue("drawing data (" + dgBytes1.length + " bytes) before and after save is different.", Arrays.equals(dgBytes1, dgBytes2));
            }
        }
        wb2.close();
    }
}
Also used : HSSFPatriarch(org.apache.poi.hssf.usermodel.HSSFPatriarch) EscherAggregate(org.apache.poi.hssf.record.EscherAggregate) HashMap(java.util.HashMap) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 20 with HSSFPatriarch

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

the class TestDrawingShapes method testBug45312.

@Test
public void testBug45312() throws Exception {
    HSSFWorkbook wb = new HSSFWorkbook();
    try {
        HSSFSheet sheet = wb.createSheet();
        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
        {
            HSSFClientAnchor a1 = new HSSFClientAnchor();
            a1.setAnchor((short) 1, 1, 0, 0, (short) 1, 1, 512, 100);
            HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
            shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
        }
        {
            HSSFClientAnchor a1 = new HSSFClientAnchor();
            a1.setAnchor((short) 1, 1, 512, 0, (short) 1, 1, 1024, 100);
            HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
            shape1.setFlipVertical(true);
            shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
        }
        {
            HSSFClientAnchor a1 = new HSSFClientAnchor();
            a1.setAnchor((short) 2, 2, 0, 0, (short) 2, 2, 512, 100);
            HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
            shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
        }
        {
            HSSFClientAnchor a1 = new HSSFClientAnchor();
            a1.setAnchor((short) 2, 2, 0, 100, (short) 2, 2, 512, 200);
            HSSFSimpleShape shape1 = patriarch.createSimpleShape(a1);
            shape1.setFlipHorizontal(true);
            shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE);
        }
        /*OutputStream stream = new FileOutputStream("/tmp/45312.xls");
            try {
                wb.write(stream);
            } finally {
                stream.close();
            }*/
        checkWorkbookBack(wb);
    } finally {
        wb.close();
    }
}
Also used : HSSFPatriarch(org.apache.poi.hssf.usermodel.HSSFPatriarch) HSSFClientAnchor(org.apache.poi.hssf.usermodel.HSSFClientAnchor) HSSFSimpleShape(org.apache.poi.hssf.usermodel.HSSFSimpleShape) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Aggregations

HSSFPatriarch (org.apache.poi.hssf.usermodel.HSSFPatriarch)21 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)21 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)20 Test (org.junit.Test)16 HSSFClientAnchor (org.apache.poi.hssf.usermodel.HSSFClientAnchor)14 HSSFSimpleShape (org.apache.poi.hssf.usermodel.HSSFSimpleShape)8 HSSFRichTextString (org.apache.poi.hssf.usermodel.HSSFRichTextString)6 EscherAggregate (org.apache.poi.hssf.record.EscherAggregate)5 HSSFShape (org.apache.poi.hssf.usermodel.HSSFShape)3 HSSFTextbox (org.apache.poi.hssf.usermodel.HSSFTextbox)3 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)2 HSSFComment (org.apache.poi.hssf.usermodel.HSSFComment)2 HSSFFont (org.apache.poi.hssf.usermodel.HSSFFont)2 HSSFPicture (org.apache.poi.hssf.usermodel.HSSFPicture)2 HSSFShapeGroup (org.apache.poi.hssf.usermodel.HSSFShapeGroup)2 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 EscherRecord (org.apache.poi.ddf.EscherRecord)1