org.pptx4j.convert.out.svginhtml.PictureExporter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of docx4j Show documentation
Show all versions of docx4j Show documentation
docx4j is a library which helps you to work with the Office Open
XML file format as used in docx
documents, pptx presentations, and xlsx spreadsheets.
package org.pptx4j.convert.out.svginhtml;
import javax.xml.bind.JAXBException;
import org.apache.log4j.Logger;
import org.docx4j.XmlUtils;
import org.docx4j.dml.CTBlip;
import org.docx4j.dml.CTPoint2D;
import org.docx4j.dml.CTPositiveSize2D;
import org.docx4j.model.images.AbstractWordXmlPicture;
import org.docx4j.model.images.ConversionImageHandler;
import org.docx4j.openpackaging.packages.PresentationMLPackage;
import org.docx4j.openpackaging.parts.Part;
import org.pptx4j.model.ResolvedLayout;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.traversal.NodeIterator;
public class PictureExporter extends AbstractWordXmlPicture {
/**
*
<-----------------------
*/
protected static Logger log = Logger.getLogger(PictureExporter.class);
public static DocumentFragment createHtmlImg(
PresentationMLPackage pmlPackage,
ResolvedLayout rl,
ConversionImageHandler imageHandler,
NodeIterator wpInline) {
PictureExporter converter = createPicture( pmlPackage,
imageHandler, wpInline, rl );
DocumentFragment df = getHtmlDocumentFragment(converter);
// Now wrap
//
// around it
CTPoint2D offset = converter.pic.getSpPr().getXfrm().getOff();
Dimensions xy = converter.readDimensions( offset.getX(), offset.getY() );
/*
* TODO, USE:
*
* // Geometrical transforms
CTTransform2D xfrm = cxnSp.getSpPr().getXfrm();
Box b = new Box(xfrm.getOff().getX(), xfrm.getOff().getY(),
xfrm.getExt().getCx(), xfrm.getExt().getCy() );
if (xfrm.getRot()!=0) {
b.rotate(xfrm.getRot());
}
if (xfrm.isFlipH() ) {
b.flipH();
}
if (xfrm.isFlipV() ) {
b.flipV();
}
// Convert from EMU to pixels
b.toPixels();
*
*/
Element div = df.getOwnerDocument().createElement("div");
div.setAttribute("style", "position:absolute; left:" + xy.width + "px; top:" + xy.height + "px;");
Node img = df.getFirstChild();
df.removeChild(img);
df.appendChild(div);
div.appendChild(img);
return df;
}
org.pptx4j.pml.Pic pic=null;
public static PictureExporter createPicture(PresentationMLPackage pmlPackage,
ConversionImageHandler imageHandler,
NodeIterator anchorOrInline,
ResolvedLayout rl) {
PictureExporter converter = new PictureExporter();
try {
Node n = anchorOrInline.nextNode();
converter.pic = (org.pptx4j.pml.Pic)XmlUtils.unmarshal(n, org.pptx4j.jaxb.Context.jcPML, org.pptx4j.pml.Pic.class);
} catch (JAXBException e1) {
e1.printStackTrace();
}
log.info("** image: " + converter.pic.getClass().getName() );
if (converter.pic.getBlipFill()==null
|| converter.pic.getBlipFill().getBlip()==null) {
log.error("blip missing!!");
return null;
}
CTBlip blip = converter.pic.getBlipFill().getBlip();
String imgRelId = blip.getEmbed();
if (imgRelId!=null) {
converter.handleImageRel(imageHandler, imgRelId, (Part)rl.relationships.getSourceP());
} else if (blip.getLink()!=null) {
converter.handleImageRel(imageHandler, blip.getLink(), (Part)rl.relationships.getSourceP());
} else {
log.error("not linked or embedded?!");
}
converter.dimensions = converter.readDimensions(
converter.pic.getSpPr().getXfrm().getExt());
// TODO:
return converter;
}
private final int extentToPixelConversionFactor = 9525; //12700;
private Dimensions readDimensions(long x, long y) {
Dimensions dimensions = new Dimensions();
dimensions.width= (int) x / extentToPixelConversionFactor;
dimensions.widthUnit = "px";
dimensions.height= (int) y / extentToPixelConversionFactor;
dimensions.heightUnit = "px";
return dimensions;
}
private Dimensions readDimensions(CTPositiveSize2D size2d) {
/*
*/
if (size2d==null) {
log.warn("wp:inline/wp:extent missing!");
return null;
}
return readDimensions(size2d.getCx(), size2d.getCy() );
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy