Search in sources :

Example 16 with PackagePart

use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.

the class EmbeddedObjects method main.

public static void main(String[] args) throws Exception {
    XSSFWorkbook workbook = new XSSFWorkbook(args[0]);
    for (PackagePart pPart : workbook.getAllEmbedds()) {
        String contentType = pPart.getContentType();
        InputStream is = pPart.getInputStream();
        Closeable document;
        if (contentType.equals("application/vnd.ms-excel")) {
            // Excel Workbook - either binary or OpenXML
            document = new HSSFWorkbook(is);
        } else if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
            // Excel Workbook - OpenXML file format
            document = new XSSFWorkbook(is);
        } else if (contentType.equals("application/msword")) {
            // Word Document - binary (OLE2CDF) file format
            document = new HWPFDocument(is);
        } else if (contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
            // Word Document - OpenXML file format
            document = new XWPFDocument(is);
        } else if (contentType.equals("application/vnd.ms-powerpoint")) {
            // PowerPoint Document - binary file format
            document = new HSLFSlideShow(is);
        } else if (contentType.equals("application/vnd.openxmlformats-officedocument.presentationml.presentation")) {
            // PowerPoint Document - OpenXML file format
            document = new XMLSlideShow(is);
        } else {
            // Any other type of embedded object.
            document = is;
        }
        document.close();
        is.close();
    }
    workbook.close();
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) InputStream(java.io.InputStream) Closeable(java.io.Closeable) XMLSlideShow(org.apache.poi.xslf.usermodel.XMLSlideShow) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 17 with PackagePart

use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.

the class POIXMLProperties method getThumbnailFilename.

/**
     * Returns the name of the Document thumbnail, eg 
     *  <code>thumbnail.jpeg</code>, or <code>null</code> if there
     *  isn't one.
     *
     * @return The thumbnail filename, or null
     */
public String getThumbnailFilename() {
    PackagePart tPart = getThumbnailPart();
    if (tPart == null)
        return null;
    String name = tPart.getPartName().getName();
    return name.substring(name.lastIndexOf('/'));
}
Also used : PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 18 with PackagePart

use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.

the class POIXMLRelation method getContents.

/**
     *  Fetches the InputStream to read the contents, based
     *  of the specified core part, for which we are defined
     *  as a suitable relationship
     *
     *  @since 3.16-beta3
     */
public InputStream getContents(PackagePart corePart) throws IOException, InvalidFormatException {
    PackageRelationshipCollection prc = corePart.getRelationshipsByType(getRelation());
    Iterator<PackageRelationship> it = prc.iterator();
    if (it.hasNext()) {
        PackageRelationship rel = it.next();
        PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
        PackagePart part = corePart.getPackage().getPart(relName);
        return part.getInputStream();
    }
    log.log(POILogger.WARN, "No part " + getDefaultFileName() + " found");
    return null;
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 19 with PackagePart

use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.

the class POIXMLDocumentPart method getPartFromOPCPackage.

/**
     * Retrieves the core document part
     * 
     * @since POI 3.14-Beta1
     */
private static PackagePart getPartFromOPCPackage(OPCPackage pkg, String coreDocumentRel) {
    PackageRelationship coreRel = pkg.getRelationshipsByType(coreDocumentRel).getRelationship(0);
    if (coreRel != null) {
        PackagePart pp = pkg.getPart(coreRel);
        if (pp == null) {
            throw new POIXMLException("OOXML file structure broken/invalid - core document '" + coreRel.getTargetURI() + "' not found.");
        }
        return pp;
    }
    coreRel = pkg.getRelationshipsByType(PackageRelationshipTypes.STRICT_CORE_DOCUMENT).getRelationship(0);
    if (coreRel != null) {
        throw new POIXMLException("Strict OOXML isn't currently supported, please see bug #57699");
    }
    throw new POIXMLException("OOXML file structure broken/invalid - no core document found!");
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 20 with PackagePart

use of org.apache.poi.openxml4j.opc.PackagePart in project poi by apache.

the class POIXMLProperties method setThumbnail.

/**
     * Sets the Thumbnail for the document, replacing any existing one.
     *
     * @param filename The filename for the thumbnail image, eg {@code thumbnail.jpg}
     * @param imageData The inputstream to read the thumbnail image from
     * 
     * @throws IOException if the thumbnail can't be written
     */
public void setThumbnail(String filename, InputStream imageData) throws IOException {
    PackagePart tPart = getThumbnailPart();
    if (tPart == null) {
        // New thumbnail
        pkg.addThumbnail(filename, imageData);
    } else {
        // Change existing
        String newType = ContentTypes.getContentTypeFromFileExtension(filename);
        if (!newType.equals(tPart.getContentType())) {
            throw new IllegalArgumentException("Can't set a Thumbnail of type " + newType + " when existing one is of a different type " + tPart.getContentType());
        }
        StreamHelper.copyStream(imageData, tPart.getOutputStream());
    }
}
Also used : PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Aggregations

PackagePart (org.apache.poi.openxml4j.opc.PackagePart)118 OutputStream (java.io.OutputStream)38 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)27 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)25 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)24 PackageRelationshipCollection (org.apache.poi.openxml4j.opc.PackageRelationshipCollection)23 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)19 QName (javax.xml.namespace.QName)18 IOException (java.io.IOException)17 XmlOptions (org.apache.xmlbeans.XmlOptions)17 InputStream (java.io.InputStream)11 Test (org.junit.Test)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)9 POIXMLException (org.apache.poi.POIXMLException)8 XmlException (org.apache.xmlbeans.XmlException)8 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)7 ArrayList (java.util.ArrayList)6 TikaException (org.apache.tika.exception.TikaException)6 URI (java.net.URI)5 SAXException (org.xml.sax.SAXException)5