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

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

Introduction

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

Prototype

public PackagePartName getPartName() 

Source Link

Usage

From source file:AddAudioToPptx.java

License:Apache License

static void addAudio(XMLSlideShow pptx, XSLFSlide slide1, PackagePart videoPart, XSLFPictureShape pic1,
        double seconds) throws IOException {

    // add video shape
    PackagePartName partName = videoPart.getPartName();
    PackageRelationship prsEmbed1 = slide1.getPackagePart().addRelationship(partName, TargetMode.INTERNAL,
            "http://schemas.microsoft.com/office/2007/relationships/media");
    PackageRelationship prsExec1 = slide1.getPackagePart().addRelationship(partName, TargetMode.INTERNAL,
            "http://schemas.openxmlformats.org/officeDocument/2006/relationships/audio");
    CTPicture xpic1 = (CTPicture) pic1.getXmlObject();
    CTHyperlink link1 = xpic1.getNvPicPr().getCNvPr().addNewHlinkClick();
    link1.setId("");
    link1.setAction("ppaction://media");

    // add video relation
    CTApplicationNonVisualDrawingProps nvPr = xpic1.getNvPicPr().getNvPr();
    nvPr.addNewVideoFile().setLink(prsExec1.getId());
    CTExtension ext = nvPr.addNewExtLst().addNewExt();
    // see http://msdn.microsoft.com/en-us/library/dd950140(v=office.12).aspx
    ext.setUri("{DAA4B4D4-6D71-4841-9C94-3DE7FCFB9230}");
    String p14Ns = "http://schemas.microsoft.com/office/powerpoint/2010/main";
    XmlCursor cur = ext.newCursor();//from   w w  w.j  a  v  a2  s.co  m
    cur.toEndToken();
    cur.beginElement(new QName(p14Ns, "media", "p14"));
    cur.insertNamespace("p14", p14Ns);
    cur.insertAttributeWithValue(new QName(STRelationshipId.type.getName().getNamespaceURI(), "embed"),
            prsEmbed1.getId());
    cur.beginElement(new QName(p14Ns, "trim", "p14"));
    cur.insertAttributeWithValue("st", df_time.format(seconds * 1000.0));
    cur.dispose();

}

From source file:AddVideoToPptx.java

License:Apache License

static void addVideo(XMLSlideShow pptx, XSLFSlide slide1, PackagePart videoPart, XSLFPictureShape pic1,
        double seconds) throws IOException {

    // add video shape
    PackagePartName partName = videoPart.getPartName();
    PackageRelationship prsEmbed1 = slide1.getPackagePart().addRelationship(partName, TargetMode.INTERNAL,
            "http://schemas.microsoft.com/office/2007/relationships/media");
    PackageRelationship prsExec1 = slide1.getPackagePart().addRelationship(partName, TargetMode.INTERNAL,
            "http://schemas.openxmlformats.org/officeDocument/2006/relationships/video");
    CTPicture xpic1 = (CTPicture) pic1.getXmlObject();
    CTHyperlink link1 = xpic1.getNvPicPr().getCNvPr().addNewHlinkClick();
    link1.setId("");
    link1.setAction("ppaction://media");

    // add video relation
    CTApplicationNonVisualDrawingProps nvPr = xpic1.getNvPicPr().getNvPr();
    nvPr.addNewVideoFile().setLink(prsExec1.getId());
    CTExtension ext = nvPr.addNewExtLst().addNewExt();
    // see http://msdn.microsoft.com/en-us/library/dd950140(v=office.12).aspx
    ext.setUri("{DAA4B4D4-6D71-4841-9C94-3DE7FCFB9230}");
    String p14Ns = "http://schemas.microsoft.com/office/powerpoint/2010/main";
    XmlCursor cur = ext.newCursor();/*  w  ww. j a  v a  2  s  . co  m*/
    cur.toEndToken();
    cur.beginElement(new QName(p14Ns, "media", "p14"));
    cur.insertNamespace("p14", p14Ns);
    cur.insertAttributeWithValue(new QName(STRelationshipId.type.getName().getNamespaceURI(), "embed"),
            prsEmbed1.getId());
    cur.beginElement(new QName(p14Ns, "trim", "p14"));
    cur.insertAttributeWithValue("st", df_time.format(seconds * 1000.0));
    cur.dispose();

}

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./*from   w  w  w. j ava 2 s  .com*/
 *
 * @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 .  j  a  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/>// w  ww.  j av a2s  .  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:mj.ocraptor.extraction.tika.parser.microsoft.ooxml.AbstractOOXMLExtractor.java

License:Apache License

/**
 * Handles an embedded file in the document
 *///from   w  ww  .ja v a  2 s  .co m
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);
    }
}

From source file:org.apache.tika.parser.microsoft.ooxml.AbstractOOXMLExtractor.java

License:Apache License

private void handleThumbnail(ContentHandler handler) {
    try {/*from   www . j  a v a  2s . c  o  m*/
        OPCPackage opcPackage = extractor.getPackage();
        for (PackageRelationship rel : opcPackage.getRelationshipsByType(PackageRelationshipTypes.THUMBNAIL)) {
            PackagePart tPart = opcPackage.getPart(rel);
            InputStream tStream = tPart.getInputStream();
            Metadata thumbnailMetadata = new Metadata();
            String thumbName = tPart.getPartName().getName();
            thumbnailMetadata.set(Metadata.RESOURCE_NAME_KEY, thumbName);

            AttributesImpl attributes = new AttributesImpl();
            attributes.addAttribute(XHTML, "class", "class", "CDATA", "embedded");
            attributes.addAttribute(XHTML, "id", "id", "CDATA", thumbName);
            handler.startElement(XHTML, "div", "div", attributes);
            handler.endElement(XHTML, "div", "div");

            thumbnailMetadata.set(Metadata.EMBEDDED_RELATIONSHIP_ID, thumbName);
            thumbnailMetadata.set(Metadata.CONTENT_TYPE, tPart.getContentType());
            thumbnailMetadata.set(TikaCoreProperties.TITLE, tPart.getPartName().getName());

            if (embeddedExtractor.shouldParseEmbedded(thumbnailMetadata)) {
                embeddedExtractor.parseEmbedded(TikaInputStream.get(tStream),
                        new EmbeddedContentHandler(handler), thumbnailMetadata, false);
            }

            tStream.close();
        }
    } catch (Exception ex) {

    }
}

From source file:poi.xslf.usermodel.DataExtraction.java

License:Apache License

public static void main(String args[]) throws Exception {

    if (args.length == 0) {
        System.out.println("Input file is required");
        return;/*from ww w.  java 2  s.c  o  m*/
    }

    FileInputStream is = new FileInputStream(args[0]);
    XMLSlideShow ppt = new XMLSlideShow(is);
    is.close();

    // Get the document's embedded files.
    List<PackagePart> embeds = ppt.getAllEmbedds();
    for (PackagePart p : embeds) {
        String type = p.getContentType();
        String name = p.getPartName().getName(); //typically file name

        InputStream pIs = p.getInputStream();
        // make sense of the part data
        pIs.close();

    }

    // Get the document's embedded files.
    List<XSLFPictureData> images = ppt.getAllPictures();
    for (XSLFPictureData data : images) {
        PackagePart p = data.getPackagePart();

        String type = p.getContentType();
        String name = data.getFileName();

        InputStream pIs = p.getInputStream();
        // make sense of the image data
        pIs.close();

    }

    Dimension pageSize = ppt.getPageSize(); // size of the canvas in points
    for (XSLFSlide slide : ppt.getSlides()) {
        for (XSLFShape shape : slide) {
            Rectangle2D anchor = shape.getAnchor(); // position on the canvas
            if (shape instanceof XSLFTextShape) {
                XSLFTextShape txShape = (XSLFTextShape) shape;
                System.out.println(txShape.getText());
            } else if (shape instanceof XSLFPictureShape) {
                XSLFPictureShape pShape = (XSLFPictureShape) shape;
                XSLFPictureData pData = pShape.getPictureData();
                System.out.println(pData.getFileName());
            } else {
                System.out.println("Process me: " + shape.getClass());
            }
        }
    }
}

From source file:poi.xwpf.usermodel.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  .  j  av 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);
                Assert.assertEquals(cell.getNumericCellValue(), NEW_VALUE);
            }
        }
    }
}

From source file:test.unit.be.fedict.eid.applet.service.signer.OOXMLSignatureVerifierTest.java

License:Open Source License

@Test
public void testOPC() throws Exception {
    // setup/*w w  w  . j  av a2 s  . co  m*/
    InputStream inputStream = OOXMLSignatureVerifierTest.class.getResourceAsStream("/hello-world-signed.docx");

    // operate
    OPCPackage opcPackage = OPCPackage.open(inputStream);

    ArrayList<PackagePart> parts = opcPackage.getParts();
    for (PackagePart part : parts) {
        LOG.debug("part name: " + part.getPartName().getName());
        LOG.debug("part content type: " + part.getContentType());
    }

    ArrayList<PackagePart> signatureParts = opcPackage
            .getPartsByContentType("application/vnd.openxmlformats-package.digital-signature-xmlsignature+xml");
    assertFalse(signatureParts.isEmpty());

    PackagePart signaturePart = signatureParts.get(0);
    LOG.debug("signature part class type: " + signaturePart.getClass().getName());

    PackageDigitalSignatureManager packageDigitalSignatureManager = new PackageDigitalSignatureManager();
    // yeah... POI implementation still missing
}