Reputation: 484
i want to insert image in one of the tablecell of my ppt table using apache *poi*, other cells have text data.
I didn't find any api to write image to tablecell. I tried using draw method of tablecell but got an exception.
File file = new File("E:\PPTGeneratorJars\Search Definition.png");
BufferedImage image = ImageIO.read(file);
Graphics graphics= image.getGraphics();
cell.draw((Graphics2D) graphics);
Exception in thread "main" java.lang.NullPointerException
at org.apache.poi.hslf.usermodel.RichTextRun.getCharTextPropVal(RichTextRun.java:284)
at org.apache.poi.hslf.usermodel.RichTextRun.getFontColor(RichTextRun.java:514)
at org.apache.poi.hslf.model.TextPainter.getAttributedString(TextPainter.java:81)
at org.apache.poi.hslf.model.TextPainter.getTextElements(TextPainter.java:161)
at org.apache.poi.hslf.model.TextPainter.paint(TextPainter.java:98)
at org.apache.poi.hslf.model.TextShape.draw(TextShape.java:562)
Can anyone please help me with this?
Upvotes: 0
Views: 5051
Reputation: 3446
From what I've understood from your question is, that you want to generate a powerpoint slide containing a table element and inside the table one of the cells should contain a image.
When you check the OOXML Schema you see, that it's possible to specify the background fill of a given cell (dml-table.xsd -> tr -> tc -> tcPr -> blipFill).
In the below snippet, a pptx is created from scratch and the blipFill is referencing a gif (jpeg/png/gif/tiff are supported). When you want to change the size of the image, you'll need to know the bounding box of the corresponding cell. The fillRect can then be limited by %-values (e.g. 30000 = 30% padding from left/right), i.e. if you know the cell has width of X (EMs) and your picture is Y (pixels) in width, you need to convert the pixels to EM-units and calculate the padding relatively to the size of the image ... something like (not validated!):
100000d*(cellSize-imageSize)/imageSize
and here is the example programm:
import java.awt.geom.Rectangle2D;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.openxml4j.opc.TargetMode;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFTable;
import org.apache.poi.xslf.usermodel.XSLFTableCell;
import org.apache.poi.xslf.usermodel.XSLFTableRow;
import org.openxmlformats.schemas.drawingml.x2006.main.CTBlip;
import org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTRelativeRect;
public class InsertImgIntoPptxTableCell {
// http://stackoverflow.com/questions/14495288/how-to-add-image-to-powerpoint-ppt-table-cell-using-apache-poi-hslf-xslf
public static void main(String[] args) throws Exception {
XMLSlideShow pptx = new XMLSlideShow();
XSLFSlide slide = pptx.createSlide();
// you need to include ooxml-schemas:1.1 for this to work!!!
// otherwise an empty table will be created
// see https://issues.apache.org/bugzilla/show_bug.cgi?id=49934
XSLFTable table = slide.createTable();
table.setAnchor(new Rectangle2D.Double(50, 50, 500, 20));
XSLFTableRow row = table.addRow();
row.addCell().setText("Cell 1");
XSLFTableCell cell = row.addCell();
cell.setText("Cell 2");
CTBlipFillProperties blipPr = cell.getXmlObject().getTcPr().addNewBlipFill();
blipPr.setDpi(72);
// http://officeopenxml.com/drwPic-ImageData.php
CTBlip blib = blipPr.addNewBlip();
blipPr.addNewSrcRect();
CTRelativeRect fillRect = blipPr.addNewStretch().addNewFillRect();
fillRect.setL(30000);
fillRect.setR(30000);
PackagePartName partName = PackagingURIHelper.createPartName("/ppt/media/100px.gif");
PackagePart part = pptx.getPackage().createPart(partName, "image/gif");
OutputStream partOs = part.getOutputStream();
FileInputStream fis = new FileInputStream("src/test/resources/100px.gif");
byte buf[] = new byte[1024];
for (int readBytes; (readBytes = fis.read(buf)) != -1; partOs.write(buf, 0, readBytes));
fis.close();
partOs.close();
PackageRelationship prs = slide.getPackagePart().addRelationship(partName, TargetMode.INTERNAL, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image");
blib.setEmbed(prs.getId());
FileOutputStream fos = new FileOutputStream("test2.pptx");
pptx.write(fos);
fos.close();
}
}
Upvotes: 3