use of org.openxmlformats.schemas.drawingml.x2006.picture.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;
}
use of org.openxmlformats.schemas.drawingml.x2006.picture.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();
}
}
use of org.openxmlformats.schemas.drawingml.x2006.picture.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;
}
use of org.openxmlformats.schemas.drawingml.x2006.picture.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;
}
use of org.openxmlformats.schemas.drawingml.x2006.picture.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();
}
Aggregations