Search in sources :

Example 41 with PackageRelationship

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

the class POIXMLDocument method getRelatedByType.

/**
     * Retrieves all the PackageParts which are defined as relationships of the base document with the
     * specified content type.
     * 
     * @param contentType the content type
     * 
     * @return all the base document PackageParts which match the content type
     * 
     * @throws InvalidFormatException when the relationships or the parts contain errors
     * 
     * @see org.apache.poi.xssf.usermodel.XSSFRelation
     * @see org.apache.poi.xslf.usermodel.XSLFRelation
     * @see org.apache.poi.xwpf.usermodel.XWPFRelation
     * @see org.apache.poi.xdgf.usermodel.XDGFRelation
     */
protected PackagePart[] getRelatedByType(String contentType) throws InvalidFormatException {
    PackageRelationshipCollection partsC = getPackagePart().getRelationshipsByType(contentType);
    PackagePart[] parts = new PackagePart[partsC.size()];
    int count = 0;
    for (PackageRelationship rel : partsC) {
        parts[count] = getPackagePart().getRelatedPart(rel);
        count++;
    }
    return parts;
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) PackagePart(org.apache.poi.openxml4j.opc.PackagePart)

Example 42 with PackageRelationship

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

the class TestContentTypeManager method testContentType.

/**
     * Test the properties part content parsing.
     */
@Test
public void testContentType() throws Exception {
    String filepath = OpenXML4JTestDataSamples.getSampleFileName("sample.docx");
    // Retrieves core properties part
    OPCPackage p = OPCPackage.open(filepath, PackageAccess.READ);
    try {
        PackageRelationshipCollection rels = p.getRelationshipsByType(PackageRelationshipTypes.CORE_PROPERTIES);
        PackageRelationship corePropertiesRelationship = rels.getRelationship(0);
        PackagePart coreDocument = p.getPart(corePropertiesRelationship);
        assertEquals("application/vnd.openxmlformats-package.core-properties+xml", coreDocument.getContentType());
        // TODO - finish writing this test
        assumeTrue("finish writing this test", false);
        ContentTypeManager ctm = new ZipContentTypeManager(coreDocument.getInputStream(), p);
        assertNotNull(ctm);
    } finally {
        p.close();
    }
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) PackageRelationshipCollection(org.apache.poi.openxml4j.opc.PackageRelationshipCollection) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) Test(org.junit.Test)

Example 43 with PackageRelationship

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

the class XSSFDrawing method createObjectData.

@Override
public XSSFObjectData createObjectData(ClientAnchor anchor, int storageId, int pictureIndex) {
    XSSFSheet sh = getSheet();
    PackagePart sheetPart = sh.getPackagePart();
    /*
         * The shape id of the ole object seems to be a legacy shape id.
         * 
         * see 5.3.2.1 legacyDrawing (Legacy Drawing Object):
         * Legacy Shape ID that is unique throughout the entire document.
         * Legacy shape IDs should be assigned based on which portion of the document the
         * drawing resides on. The assignment of these ids is broken down into clusters of
         * 1024 values. The first cluster is 1-1024, the second 1025-2048 and so on.
         *
         * Ole shapes seem to start with 1025 on the first sheet ...
         * and not sure, if the ids need to be reindexed when sheets are removed
         * or more than 1024 shapes are on a given sheet (see #51332 for a similar issue)
         */
    XSSFSheet sheet = getSheet();
    XSSFWorkbook wb = sheet.getWorkbook();
    int sheetIndex = wb.getSheetIndex(sheet);
    long shapeId = (sheetIndex + 1) * 1024 + newShapeId();
    // add reference to OLE part
    PackagePartName olePN;
    try {
        olePN = PackagingURIHelper.createPartName("/xl/embeddings/oleObject" + storageId + ".bin");
    } catch (InvalidFormatException e) {
        throw new POIXMLException(e);
    }
    PackageRelationship olePR = sheetPart.addRelationship(olePN, TargetMode.INTERNAL, POIXMLDocument.OLE_OBJECT_REL_TYPE);
    // add reference to image part
    XSSFPictureData imgPD = sh.getWorkbook().getAllPictures().get(pictureIndex);
    PackagePartName imgPN = imgPD.getPackagePart().getPartName();
    PackageRelationship imgSheetPR = sheetPart.addRelationship(imgPN, TargetMode.INTERNAL, PackageRelationshipTypes.IMAGE_PART);
    PackageRelationship imgDrawPR = getPackagePart().addRelationship(imgPN, TargetMode.INTERNAL, PackageRelationshipTypes.IMAGE_PART);
    // add OLE part metadata to sheet
    CTWorksheet cwb = sh.getCTWorksheet();
    CTOleObjects oo = cwb.isSetOleObjects() ? cwb.getOleObjects() : cwb.addNewOleObjects();
    CTOleObject ole1 = oo.addNewOleObject();
    ole1.setProgId("Package");
    ole1.setShapeId(shapeId);
    ole1.setId(olePR.getId());
    XmlCursor cur1 = ole1.newCursor();
    cur1.toEndToken();
    cur1.beginElement("objectPr", XSSFRelation.NS_SPREADSHEETML);
    cur1.insertAttributeWithValue("id", PackageRelationshipTypes.CORE_PROPERTIES_ECMA376_NS, imgSheetPR.getId());
    cur1.insertAttributeWithValue("defaultSize", "0");
    cur1.beginElement("anchor", XSSFRelation.NS_SPREADSHEETML);
    cur1.insertAttributeWithValue("moveWithCells", "1");
    CTTwoCellAnchor ctAnchor = createTwoCellAnchor((XSSFClientAnchor) anchor);
    XmlCursor cur2 = ctAnchor.newCursor();
    cur2.copyXmlContents(cur1);
    cur2.dispose();
    cur1.toParent();
    cur1.toFirstChild();
    cur1.setName(new QName(XSSFRelation.NS_SPREADSHEETML, "from"));
    cur1.toNextSibling();
    cur1.setName(new QName(XSSFRelation.NS_SPREADSHEETML, "to"));
    cur1.dispose();
    // add a new shape and link OLE & image part
    CTShape ctShape = ctAnchor.addNewSp();
    ctShape.set(XSSFObjectData.prototype());
    ctShape.getSpPr().setXfrm(createXfrm((XSSFClientAnchor) anchor));
    // workaround for not having the vmlDrawing filled
    CTBlipFillProperties blipFill = ctShape.getSpPr().addNewBlipFill();
    blipFill.addNewBlip().setEmbed(imgDrawPR.getId());
    blipFill.addNewStretch().addNewFillRect();
    CTNonVisualDrawingProps cNvPr = ctShape.getNvSpPr().getCNvPr();
    cNvPr.setId(shapeId);
    cNvPr.setName("Object " + shapeId);
    XmlCursor extCur = cNvPr.getExtLst().getExtArray(0).newCursor();
    extCur.toFirstChild();
    extCur.setAttributeText(new QName("spid"), "_x0000_s" + shapeId);
    extCur.dispose();
    XSSFObjectData shape = new XSSFObjectData(this, ctShape);
    shape.anchor = (XSSFClientAnchor) anchor;
    return shape;
}
Also used : PackagePartName(org.apache.poi.openxml4j.opc.PackagePartName) QName(javax.xml.namespace.QName) CTWorksheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet) CTOleObjects(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTOleObjects) CTShape(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTShape) POIXMLException(org.apache.poi.POIXMLException) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) XmlCursor(org.apache.xmlbeans.XmlCursor) PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) CTBlipFillProperties(org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties) CTNonVisualDrawingProps(org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps) CTOleObject(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTOleObject) CTTwoCellAnchor(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTTwoCellAnchor)

Example 44 with PackageRelationship

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

the class XSSFDrawing method createPicture.

/**
     * Creates a picture.
     *
     * @param anchor    the client anchor describes how this picture is attached to the sheet.
     * @param pictureIndex the index of the picture in the workbook collection of pictures,
     *   {@link org.apache.poi.xssf.usermodel.XSSFWorkbook#getAllPictures()} .
     *
     * @return  the newly created picture shape.
     */
public XSSFPicture createPicture(XSSFClientAnchor anchor, int pictureIndex) {
    PackageRelationship rel = addPictureReference(pictureIndex);
    long shapeId = newShapeId();
    CTTwoCellAnchor ctAnchor = createTwoCellAnchor(anchor);
    CTPicture ctShape = ctAnchor.addNewPic();
    ctShape.set(XSSFPicture.prototype());
    ctShape.getNvPicPr().getCNvPr().setId(shapeId);
    XSSFPicture shape = new XSSFPicture(this, ctShape);
    shape.anchor = anchor;
    shape.setPictureReference(rel);
    ctShape.getSpPr().setXfrm(createXfrm(anchor));
    return shape;
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) CTPicture(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPicture) CTTwoCellAnchor(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTTwoCellAnchor)

Example 45 with PackageRelationship

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

the class XSLFShape method selectPaint.

protected static PaintStyle selectPaint(final CTBlipFillProperties blipFill, final PackagePart parentPart) {
    final CTBlip blip = blipFill.getBlip();
    return new TexturePaint() {

        private PackagePart getPart() {
            try {
                String blipId = blip.getEmbed();
                PackageRelationship rel = parentPart.getRelationship(blipId);
                return parentPart.getRelatedPart(rel);
            } catch (InvalidFormatException e) {
                throw new RuntimeException(e);
            }
        }

        public InputStream getImageData() {
            try {
                return getPart().getInputStream();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        public String getContentType() {
            /* TOOD: map content-type */
            return getPart().getContentType();
        }

        public int getAlpha() {
            return (blip.sizeOfAlphaModFixArray() > 0) ? blip.getAlphaModFixArray(0).getAmt() : 100000;
        }
    };
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) TexturePaint(org.apache.poi.sl.usermodel.PaintStyle.TexturePaint) CTBlip(org.openxmlformats.schemas.drawingml.x2006.main.CTBlip) IOException(java.io.IOException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException)

Aggregations

PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)50 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)28 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)21 PackageRelationshipCollection (org.apache.poi.openxml4j.opc.PackageRelationshipCollection)15 PackagePartName (org.apache.poi.openxml4j.opc.PackagePartName)13 IOException (java.io.IOException)11 POIXMLException (org.apache.poi.POIXMLException)8 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)5 TikaException (org.apache.tika.exception.TikaException)5 ArrayList (java.util.ArrayList)4 XmlException (org.apache.xmlbeans.XmlException)4 Test (org.junit.Test)4 InputStream (java.io.InputStream)3 URI (java.net.URI)3 HashMap (java.util.HashMap)3 OpenXML4JException (org.apache.poi.openxml4j.exceptions.OpenXML4JException)3 SAXException (org.xml.sax.SAXException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2