Search in sources :

Example 1 with CTPicture

use of org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPicture in project poi by apache.

the class XSSFShapeGroup 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 XSSFWorkbook#getAllPictures()} .
     * @return the newly created picture shape.
     */
public XSSFPicture createPicture(XSSFClientAnchor anchor, int pictureIndex) {
    PackageRelationship rel = getDrawing().addPictureReference(pictureIndex);
    CTPicture ctShape = ctGroup.addNewPic();
    ctShape.set(XSSFPicture.prototype());
    XSSFPicture shape = new XSSFPicture(getDrawing(), ctShape);
    shape.parent = this;
    shape.anchor = anchor;
    shape.setPictureReference(rel);
    return shape;
}
Also used : PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) CTPicture(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPicture)

Example 2 with CTPicture

use of org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPicture in project poi by apache.

the class XSSFDrawing method addShapes.

private void addShapes(XmlCursor cur, List<XSSFShape> lst) {
    try {
        do {
            cur.push();
            if (cur.toFirstChild()) {
                do {
                    XmlObject obj = cur.getObject();
                    XSSFShape shape;
                    if (obj instanceof CTMarker) {
                        // ignore anchor elements
                        continue;
                    } else if (obj instanceof CTPicture) {
                        shape = new XSSFPicture(this, (CTPicture) obj);
                    } else if (obj instanceof CTConnector) {
                        shape = new XSSFConnector(this, (CTConnector) obj);
                    } else if (obj instanceof CTShape) {
                        shape = hasOleLink(obj) ? new XSSFObjectData(this, (CTShape) obj) : new XSSFSimpleShape(this, (CTShape) obj);
                    } else if (obj instanceof CTGraphicalObjectFrame) {
                        shape = new XSSFGraphicFrame(this, (CTGraphicalObjectFrame) obj);
                    } else if (obj instanceof CTGroupShape) {
                        shape = new XSSFShapeGroup(this, (CTGroupShape) obj);
                    } else if (obj instanceof XmlAnyTypeImpl) {
                        LOG.log(POILogger.WARN, "trying to parse AlternateContent, " + "this unlinks the returned Shapes from the underlying xml content, " + "so those shapes can't be used to modify the drawing, " + "i.e. modifications will be ignored!");
                        // XmlAnyTypeImpl is returned for AlternateContent parts, which might contain a CTDrawing
                        cur.push();
                        cur.toFirstChild();
                        XmlCursor cur2 = null;
                        try {
                            // need to parse AlternateContent again, otherwise the child elements aren't typed,
                            // but also XmlAnyTypes
                            CTDrawing alterWS = CTDrawing.Factory.parse(cur.newXMLStreamReader());
                            cur2 = alterWS.newCursor();
                            if (cur2.toFirstChild()) {
                                addShapes(cur2, lst);
                            }
                        } catch (XmlException e) {
                            LOG.log(POILogger.WARN, "unable to parse CTDrawing in alternate content.", e);
                        } finally {
                            if (cur2 != null) {
                                cur2.dispose();
                            }
                            cur.pop();
                        }
                        continue;
                    } else {
                        // ignore anything else
                        continue;
                    }
                    assert (shape != null);
                    shape.anchor = getAnchorFromParent(obj);
                    lst.add(shape);
                } while (cur.toNextSibling());
            }
            cur.pop();
        } while (cur.toNextSibling());
    } finally {
        cur.dispose();
    }
}
Also used : CTMarker(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker) CTConnector(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTConnector) CTGraphicalObjectFrame(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTGraphicalObjectFrame) CTDrawing(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTDrawing) CTShape(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTShape) CTGroupShape(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTGroupShape) XmlCursor(org.apache.xmlbeans.XmlCursor) XmlException(org.apache.xmlbeans.XmlException) CTPicture(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPicture) XmlAnyTypeImpl(org.apache.xmlbeans.impl.values.XmlAnyTypeImpl) XmlObject(org.apache.xmlbeans.XmlObject)

Example 3 with CTPicture

use of org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPicture in project poi by apache.

the class XSSFPicture method prototype.

/**
     * Returns a prototype that is used to construct new shapes
     *
     * @return a prototype that is used to construct new shapes
     */
protected static CTPicture prototype() {
    if (prototype == null) {
        CTPicture pic = CTPicture.Factory.newInstance();
        CTPictureNonVisual nvpr = pic.addNewNvPicPr();
        CTNonVisualDrawingProps nvProps = nvpr.addNewCNvPr();
        nvProps.setId(1);
        nvProps.setName("Picture 1");
        nvProps.setDescr("Picture");
        CTNonVisualPictureProperties nvPicProps = nvpr.addNewCNvPicPr();
        nvPicProps.addNewPicLocks().setNoChangeAspect(true);
        CTBlipFillProperties blip = pic.addNewBlipFill();
        blip.addNewBlip().setEmbed("");
        blip.addNewStretch().addNewFillRect();
        CTShapeProperties sppr = pic.addNewSpPr();
        CTTransform2D t2d = sppr.addNewXfrm();
        CTPositiveSize2D ext = t2d.addNewExt();
        //should be original picture width and height expressed in EMUs
        ext.setCx(0);
        ext.setCy(0);
        CTPoint2D off = t2d.addNewOff();
        off.setX(0);
        off.setY(0);
        CTPresetGeometry2D prstGeom = sppr.addNewPrstGeom();
        prstGeom.setPrst(STShapeType.RECT);
        prstGeom.addNewAvLst();
        prototype = pic;
    }
    return prototype;
}
Also used : CTNonVisualPictureProperties(org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualPictureProperties) CTTransform2D(org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D) CTPoint2D(org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D) CTBlipFillProperties(org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties) CTShapeProperties(org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties) CTPresetGeometry2D(org.openxmlformats.schemas.drawingml.x2006.main.CTPresetGeometry2D) CTNonVisualDrawingProps(org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps) CTPicture(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPicture) CTPictureNonVisual(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPictureNonVisual) CTPositiveSize2D(org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D)

Example 4 with CTPicture

use of org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPicture in project poi by apache.

the class XWPFRun method getCTPictures.

private List<CTPicture> getCTPictures(XmlObject o) {
    List<CTPicture> pics = new ArrayList<CTPicture>();
    XmlObject[] picts = o.selectPath("declare namespace pic='" + CTPicture.type.getName().getNamespaceURI() + "' .//pic:pic");
    for (XmlObject pict : picts) {
        if (pict instanceof XmlAnyTypeImpl) {
            // Pesky XmlBeans bug - see Bugzilla #49934
            try {
                pict = CTPicture.Factory.parse(pict.toString(), DEFAULT_XML_OPTIONS);
            } catch (XmlException e) {
                throw new POIXMLException(e);
            }
        }
        if (pict instanceof CTPicture) {
            pics.add((CTPicture) pict);
        }
    }
    return pics;
}
Also used : XmlException(org.apache.xmlbeans.XmlException) ArrayList(java.util.ArrayList) CTPicture(org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture) XmlAnyTypeImpl(org.apache.xmlbeans.impl.values.XmlAnyTypeImpl) XmlObject(org.apache.xmlbeans.XmlObject) POIXMLException(org.apache.poi.POIXMLException)

Example 5 with CTPicture

use of org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPicture in project poi by apache.

the class TestXWPFParagraph method testPictures.

@Test
public void testPictures() throws IOException {
    XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("VariousPictures.docx");
    assertEquals(7, doc.getParagraphs().size());
    XWPFParagraph p;
    XWPFRun r;
    // Text paragraphs
    assertEquals("Sheet with various pictures", doc.getParagraphs().get(0).getText());
    assertEquals("(jpeg, png, wmf, emf and pict) ", doc.getParagraphs().get(1).getText());
    // Spacer ones
    assertEquals("", doc.getParagraphs().get(2).getText());
    assertEquals("", doc.getParagraphs().get(3).getText());
    assertEquals("", doc.getParagraphs().get(4).getText());
    // Image one
    p = doc.getParagraphs().get(5);
    assertEquals(6, p.getRuns().size());
    r = p.getRuns().get(0);
    assertEquals("", r.toString());
    assertEquals(1, r.getEmbeddedPictures().size());
    assertNotNull(r.getEmbeddedPictures().get(0).getPictureData());
    assertEquals("image1.wmf", r.getEmbeddedPictures().get(0).getPictureData().getFileName());
    r = p.getRuns().get(1);
    assertEquals("", r.toString());
    assertEquals(1, r.getEmbeddedPictures().size());
    assertNotNull(r.getEmbeddedPictures().get(0).getPictureData());
    assertEquals("image2.png", r.getEmbeddedPictures().get(0).getPictureData().getFileName());
    r = p.getRuns().get(2);
    assertEquals("", r.toString());
    assertEquals(1, r.getEmbeddedPictures().size());
    assertNotNull(r.getEmbeddedPictures().get(0).getPictureData());
    assertEquals("image3.emf", r.getEmbeddedPictures().get(0).getPictureData().getFileName());
    r = p.getRuns().get(3);
    assertEquals("", r.toString());
    assertEquals(1, r.getEmbeddedPictures().size());
    assertNotNull(r.getEmbeddedPictures().get(0).getPictureData());
    assertEquals("image4.emf", r.getEmbeddedPictures().get(0).getPictureData().getFileName());
    r = p.getRuns().get(4);
    assertEquals("", r.toString());
    assertEquals(1, r.getEmbeddedPictures().size());
    assertNotNull(r.getEmbeddedPictures().get(0).getPictureData());
    assertEquals("image5.jpeg", r.getEmbeddedPictures().get(0).getPictureData().getFileName());
    r = p.getRuns().get(5);
    assertEquals(" ", r.toString());
    assertEquals(0, r.getEmbeddedPictures().size());
    // Final spacer
    assertEquals("", doc.getParagraphs().get(6).getText());
    // Look in detail at one
    r = p.getRuns().get(4);
    XWPFPicture pict = r.getEmbeddedPictures().get(0);
    CTPicture picture = pict.getCTPicture();
    assertEquals("rId8", picture.getBlipFill().getBlip().getEmbed());
    // Ensure that the ooxml compiler finds everything we need
    r.getCTR().getDrawingArray(0);
    r.getCTR().getDrawingArray(0).getInlineArray(0);
    r.getCTR().getDrawingArray(0).getInlineArray(0).getGraphic();
    r.getCTR().getDrawingArray(0).getInlineArray(0).getGraphic().getGraphicData();
    PicDocument pd = new PicDocumentImpl(null);
    assertTrue(pd.isNil());
    doc.close();
}
Also used : PicDocument(org.openxmlformats.schemas.drawingml.x2006.picture.PicDocument) CTPicture(org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture) PicDocumentImpl(org.openxmlformats.schemas.drawingml.x2006.picture.impl.PicDocumentImpl) Test(org.junit.Test)

Aggregations

CTBlipFillProperties (org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties)5 XmlException (org.apache.xmlbeans.XmlException)4 XmlObject (org.apache.xmlbeans.XmlObject)4 CTBlip (org.openxmlformats.schemas.drawingml.x2006.main.CTBlip)4 CTPicture (org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture)4 CTPicture (org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTPicture)4 CTNonVisualDrawingProps (org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps)3 CTPresetGeometry2D (org.openxmlformats.schemas.drawingml.x2006.main.CTPresetGeometry2D)3 CTShapeProperties (org.openxmlformats.schemas.drawingml.x2006.main.CTShapeProperties)3 CTPicture (org.openxmlformats.schemas.presentationml.x2006.main.CTPicture)3 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)2 XmlCursor (org.apache.xmlbeans.XmlCursor)2 XmlAnyTypeImpl (org.apache.xmlbeans.impl.values.XmlAnyTypeImpl)2 Test (org.junit.Test)2 CTNonVisualPictureProperties (org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualPictureProperties)2 CTPoint2D (org.openxmlformats.schemas.drawingml.x2006.main.CTPoint2D)2 CTPositiveSize2D (org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D)2 CTTransform2D (org.openxmlformats.schemas.drawingml.x2006.main.CTTransform2D)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 StringReader (java.io.StringReader)1