Search in sources :

Example 81 with PackagePart

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

the class XSLFSheet method commit.

protected final void commit() throws IOException {
    XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
    String docName = getRootElementName();
    if (docName != null) {
        xmlOptions.setSaveSyntheticDocumentElement(new QName("http://schemas.openxmlformats.org/presentationml/2006/main", docName));
    }
    PackagePart part = getPackagePart();
    OutputStream out = part.getOutputStream();
    getXmlObject().save(out, xmlOptions);
    out.close();
}
Also used : QName(javax.xml.namespace.QName) XmlOptions(org.apache.xmlbeans.XmlOptions) OutputStream(java.io.OutputStream) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 82 with PackagePart

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

the class XSLFSheet method importBlip.

/**
     * Import a picture data from another document.
     *
     * @param blipId        ID of the package relationship to retrieve.
     * @param packagePart   package part containing the data to import
     * @return ID of the created relationship
     */
String importBlip(String blipId, PackagePart packagePart) {
    PackageRelationship blipRel = packagePart.getRelationship(blipId);
    PackagePart blipPart;
    try {
        blipPart = packagePart.getRelatedPart(blipRel);
    } catch (InvalidFormatException e) {
        throw new POIXMLException(e);
    }
    XSLFPictureData data = new XSLFPictureData(blipPart);
    XMLSlideShow ppt = getSlideShow();
    XSLFPictureData pictureData = ppt.addPicture(data.getData(), data.getType());
    PackagePart pic = pictureData.getPackagePart();
    RelationPart rp = addRelation(blipId, XSLFRelation.IMAGES, new XSLFPictureData(pic));
    return rp.getRelationship().getId();
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) POIXMLException(org.apache.poi.POIXMLException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Example 83 with PackagePart

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

the class XSLFPictureShape method getPictureData.

/**
     * Return the data on the (internal) picture.
     * For an external linked picture, will return null
     */
public XSLFPictureData getPictureData() {
    if (_data == null) {
        String blipId = getBlipId();
        if (blipId == null)
            return null;
        PackagePart p = getSheet().getPackagePart();
        PackageRelationship rel = p.getRelationship(blipId);
        if (rel != null) {
            try {
                PackagePart imgPart = p.getRelatedPart(rel);
                _data = new XSLFPictureData(imgPart);
            } catch (Exception e) {
                throw new POIXMLException(e);
            }
        }
    }
    return _data;
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) POIXMLException(org.apache.poi.POIXMLException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) XmlException(org.apache.xmlbeans.XmlException) POIXMLException(org.apache.poi.POIXMLException)

Example 84 with PackagePart

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

the class XSLFGraphicFrame method copyDiagram.

// TODO should be moved to a sub-class
private void copyDiagram(CTGraphicalObjectData objData, XSLFGraphicFrame srcShape) {
    String xpath = "declare namespace dgm='http://schemas.openxmlformats.org/drawingml/2006/diagram' $this//dgm:relIds";
    XmlObject[] obj = objData.selectPath(xpath);
    if (obj != null && obj.length == 1) {
        XmlCursor c = obj[0].newCursor();
        XSLFSheet sheet = srcShape.getSheet();
        try {
            String dm = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "dm"));
            PackageRelationship dmRel = sheet.getPackagePart().getRelationship(dm);
            PackagePart dmPart = sheet.getPackagePart().getRelatedPart(dmRel);
            getSheet().importPart(dmRel, dmPart);
            String lo = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "lo"));
            PackageRelationship loRel = sheet.getPackagePart().getRelationship(lo);
            PackagePart loPart = sheet.getPackagePart().getRelatedPart(loRel);
            getSheet().importPart(loRel, loPart);
            String qs = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "qs"));
            PackageRelationship qsRel = sheet.getPackagePart().getRelationship(qs);
            PackagePart qsPart = sheet.getPackagePart().getRelatedPart(qsRel);
            getSheet().importPart(qsRel, qsPart);
            String cs = c.getAttributeText(new QName("http://schemas.openxmlformats.org/officeDocument/2006/relationships", "cs"));
            PackageRelationship csRel = sheet.getPackagePart().getRelationship(cs);
            PackagePart csPart = sheet.getPackagePart().getRelatedPart(csRel);
            getSheet().importPart(csRel, csPart);
        } catch (InvalidFormatException e) {
            throw new POIXMLException(e);
        }
        c.dispose();
    }
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) QName(javax.xml.namespace.QName) XmlObject(org.apache.xmlbeans.XmlObject) POIXMLException(org.apache.poi.POIXMLException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 85 with PackagePart

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

the class XSLFHyperlink method linkToExternal.

private void linkToExternal(String url) {
    PackagePart thisPP = _sheet.getPackagePart();
    if (_link.isSetId() && !_link.getId().isEmpty()) {
        thisPP.removeRelationship(_link.getId());
    }
    PackageRelationship rel = thisPP.addExternalRelationship(url, XSLFRelation.HYPERLINK.getRelation());
    _link.setId(rel.getId());
    if (_link.isSetAction()) {
        _link.unsetAction();
    }
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) 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