2011-10-24 07:07:59 -04:00
|
|
|
/*
|
|
|
|
* ====================================================================
|
|
|
|
* 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.xslf.usermodel;
|
|
|
|
|
2011-11-07 04:12:16 -05:00
|
|
|
import java.awt.Graphics2D;
|
2014-10-26 20:00:05 -04:00
|
|
|
import java.awt.Insets;
|
|
|
|
import java.awt.Shape;
|
2011-11-24 11:13:42 -05:00
|
|
|
import java.awt.geom.AffineTransform;
|
2011-10-24 07:07:59 -04:00
|
|
|
import java.awt.geom.Rectangle2D;
|
|
|
|
import java.awt.image.BufferedImage;
|
2011-10-28 10:25:53 -04:00
|
|
|
import java.io.IOException;
|
2011-10-24 07:07:59 -04:00
|
|
|
|
2014-10-26 20:00:05 -04:00
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
|
|
|
import org.apache.poi.openxml4j.opc.PackagePart;
|
|
|
|
import org.apache.poi.util.Beta;
|
|
|
|
|
2011-10-24 07:07:59 -04:00
|
|
|
/**
|
|
|
|
* For now this class renders only images supported by the javax.imageio.ImageIO
|
|
|
|
* framework. Subclasses can override this class to support other formats, for
|
|
|
|
* example, Use Apache batik to render WMF:
|
|
|
|
*
|
|
|
|
* <pre>
|
|
|
|
* <code>
|
|
|
|
* public class MyImageRendener extends XSLFImageRendener{
|
|
|
|
* public boolean drawImage(Graphics2D graphics, XSLFPictureData data, Rectangle2D anchor){
|
|
|
|
* boolean ok = super.drawImage(graphics, data, anchor);
|
|
|
|
* if(!ok){
|
|
|
|
* // see what type of image we are
|
|
|
|
* String contentType = data.getPackagePart().getContentType();
|
|
|
|
* if(contentType.equals("image/wmf")){
|
|
|
|
* // use Apache Batik to handle WMF
|
|
|
|
* // see http://xmlgraphics.apache.org/batik/
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* }
|
|
|
|
* return ok;
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* </code>
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* and then pass this class to your instance of java.awt.Graphics2D:
|
|
|
|
*
|
|
|
|
* <pre>
|
|
|
|
* <code>
|
|
|
|
* graphics.setRenderingHint(XSLFRenderingHint.IMAGE_RENDERER, new MyImageRendener());
|
|
|
|
* </code>
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* @author Yegor Kozlov
|
|
|
|
*/
|
|
|
|
@Beta
|
2011-12-09 11:48:38 -05:00
|
|
|
public class XSLFImageRenderer {
|
2011-10-24 07:07:59 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Render picture data into the supplied graphics
|
|
|
|
*
|
2011-12-09 11:48:38 -05:00
|
|
|
* @return true if the picture data was successfully rendered
|
2011-10-24 07:07:59 -04:00
|
|
|
*/
|
2014-10-26 20:00:05 -04:00
|
|
|
public boolean drawImage(Graphics2D graphics, XSLFPictureData data,
|
|
|
|
Rectangle2D anchor) {
|
|
|
|
return drawImage(graphics, data, anchor, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render picture data into the supplied graphics
|
|
|
|
*
|
|
|
|
* @return true if the picture data was successfully rendered
|
|
|
|
*/
|
|
|
|
public boolean drawImage(Graphics2D graphics, XSLFPictureData data,
|
|
|
|
Rectangle2D anchor, Insets clip) {
|
|
|
|
boolean isClipped = true;
|
|
|
|
if (clip == null) {
|
|
|
|
isClipped = false;
|
|
|
|
clip = new Insets(0,0,0,0);
|
|
|
|
}
|
|
|
|
|
2014-10-27 20:04:10 -04:00
|
|
|
BufferedImage img;
|
|
|
|
try {
|
|
|
|
img = ImageIO.read(data.getPackagePart().getInputStream());
|
|
|
|
} catch (Exception e) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-20 14:10:43 -04:00
|
|
|
|
|
|
|
if(img == null) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-26 20:00:05 -04:00
|
|
|
|
2014-10-27 20:04:10 -04:00
|
|
|
int iw = img.getWidth();
|
|
|
|
int ih = img.getHeight();
|
2011-12-04 06:16:57 -05:00
|
|
|
|
2014-10-27 20:04:10 -04:00
|
|
|
double cw = (100000-clip.left-clip.right) / 100000.0;
|
|
|
|
double ch = (100000-clip.top-clip.bottom) / 100000.0;
|
|
|
|
double sx = anchor.getWidth()/(iw*cw);
|
|
|
|
double sy = anchor.getHeight()/(ih*ch);
|
|
|
|
double tx = anchor.getX()-(iw*sx*clip.left/100000.0);
|
|
|
|
double ty = anchor.getY()-(ih*sy*clip.top/100000.0);
|
|
|
|
AffineTransform at = new AffineTransform(sx, 0, 0, sy, tx, ty) ;
|
2011-12-04 06:16:57 -05:00
|
|
|
|
2014-10-27 20:04:10 -04:00
|
|
|
Shape clipOld = graphics.getClip();
|
|
|
|
if (isClipped) graphics.clip(anchor.getBounds2D());
|
|
|
|
graphics.drawRenderedImage(img, at);
|
|
|
|
graphics.setClip(clipOld);
|
2011-10-24 07:07:59 -04:00
|
|
|
|
2014-10-27 20:04:10 -04:00
|
|
|
return true;
|
2011-10-24 07:07:59 -04:00
|
|
|
}
|
2011-10-28 10:25:53 -04:00
|
|
|
|
|
|
|
/**
|
2011-11-07 04:12:16 -05:00
|
|
|
* Create a buffered image from the supplied package part.
|
|
|
|
* This method is called to create texture paints.
|
2011-10-28 10:25:53 -04:00
|
|
|
*
|
|
|
|
* @return a <code>BufferedImage</code> containing the decoded
|
|
|
|
* contents of the input, or <code>null</code>.
|
|
|
|
*/
|
2011-11-07 04:12:16 -05:00
|
|
|
public BufferedImage readImage(PackagePart packagePart) throws IOException {
|
|
|
|
return ImageIO.read(packagePart.getInputStream());
|
2011-10-28 10:25:53 -04:00
|
|
|
}
|
2011-10-24 07:07:59 -04:00
|
|
|
}
|