Search in sources :

Example 1 with EmbeddedObjectRefSubRecord

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

the class TestBugs method bug44840.

/**
     * Problems with extracting check boxes from
     *  HSSFObjectData
     */
@Test(expected = FileNotFoundException.class)
public void bug44840() throws Exception {
    HSSFWorkbook wb = openSample("WithCheckBoxes.xls");
    // Take a look at the embedded objects
    List<HSSFObjectData> objects = wb.getAllEmbeddedObjects();
    assertEquals(1, objects.size());
    HSSFObjectData obj = objects.get(0);
    assertNotNull(obj);
    // Peek inside the underlying record
    EmbeddedObjectRefSubRecord rec = obj.findObjectRecord();
    assertNotNull(rec);
    //        assertEquals(32, rec.field_1_stream_id_offset);
    // WRONG!
    assertEquals(0, rec.getStreamId().intValue());
    assertEquals("Forms.CheckBox.1", rec.getOLEClassName());
    assertEquals(12, rec.getObjectData().length);
    // Doesn't have a directory
    assertFalse(obj.hasDirectoryEntry());
    assertNotNull(obj.getObjectData());
    assertEquals(12, obj.getObjectData().length);
    assertEquals("Forms.CheckBox.1", obj.getOLE2ClassName());
    try {
        obj.getDirectory();
    } finally {
        wb.close();
    }
}
Also used : EmbeddedObjectRefSubRecord(org.apache.poi.hssf.record.EmbeddedObjectRefSubRecord) Test(org.junit.Test)

Example 2 with EmbeddedObjectRefSubRecord

use of org.apache.poi.hssf.record.EmbeddedObjectRefSubRecord 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)

Aggregations

EmbeddedObjectRefSubRecord (org.apache.poi.hssf.record.EmbeddedObjectRefSubRecord)2 FileNotFoundException (java.io.FileNotFoundException)1 EscherContainerRecord (org.apache.poi.ddf.EscherContainerRecord)1 EscherSpRecord (org.apache.poi.ddf.EscherSpRecord)1 CommonObjectDataSubRecord (org.apache.poi.hssf.record.CommonObjectDataSubRecord)1 EndSubRecord (org.apache.poi.hssf.record.EndSubRecord)1 FtCfSubRecord (org.apache.poi.hssf.record.FtCfSubRecord)1 FtPioGrbitSubRecord (org.apache.poi.hssf.record.FtPioGrbitSubRecord)1 ObjRecord (org.apache.poi.hssf.record.ObjRecord)1 DirectoryEntry (org.apache.poi.poifs.filesystem.DirectoryEntry)1 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)1 Test (org.junit.Test)1