Example usage for org.apache.poi.openxml4j.opc PackagePart getInputStream

List of usage examples for org.apache.poi.openxml4j.opc PackagePart getInputStream

Introduction

In this page you can find the example usage for org.apache.poi.openxml4j.opc PackagePart getInputStream.

Prototype

public InputStream getInputStream() throws IOException 

Source Link

Document

Get the input stream of this part to read its content.

Usage

From source file:AddAudioToPptx.java

License:Apache License

static XSLFPictureShape addPreview(XMLSlideShow pptx, XSLFSlide slide1, PackagePart videoPart, double seconds,
        int x, int y) throws IOException {
    // get preview after 5 sec.
    IContainer ic = IContainer.make();//from www.j  a v a2s  .  com
    InputOutputStreamHandler iosh = new InputOutputStreamHandler(videoPart.getInputStream());
    if (ic.open(iosh, IContainer.Type.READ, null) < 0)
        return null;

    IMediaReader mediaReader = ToolFactory.makeReader(ic);

    // stipulate that we want BufferedImages created in BGR 24bit color space
    mediaReader.setBufferedImageTypeToGenerate(BufferedImage.TYPE_3BYTE_BGR);

    ImageSnapListener isl = new ImageSnapListener(seconds);
    mediaReader.addListener(isl);

    // read out the contents of the media file and
    // dispatch events to the attached listener
    while (!isl.hasFired && mediaReader.readPacket() == null)
        ;

    mediaReader.close();
    ic.close();

    // add snapshot
    BufferedImage image1 = isl.image;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ImageIO.write(image1, "jpeg", bos);
    XSLFPictureData snap = pptx.addPicture(bos.toByteArray(), PictureType.JPEG);
    XSLFPictureShape pic1 = slide1.createPicture(snap);
    pic1.setAnchor(new Rectangle(x, y, image1.getWidth(), image1.getHeight()));
    return pic1;
}

From source file:com.gezipu360.cashier.bean.word.UpdateEmbeddedDoc.java

License:Apache License

/**
 * Called to update the embedded Excel workbook. As the format and structire
 * of the workbook are known in advance, all this code attempts to do is
 * write a new value into the first cell on the first row of the first
 * worksheet. Prior to executing this method, that cell will contain the
 * value 1.//  ww w . jav  a 2 s  .  c  o  m
 *
 * @throws org.apache.poi.openxml4j.exceptions.OpenXML4JException
 *                             Rather
 *                             than use the specific classes (HSSF/XSSF) to handle the embedded
 *                             workbook this method uses those defeined in the SS stream. As
 *                             a result, it might be the case that a SpreadsheetML file is
 *                             opened for processing, throwing this exception if that file is
 *                             invalid.
 * @throws java.io.IOException Thrown if a problem occurs in the underlying
 *                             file system.
 */
public void updateEmbeddedDoc() throws OpenXML4JException, IOException {
    Workbook workbook = null;
    Sheet sheet = null;
    Row row = null;
    Cell cell = null;
    PackagePart pPart = null;
    Iterator<PackagePart> pIter = null;
    List<PackagePart> embeddedDocs = this.doc.getAllEmbedds();
    if (embeddedDocs != null && !embeddedDocs.isEmpty()) {
        pIter = embeddedDocs.iterator();
        while (pIter.hasNext()) {
            pPart = pIter.next();
            if (pPart.getPartName().getExtension().equals(BINARY_EXTENSION)
                    || pPart.getPartName().getExtension().equals(OPENXML_EXTENSION)) {

                // Get an InputStream from the pacage part and pass that
                // to the create method of the WorkbookFactory class. Update
                // the resulting Workbook and then stream that out again
                // using an OutputStream obtained from the same PackagePart.
                workbook = WorkbookFactory.create(pPart.getInputStream());
                sheet = workbook.getSheetAt(SHEET_NUM);
                row = sheet.getRow(ROW_NUM);
                cell = row.getCell(CELL_NUM);
                cell.setCellValue(NEW_VALUE);
                workbook.write(pPart.getOutputStream());
            }
        }

        // Finally, write the newly modified Word document out to file.
        this.doc.write(new FileOutputStream(this.docFile));
    }
}

From source file:com.gezipu360.cashier.bean.word.UpdateEmbeddedDoc.java

License:Apache License

/**
 * Called to test whether or not the embedded workbook was correctly
 * updated. This method simply recovers the first cell from the first row
 * of the first workbook and tests the value it contains.
 * <p/>//from ww w.  ja  v  a 2  s. com
 * Note that execution will not continue up to the assertion as the
 * embedded workbook is now corrupted and causes an IllegalArgumentException
 * with the following message
 * <p/>
 * <em>java.lang.IllegalArgumentException: Your InputStream was neither an
 * OLE2 stream, nor an OOXML stream</em>
 * <p/>
 * to be thrown when the WorkbookFactory.createWorkbook(InputStream) method
 * is executed.
 *
 * @throws org.apache.poi.openxml4j.exceptions.OpenXML4JException
 *                             Rather
 *                             than use the specific classes (HSSF/XSSF) to handle the embedded
 *                             workbook this method uses those defeined in the SS stream. As
 *                             a result, it might be the case that a SpreadsheetML file is
 *                             opened for processing, throwing this exception if that file is
 *                             invalid.
 * @throws java.io.IOException Thrown if a problem occurs in the underlying
 *                             file system.
 */
public void checkUpdatedDoc() throws OpenXML4JException, IOException {
    Workbook workbook = null;
    Sheet sheet = null;
    Row row = null;
    Cell cell = null;
    PackagePart pPart = null;
    Iterator<PackagePart> pIter = null;
    List<PackagePart> embeddedDocs = this.doc.getAllEmbedds();
    if (embeddedDocs != null && !embeddedDocs.isEmpty()) {
        pIter = embeddedDocs.iterator();
        while (pIter.hasNext()) {
            pPart = pIter.next();
            if (pPart.getPartName().getExtension().equals(BINARY_EXTENSION)
                    || pPart.getPartName().getExtension().equals(OPENXML_EXTENSION)) {
                workbook = WorkbookFactory.create(pPart.getInputStream());
                sheet = workbook.getSheetAt(SHEET_NUM);
                row = sheet.getRow(ROW_NUM);
                cell = row.getCell(CELL_NUM);
                //assertEquals(cell.getNumericCellValue(), NEW_VALUE, 0.0001);
            }
        }
    }
}

From source file:com.glodon.tika.UpdateEmbeddedDoc.java

License:Apache License

/**
 * Called to test whether or not the embedded workbook was correctly
 * updated. This method simply recovers the first cell from the first row
 * of the first workbook and tests the value it contains.
 * <p/>//from   ww  w . jav  a 2  s  . c  o m
 * Note that execution will not continue up to the assertion as the
 * embedded workbook is now corrupted and causes an IllegalArgumentException
 * with the following message
 * <p/>
 * <em>java.lang.IllegalArgumentException: Your InputStream was neither an
 * OLE2 stream, nor an OOXML stream</em>
 * <p/>
 * to be thrown when the WorkbookFactory.createWorkbook(InputStream) method
 * is executed.
 *
 * @throws org.apache.poi.openxml4j.exceptions.OpenXML4JException
 *                             Rather
 *                             than use the specific classes (HSSF/XSSF) to handle the embedded
 *                             workbook this method uses those defeined in the SS stream. As
 *                             a result, it might be the case that a SpreadsheetML file is
 *                             opened for processing, throwing this exception if that file is
 *                             invalid.
 * @throws java.io.IOException Thrown if a problem occurs in the underlying
 *                             file system.
 */
public void checkUpdatedDoc() throws OpenXML4JException, IOException {
    Workbook workbook = null;
    Sheet sheet = null;
    Row row = null;
    Cell cell = null;
    PackagePart pPart = null;
    Iterator<PackagePart> pIter = null;
    List<PackagePart> embeddedDocs = this.doc.getAllEmbedds();
    if (embeddedDocs != null && !embeddedDocs.isEmpty()) {
        pIter = embeddedDocs.iterator();
        while (pIter.hasNext()) {
            pPart = pIter.next();
            if (pPart.getPartName().getExtension().equals(BINARY_EXTENSION)
                    || pPart.getPartName().getExtension().equals(OPENXML_EXTENSION)) {
                workbook = WorkbookFactory.create(pPart.getInputStream());
                sheet = workbook.getSheetAt(SHEET_NUM);
                row = sheet.getRow(ROW_NUM);
                cell = row.getCell(CELL_NUM);
                assertEquals(cell.getNumericCellValue(), NEW_VALUE, 0.0001);
            }
        }
    }
}

From source file:excelimporter.reader.readers.ReadOnlySharedStringsTable.java

License:Apache License

/**
 * @param pkg//  w  w  w.jav a2s  .c om
 * @throws IOException
 * @throws SAXException
 * @throws ParserConfigurationException
 */
public ReadOnlySharedStringsTable(OPCPackage pkg) throws IOException, SAXException {
    ArrayList<PackagePart> parts = pkg.getPartsByContentType(XSSFRelation.SHARED_STRINGS.getContentType());

    // Some workbooks have no shared strings table.
    if (parts.size() > 0) {
        PackagePart sstPart = parts.get(0);
        readFrom(sstPart.getInputStream());
    }
}

From source file:excelimporter.reader.readers.ReadOnlySharedStringsTable.java

License:Apache License

/**
 * Like POIXMLDocumentPart constructor/*ww w  .  jav  a  2 s . c  o m*/
 *
 * @param part
 * @param rel_ignored
 * @throws IOException
 */
public ReadOnlySharedStringsTable(PackagePart part, @SuppressWarnings("unused") PackageRelationship rel_ignored)
        throws IOException, SAXException {
    readFrom(part.getInputStream());
}

From source file:fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.java

License:Open Source License

/**
 * Returns the {@link XWPFHeader} of the given header reference.
 * /*w ww  .  ja  v  a 2  s .com*/
 * @param headerRef the header reference.
 * @return
 * @throws XmlException
 * @throws IOException
 */
protected XWPFHeader getXWPFHeader(CTHdrFtrRef headerRef) throws XmlException, IOException {
    PackagePart hdrPart = document.getPartById(headerRef.getId());
    List<XWPFHeader> headers = document.getHeaderList();
    for (XWPFHeader header : headers) {
        if (header.getPackagePart().equals(hdrPart)) {
            // header is aleady loaded, return it.
            return header;
        }
    }
    // should never come, but load the header if needed.
    HdrDocument hdrDoc = HdrDocument.Factory.parse(hdrPart.getInputStream());
    CTHdrFtr hdrFtr = hdrDoc.getHdr();
    XWPFHeader hdr = new XWPFHeader(document, hdrFtr);
    return hdr;
}

From source file:fr.opensagres.poi.xwpf.converter.core.XWPFDocumentVisitor.java

License:Open Source License

/**
 * Returns the {@link XWPFFooter} of the given footer reference.
 * //from w w w . ja  v a  2s.c o  m
 * @param footerRef the footer reference.
 * @return
 * @throws XmlException
 * @throws IOException
 */
protected XWPFFooter getXWPFFooter(CTHdrFtrRef footerRef) throws XmlException, IOException {
    PackagePart hdrPart = document.getPartById(footerRef.getId());
    List<XWPFFooter> footers = document.getFooterList();
    for (XWPFFooter footer : footers) {
        if (footer.getPackagePart().equals(hdrPart)) {
            // footer is aleady loaded, return it.
            return footer;
        }
    }
    // should never come, but load the footer if needed.
    FtrDocument hdrDoc = FtrDocument.Factory.parse(hdrPart.getInputStream());
    CTHdrFtr hdrFtr = hdrDoc.getFtr();
    XWPFFooter ftr = new XWPFFooter(document, hdrFtr);
    return ftr;
}

From source file:mj.ocraptor.extraction.tika.parser.microsoft.ooxml.AbstractOOXMLExtractor.java

License:Apache License

/**
 * Handles an embedded OLE object in the document
 */// www  . j av a2s  . c om
private void handleEmbeddedOLE(PackagePart part, ContentHandler handler, String rel)
        throws IOException, SAXException {
    // A POIFSFileSystem needs to be at least 3 blocks big to be valid
    // TODO: TIKA-1118 Upgrade to POI 4.0 then enable this block of code
    //        if (part.getSize() >= 0 && part.getSize() < 512*3) {
    //           // Too small, skip
    //           return;
    //        }

    // Open the POIFS (OLE2) structure and process
    POIFSFileSystem fs = new POIFSFileSystem(part.getInputStream());
    try {
        Metadata metadata = new Metadata();
        TikaInputStream stream = null;
        metadata.set(Metadata.EMBEDDED_RELATIONSHIP_ID, rel);

        DirectoryNode root = fs.getRoot();
        POIFSDocumentType type = POIFSDocumentType.detectType(root);

        if (root.hasEntry("CONTENTS") && root.hasEntry("\u0001Ole") && root.hasEntry("\u0001CompObj")
                && root.hasEntry("\u0003ObjInfo")) {
            // TIKA-704: OLE 2.0 embedded non-Office document?
            stream = TikaInputStream.get(fs.createDocumentInputStream("CONTENTS"));
            if (embeddedExtractor.shouldParseEmbedded(metadata)) {
                embeddedExtractor.parseEmbedded(stream, new EmbeddedContentHandler(handler), metadata, false);
            }
        } else if (POIFSDocumentType.OLE10_NATIVE == type) {
            // TIKA-704: OLE 1.0 embedded document
            Ole10Native ole = Ole10Native.createFromEmbeddedOleObject(fs);
            metadata.set(Metadata.RESOURCE_NAME_KEY, ole.getLabel());
            byte[] data = ole.getDataBuffer();
            if (data != null) {
                stream = TikaInputStream.get(data);
            }

            if (stream != null && embeddedExtractor.shouldParseEmbedded(metadata)) {
                embeddedExtractor.parseEmbedded(stream, new EmbeddedContentHandler(handler), metadata, false);
            }
        } else {
            handleEmbeddedFile(part, handler, rel);
        }
    } catch (FileNotFoundException e) {
        // There was no CONTENTS entry, so skip this part
    } catch (Ole10NativeException e) {
        // Could not process an OLE 1.0 entry, so skip this part
    }
}

From source file:mj.ocraptor.extraction.tika.parser.microsoft.ooxml.AbstractOOXMLExtractor.java

License:Apache License

/**
 * Handles an embedded file in the document
 *///  w w w .j av  a2  s  . c om
protected void handleEmbeddedFile(PackagePart part, ContentHandler handler, String rel)
        throws SAXException, IOException {
    Metadata metadata = new Metadata();
    metadata.set(Metadata.EMBEDDED_RELATIONSHIP_ID, rel);

    // Get the name
    String name = part.getPartName().getName();
    metadata.set(Metadata.RESOURCE_NAME_KEY, name.substring(name.lastIndexOf('/') + 1));

    // Get the content type
    metadata.set(Metadata.CONTENT_TYPE, part.getContentType());

    // Call the recursing handler
    if (embeddedExtractor.shouldParseEmbedded(metadata)) {
        embeddedExtractor.parseEmbedded(TikaInputStream.get(part.getInputStream()),
                new EmbeddedContentHandler(handler), metadata, false);
    }
}