Search in sources :

Example 6 with Ole10Native

use of org.apache.poi.poifs.filesystem.Ole10Native in project poi by apache.

the class TestOLE2Embeding method testReallyEmbedSomething.

@Test
public void testReallyEmbedSomething() throws Exception {
    HSSFWorkbook wb1 = new HSSFWorkbook();
    HSSFSheet sheet = wb1.createSheet();
    HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
    byte[] pictureData = HSSFTestDataSamples.getTestDataFileContent("logoKarmokar4.png");
    byte[] picturePPT = POIDataSamples.getSlideShowInstance().readFile("clock.jpg");
    int imgIdx = wb1.addPicture(pictureData, HSSFWorkbook.PICTURE_TYPE_PNG);
    POIFSFileSystem pptPoifs = getSamplePPT();
    int pptIdx = wb1.addOlePackage(pptPoifs, "Sample-PPT", "sample.ppt", "sample.ppt");
    POIFSFileSystem xlsPoifs = getSampleXLS();
    int imgPPT = wb1.addPicture(picturePPT, HSSFWorkbook.PICTURE_TYPE_JPEG);
    int xlsIdx = wb1.addOlePackage(xlsPoifs, "Sample-XLS", "sample.xls", "sample.xls");
    int txtIdx = wb1.addOlePackage(getSampleTXT(), "Sample-TXT", "sample.txt", "sample.txt");
    int rowoffset = 5;
    int coloffset = 5;
    CreationHelper ch = wb1.getCreationHelper();
    HSSFClientAnchor anchor = (HSSFClientAnchor) ch.createClientAnchor();
    anchor.setAnchor((short) (2 + coloffset), 1 + rowoffset, 0, 0, (short) (3 + coloffset), 5 + rowoffset, 0, 0);
    anchor.setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE);
    patriarch.createObjectData(anchor, pptIdx, imgPPT);
    anchor = (HSSFClientAnchor) ch.createClientAnchor();
    anchor.setAnchor((short) (5 + coloffset), 1 + rowoffset, 0, 0, (short) (6 + coloffset), 5 + rowoffset, 0, 0);
    anchor.setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE);
    patriarch.createObjectData(anchor, xlsIdx, imgIdx);
    anchor = (HSSFClientAnchor) ch.createClientAnchor();
    anchor.setAnchor((short) (3 + coloffset), 10 + rowoffset, 0, 0, (short) (5 + coloffset), 11 + rowoffset, 0, 0);
    anchor.setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE);
    patriarch.createObjectData(anchor, txtIdx, imgIdx);
    anchor = (HSSFClientAnchor) ch.createClientAnchor();
    anchor.setAnchor((short) (1 + coloffset), -2 + rowoffset, 0, 0, (short) (7 + coloffset), 14 + rowoffset, 0, 0);
    anchor.setAnchorType(AnchorType.DONT_MOVE_AND_RESIZE);
    HSSFSimpleShape circle = patriarch.createSimpleShape(anchor);
    circle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL);
    circle.setNoFill(true);
    //        if (false) {
    //	        FileOutputStream fos = new FileOutputStream("embed.xls");
    //	        wb.write(fos);
    //	        fos.close();
    //        }
    HSSFWorkbook wb2 = HSSFTestDataSamples.writeOutAndReadBack(wb1);
    wb1.close();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    HSSFObjectData od = wb2.getAllEmbeddedObjects().get(0);
    Ole10Native ole10 = Ole10Native.createFromEmbeddedOleObject((DirectoryNode) od.getDirectory());
    bos.reset();
    pptPoifs.writeFilesystem(bos);
    assertArrayEquals(ole10.getDataBuffer(), bos.toByteArray());
    od = wb2.getAllEmbeddedObjects().get(1);
    ole10 = Ole10Native.createFromEmbeddedOleObject((DirectoryNode) od.getDirectory());
    bos.reset();
    xlsPoifs.writeFilesystem(bos);
    assertArrayEquals(ole10.getDataBuffer(), bos.toByteArray());
    od = wb2.getAllEmbeddedObjects().get(2);
    ole10 = Ole10Native.createFromEmbeddedOleObject((DirectoryNode) od.getDirectory());
    assertArrayEquals(ole10.getDataBuffer(), getSampleTXT());
    xlsPoifs.close();
    pptPoifs.close();
    wb2.close();
}
Also used : Ole10Native(org.apache.poi.poifs.filesystem.Ole10Native) CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) DirectoryNode(org.apache.poi.poifs.filesystem.DirectoryNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) POIFSFileSystem(org.apache.poi.poifs.filesystem.POIFSFileSystem) Test(org.junit.Test)

Aggregations

Ole10Native (org.apache.poi.poifs.filesystem.Ole10Native)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)4 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 Ole10NativeException (org.apache.poi.poifs.filesystem.Ole10NativeException)3 POIFSFileSystem (org.apache.poi.poifs.filesystem.POIFSFileSystem)3 TikaInputStream (org.apache.tika.io.TikaInputStream)3 POIFSDocumentType (org.apache.tika.parser.microsoft.OfficeParser.POIFSDocumentType)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)2 DirectoryEntry (org.apache.poi.poifs.filesystem.DirectoryEntry)2 DocumentEntry (org.apache.poi.poifs.filesystem.DocumentEntry)2 DocumentInputStream (org.apache.poi.poifs.filesystem.DocumentInputStream)2 Entry (org.apache.poi.poifs.filesystem.Entry)2 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)2 TikaException (org.apache.tika.exception.TikaException)2 Metadata (org.apache.tika.metadata.Metadata)2 SAXException (org.xml.sax.SAXException)2 InputStream (java.io.InputStream)1