Issue
I'm working with ppt tables and need to add an image inside the table cell (or as a background cell image), so what I did was, I read the dimensions of that table cell, change the dimensions of the image accordingly and pasted it on top of that cell, not inside, so I'm just creating an illusion that the image is inside the cell.
But, I don't think that it is the correct way to do it, so anyone can suggest a better way to deal with image situations like this one?
Solution
There are no inline-with-text-images in PowerPoint, which could be placed inside table cells. But a table cell may have an image as background fill.
Using Apache POI this could be set like so:
- get a
XSLFPictureData
usingXMLSlideShow.addPicture
- create a relationship between the slide and this picture data using
XSLFSheet.addRelation
; we need therId
. - add a blip fill having a blip referencing that
rId
to the table cell properties of theXSLFTableCell
and set the strech property of that bilp fill to fill rect.
Complete example:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.sl.usermodel.TableCell.BorderEdge;
import org.apache.poi.sl.usermodel.PictureData;
import java.awt.Rectangle;
import java.awt.Point;
import java.awt.Color;
public class CreatePPTXTableCellImageFill {
static void setAllCellBorders(XSLFTableCell cell, Color color) {
cell.setBorderColor(BorderEdge.top, color);
cell.setBorderColor(BorderEdge.right, color);
cell.setBorderColor(BorderEdge.bottom, color);
cell.setBorderColor(BorderEdge.left, color);
}
static void setImageFill(XSLFTableCell cell, XSLFPictureData pictureData) {
org.apache.poi.ooxml.POIXMLDocumentPart.RelationPart rp = cell.getSheet().addRelation(null, XSLFRelation.IMAGES, pictureData);
String rId = rp.getRelationship().getId();
if (cell.getXmlObject() instanceof org.openxmlformats.schemas.drawingml.x2006.main.CTTableCell) {
org.openxmlformats.schemas.drawingml.x2006.main.CTTableCell ctTableCell = (org.openxmlformats.schemas.drawingml.x2006.main.CTTableCell)cell.getXmlObject();
if (ctTableCell.getTcPr() == null) ctTableCell.addNewTcPr();
if (ctTableCell.getTcPr().isSetBlipFill()) ctTableCell.getTcPr().unsetBlipFill();
ctTableCell.getTcPr().addNewBlipFill();
ctTableCell.getTcPr().getBlipFill().addNewBlip();
ctTableCell.getTcPr().getBlipFill().getBlip().setEmbed(rId);
ctTableCell.getTcPr().getBlipFill().addNewStretch();
ctTableCell.getTcPr().getBlipFill().getStretch().addNewFillRect();
}
}
public static void main(String[] args) throws Exception {
XMLSlideShow slideShow = new XMLSlideShow();
XSLFSlide slide = slideShow.createSlide();
XSLFTable tbl;
XSLFTableRow row;
XSLFTableCell cell;
String[][] data = {
{"R1C1", "R1C2", "R1C3"},
{"R2C1", "R2C2", "R2C3"},
{"R3C1", "R3C2", "R3C3"}
};
tbl = slide.createTable(data.length, data[0].length);
tbl.setAnchor(new Rectangle(new Point(100, 100)));
for (int r = 0; r < data.length; r++) {
tbl.setRowHeight(r, 100d);
String[] dataRow = data[r];
for (int c = 0; c < dataRow.length; c++) {
tbl.setColumnWidth(c, 120d);
String value = dataRow[c];
cell = tbl.getCell(r, c);
setAllCellBorders(cell, Color.BLACK);
cell.setText(value);
}
}
XSLFPictureData pictureData = slideShow.addPicture(new FileInputStream("./index.png"), PictureData.PictureType.PNG);
cell = tbl.getCell(1, 1);
setImageFill(cell, pictureData);
FileOutputStream out = new FileOutputStream("./CreatePPTXTableCellImageFill.pptx");
slideShow.write(out);
out.close();
slideShow.close();
}
}
Result:
Answered By - Axel Richter
Answer Checked By - Pedro (JavaFixing Volunteer)