@@ -, +, @@ - builds on code from https://bz.apache.org/bugzilla/show_bug.cgi?id=58207 - This functionality is useful when using placeholder anchor information when placing images. // content placeholders LinkedList placeContent = ...; LOOP: { XSLFShape place = placeContent.poll(); Rectangle2D anchor = place.getAnchor(); slide.removeShape(place); XSLFPictureShape shape = slide.createPicture ( slide.getSlideShow().addPicture ( ... ) ); shape.resize(anchor); } isolate the user from the draw.binding.STRectAlignment details --- .../org/apache/poi/sl/usermodel/RectAlign.java | 79 +++++++++++++++ .../poi/xslf/usermodel/XSLFPictureShape.java | 106 ++++++++++++++++++++ 2 files changed, 185 insertions(+), 0 deletions(-) create mode 100755 src/java/org/apache/poi/sl/usermodel/RectAlign.java --- a/src/java/org/apache/poi/sl/usermodel/RectAlign.java +++ a/src/java/org/apache/poi/sl/usermodel/RectAlign.java @@ -0,0 +1,79 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + */ +package org.apache.poi.sl.usermodel; + +/** + * Specifies possible rectangle alignment types. + * + *

+ * corresponds toe enumerations defined by + * org.openxmlformats.schemas.drawingml.x2006.main.STRectAlignment + * and by + * org.apache.poi.sl.draw.binding.STRectAlignment + */ +public enum RectAlign +{ + /** Top-Left rectangle alignment */ + TOP_LEFT("tl"), + + /** Top rectangle alignment */ + TOP("t"), + + /** Top-Right rectangle alignment */ + TOP_RIGHT("tr"), + + /** Left rectangle alignment */ + LEFT("l"), + + /** Center rectangle alignment */ + CENTER("ctr"), + + /** Right rectangle alignment */ + RIGHT("r"), + + /** Bottom-Left rectangle alignment */ + BOTTOM_LEFT("bl"), + + /** Bottom rectangle alignment */ + BOTTOM("b"), + + /** Bottom-Right rectangle alignment */ + BOTTOM_RIGHT("br"); + + /** The corresponding xml enum value */ + private final String _value; + + private RectAlign(String val) + { + _value = val; + } + + + /** + * The string representation, + * which corresponds to the internal XML enum value + */ + @Override public String toString() + { + return _value; + } + +} + +/* ************************************************************************** */ --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java +++ a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFPictureShape.java @@ -22,6 +22,7 @@ package org.apache.poi.xslf.usermodel; import java.awt.Dimension; import java.awt.Insets; import java.awt.Rectangle; +import java.awt.geom.Rectangle2D; import java.net.URI; import javax.xml.namespace.QName; @@ -29,6 +30,7 @@ import org.apache.poi.POIXMLException; import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackageRelationship; import org.apache.poi.sl.usermodel.PictureShape; +import org.apache.poi.sl.usermodel.RectAlign; import org.apache.poi.util.Beta; import org.apache.xmlbeans.XmlCursor; import org.apache.xmlbeans.XmlObject; @@ -101,6 +103,110 @@ public class XSLFPictureShape extends XSLFSimpleShape implements PictureShape { } } + + /** + * Fit picture shape into the target rectangle, maintaining the aspect ratio + * and repositioning within the target rectangle with a centered alignment. + * + * @param target The target rectangle + */ + public void resize(Rectangle2D target) + { + resize(target, RectAlign.CENTER); + } + + + /** + * Fit picture shape into the target rectangle, maintaining the aspect ratio + * and repositioning within the target rectangle based on the specified + * alignment (gravity). + * + * @param target The target rectangle + * @param align + * The alignment within the target rectangle when resizing. + * A null value corresponds to RectAlign.CENTER + */ + public void resize(Rectangle2D target, RectAlign align) + { + Dimension dim = getPictureData().getImageDimension(); + if (dim.width <= 0 || dim.height <= 0) + { + // nothing useful to be done for this case + setAnchor(target); + return; + } + + double w = target.getWidth(); + double h = target.getHeight(); + + // scaling + double sx = w / dim.width; + double sy = h / dim.height; + + // position adjustments + double dx = 0, dy = 0; + + if (sx > sy) + { + // use y-scaling for both, reposition x accordingly + w = sy * dim.width; + dx = target.getWidth() - w; + } + else if (sy > sx) + { + // use x-scaling for both, reposition y accordingly + h = sx * dim.height; + dy = target.getHeight() - h; + } + else + { + // uniform scaling, can use target values directly + setAnchor(target); + return; + } + + // the positioning + double x = target.getX(); + double y = target.getY(); + switch (align) + { + case TOP: // X=balance, Y=ok + x += dx/2; + break; + case TOP_RIGHT: // X=shift, Y=ok + x += dx; + break; + case RIGHT: // X=shift, Y=balance + x += dx; + y += dy/2; + break; + case BOTTOM_RIGHT: // X=shift, Y=shift + x += dx; + y += dy; + break; + case BOTTOM: // X=balance, Y=shift + x += dx/2; + y += dy; + break; + case BOTTOM_LEFT: // X=ok, Y=shift + y += dy; + break; + case LEFT: // X=ok, Y=balance + y += dy/2; + break; + case TOP_LEFT: // X=ok, Y=ok + /* no-op */ + break; + default: // CENTER: X=balance, Y=balance + x += dx/2; + y += dy/2; + break; + } + + setAnchor(new Rectangle2D.Double(x, y, w, h)); + } + + /** * Is this an internal picture (image data included within * the PowerPoint file), or an external linked picture --