Hi Jason
Can you give me any hints on how to add an image to an XLSX file?
Many thanks
slidePart.getJaxbElement().getCSld().getSpTree().getSpOrGrpSpOrGraphicFrame().add(pic);
BinaryPartAbstractImage image = BinaryPartAbstractImage.createImagePart(package,worksheetPart,bytes);
BinaryPartAbstractImage image = null;
try
{
image = BinaryPartAbstractImage.createImagePart(pkg, worksheetPart, bytes);
}
catch (Exception e)
{
e.printStackTrace();
}
PartName drawingPartName = new PartName(worksheetPart.getPartName().getName().replace("worksheets/sheet","drawings/drawing"));
Drawing drawing = (Drawing)pkg.getParts().get(drawingPartName);
List<Object> objects = drawing.getJaxbElement().getEGAnchor();
String rid = "rId" + (objects.size() + 1);
CTOneCellAnchor anchor = new CTOneCellAnchor();
//anchor.setEditAs(STEditAs.ONE_CELL);
CTMarker from = new CTMarker();
//CTMarker to = new CTMarker();
from.setCol(column);
from.setRow(row);
from.setColOff(0L);
from.setRowOff(0L);
//to.setCol(16);
//to.setRow(16);
anchor.setFrom(from);
//anchor.setTo(to);
CTPicture pic = new CTPicture();
CTPictureNonVisual nvPicPr = new CTPictureNonVisual();
CTNonVisualDrawingProps nvpr = new CTNonVisualDrawingProps();
nvpr.setId(objects.size() + 2);
String name = image.getPartName().getName();
name = name.substring(name.lastIndexOf("/")+1);
nvpr.setName(name);
nvpr.setDescr(name);
nvPicPr.setCNvPr(nvpr);
CTPictureLocking ctPictureLocking = new CTPictureLocking();
ctPictureLocking.setNoChangeAspect(true);
CTNonVisualPictureProperties nvpp = new CTNonVisualPictureProperties();
nvpp.setPicLocks(ctPictureLocking);
nvPicPr.setCNvPicPr(nvpp);
pic.setNvPicPr(nvPicPr);
CTBlipFillProperties blipProps = new CTBlipFillProperties();
CTStretchInfoProperties props = new CTStretchInfoProperties();
CTRelativeRect rect = new CTRelativeRect();
props.setFillRect(rect);
blipProps.setStretch(props);
CTBlip blip = new CTBlip();
blip.setEmbed(rid);
blip.setCstate(STBlipCompression.PRINT);
blipProps.setBlip(blip);
pic.setBlipFill(blipProps);
CTShapeProperties sppr = new CTShapeProperties();
ImageSize imageSize = image.getImageInfo().getSize();
CTPoint2D off = new CTPoint2D();
off.setX(0);
off.setY(0);
CTPositiveSize2D ext = new CTPositiveSize2D();
ext.setCx(imageSize.getWidthMpt());
ext.setCy(imageSize.getHeightMpt());
CTTransform2D xfrm = new CTTransform2D();
xfrm.setOff(off);
xfrm.setExt(ext);
sppr.setXfrm(xfrm);
CTPresetGeometry2D prstGeom = new CTPresetGeometry2D();
prstGeom.setPrst(STShapeType.RECT);
prstGeom.setAvLst(new CTGeomGuideList());
sppr.setPrstGeom(prstGeom);
pic.setSpPr(sppr);
anchor.setPic(pic);
CTAnchorClientData data = new CTAnchorClientData();
anchor.setClientData(data);
drawing.getJaxbElement().getEGAnchor().add(anchor);
Relationship rel = new Relationship();
rel.setId(rid);
rel.setType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/image");
rel.setTarget("../media/" + name);
drawing.getRelationshipsPart().addRelationship(rel);
RelationshipsPart relPart = drawing.getRelationshipsPart();
pkg.getParts().remove(relPart.getPartName());
pkg.getParts().put(relPart);
pkg.getParts().remove(drawing.getPartName());
pkg.getParts().put(drawing);
mjrobbins wrote:I've found that BinaryPartAbstractImage has a bunch of undocumented methods (since the 2.7 javadoc isn't available on this site). Looking at those I've figured out that BinaryPartAbstractImage.createImagePart gets me the image part, but the method is completely undocumented, gives no clue what it actually does
Users browsing this forum: No registered users and 9 guests