我想使用 apache * ppt 表的 tablecell 之一中插入图片 > poi *,其他单元格有文本数据。
我没有找到任何api将图像写入tablecell。我尝试使用tablecell的draw方法,但有例外。
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)
有人可以帮我解决这个问题吗?
答案 0 :(得分:2)
从我对你的问题的理解是,你想要生成一个包含表格元素的powerpoint幻灯片,并且在表格中,其中一个单元格应该包含一个图像。
检查您看到的OOXML Schema时,可以指定给定单元格的背景填充(dml-table.xsd - > tr - > tc - > tcPr - > blipFill)
在下面的片段中,从头开始创建pptx,blipFill引用gif(支持jpeg / png / gif / tiff)。如果要更改图像的大小,则需要知道相应单元格的边界框。然后fillRect可以被%-values限制(例如30000 =从左/右填充30%),即如果你知道单元格的宽度为X(EMs)而你的图片宽度为Y(像素),你需要将像素转换为EM单位并相对于图像的大小计算填充...类似(未经验证!):
100000d*(cellSize-imageSize)/imageSize
以下是示例程序:
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();
}
}