Search in sources :

Example 11 with CommonObjectDataSubRecord

use of org.apache.poi.hssf.record.CommonObjectDataSubRecord in project poi by apache.

the class HSSFPatriarch method createObjectData.

@Override
public HSSFObjectData createObjectData(ClientAnchor anchor, int storageId, int pictureIndex) {
    ObjRecord obj = new ObjRecord();
    CommonObjectDataSubRecord ftCmo = new CommonObjectDataSubRecord();
    ftCmo.setObjectType(CommonObjectDataSubRecord.OBJECT_TYPE_PICTURE);
    // ftCmo.setObjectId(oleShape.getShapeId()); ... will be set by onCreate(...)
    ftCmo.setLocked(true);
    ftCmo.setPrintable(true);
    ftCmo.setAutofill(true);
    ftCmo.setAutoline(true);
    ftCmo.setReserved1(0);
    ftCmo.setReserved2(0);
    ftCmo.setReserved3(0);
    obj.addSubRecord(ftCmo);
    // FtCf (pictFormat) 
    FtCfSubRecord ftCf = new FtCfSubRecord();
    HSSFPictureData pictData = getSheet().getWorkbook().getAllPictures().get(pictureIndex - 1);
    switch(pictData.getFormat()) {
        case Workbook.PICTURE_TYPE_WMF:
        case Workbook.PICTURE_TYPE_EMF:
            // this needs patch #49658 to be applied to actually work 
            ftCf.setFlags(FtCfSubRecord.METAFILE_BIT);
            break;
        case Workbook.PICTURE_TYPE_DIB:
        case Workbook.PICTURE_TYPE_PNG:
        case Workbook.PICTURE_TYPE_JPEG:
        case Workbook.PICTURE_TYPE_PICT:
            ftCf.setFlags(FtCfSubRecord.BITMAP_BIT);
            break;
        default:
            throw new IllegalStateException("Invalid picture type: " + pictData.getFormat());
    }
    obj.addSubRecord(ftCf);
    // FtPioGrbit (pictFlags)
    FtPioGrbitSubRecord ftPioGrbit = new FtPioGrbitSubRecord();
    ftPioGrbit.setFlagByBit(FtPioGrbitSubRecord.AUTO_PICT_BIT, true);
    obj.addSubRecord(ftPioGrbit);
    EmbeddedObjectRefSubRecord ftPictFmla = new EmbeddedObjectRefSubRecord();
    ftPictFmla.setUnknownFormulaData(new byte[] { 2, 0, 0, 0, 0 });
    ftPictFmla.setOleClassname("Paket");
    ftPictFmla.setStorageId(storageId);
    obj.addSubRecord(ftPictFmla);
    obj.addSubRecord(new EndSubRecord());
    String entryName = "MBD" + HexDump.toHex(storageId);
    DirectoryEntry oleRoot;
    try {
        DirectoryNode dn = _sheet.getWorkbook().getDirectory();
        if (dn == null) {
            throw new FileNotFoundException();
        }
        oleRoot = (DirectoryEntry) dn.getEntry(entryName);
    } catch (FileNotFoundException e) {
        throw new IllegalStateException("trying to add ole shape without actually adding data first - use HSSFWorkbook.addOlePackage first", e);
    }
    // create picture shape, which need to be minimal modified for oleshapes
    HSSFPicture shape = new HSSFPicture(null, (HSSFClientAnchor) anchor);
    shape.setPictureIndex(pictureIndex);
    EscherContainerRecord spContainer = shape.getEscherContainer();
    EscherSpRecord spRecord = spContainer.getChildById(EscherSpRecord.RECORD_ID);
    spRecord.setFlags(spRecord.getFlags() | EscherSpRecord.FLAG_OLESHAPE);
    HSSFObjectData oleShape = new HSSFObjectData(spContainer, obj, oleRoot);
    addShape(oleShape);
    onCreate(oleShape);
    return oleShape;
}
Also used : FtPioGrbitSubRecord(org.apache.poi.hssf.record.FtPioGrbitSubRecord) EmbeddedObjectRefSubRecord(org.apache.poi.hssf.record.EmbeddedObjectRefSubRecord) CommonObjectDataSubRecord(org.apache.poi.hssf.record.CommonObjectDataSubRecord) EndSubRecord(org.apache.poi.hssf.record.EndSubRecord) FileNotFoundException(java.io.FileNotFoundException) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) DirectoryEntry(org.apache.poi.poifs.filesystem.DirectoryEntry) EscherSpRecord(org.apache.poi.ddf.EscherSpRecord) FtCfSubRecord(org.apache.poi.hssf.record.FtCfSubRecord) ObjRecord(org.apache.poi.hssf.record.ObjRecord) EscherContainerRecord(org.apache.poi.ddf.EscherContainerRecord)

Example 12 with CommonObjectDataSubRecord

use of org.apache.poi.hssf.record.CommonObjectDataSubRecord in project poi by apache.

the class TestHSSFComment method shapeId.

@Test
public void shapeId() throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sh = wb.createSheet();
    HSSFPatriarch patriarch = sh.createDrawingPatriarch();
    HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor());
    comment.setShapeId(2024);
    assertEquals(comment.getShapeId(), 2024);
    CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord) comment.getObjRecord().getSubRecords().get(0);
    assertEquals(2024, cod.getObjectId());
    EscherSpRecord spRecord = (EscherSpRecord) comment.getEscherContainer().getChild(0);
    assertEquals(2024, spRecord.getShapeId(), 2024);
    assertEquals(2024, comment.getShapeId(), 2024);
    assertEquals(2024, comment.getNoteRecord().getShapeId());
    wb.close();
}
Also used : CommonObjectDataSubRecord(org.apache.poi.hssf.record.CommonObjectDataSubRecord) EscherSpRecord(org.apache.poi.ddf.EscherSpRecord) Test(org.junit.Test)

Aggregations

CommonObjectDataSubRecord (org.apache.poi.hssf.record.CommonObjectDataSubRecord)12 ObjRecord (org.apache.poi.hssf.record.ObjRecord)6 EscherSpRecord (org.apache.poi.ddf.EscherSpRecord)5 Test (org.junit.Test)5 EndSubRecord (org.apache.poi.hssf.record.EndSubRecord)3 EscherContainerRecord (org.apache.poi.ddf.EscherContainerRecord)2 FileNotFoundException (java.io.FileNotFoundException)1 Map (java.util.Map)1 EscherClientDataRecord (org.apache.poi.ddf.EscherClientDataRecord)1 EscherOptRecord (org.apache.poi.ddf.EscherOptRecord)1 EscherProperty (org.apache.poi.ddf.EscherProperty)1 EscherRecord (org.apache.poi.ddf.EscherRecord)1 InternalSheet (org.apache.poi.hssf.model.InternalSheet)1 InternalWorkbook (org.apache.poi.hssf.model.InternalWorkbook)1 AutoFilterInfoRecord (org.apache.poi.hssf.record.AutoFilterInfoRecord)1 EmbeddedObjectRefSubRecord (org.apache.poi.hssf.record.EmbeddedObjectRefSubRecord)1 FtCblsSubRecord (org.apache.poi.hssf.record.FtCblsSubRecord)1 FtCfSubRecord (org.apache.poi.hssf.record.FtCfSubRecord)1 FtPioGrbitSubRecord (org.apache.poi.hssf.record.FtPioGrbitSubRecord)1 LbsDataSubRecord (org.apache.poi.hssf.record.LbsDataSubRecord)1