首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用apache /xslf将映像添加到PowerPoint ppt表单元中?

如何使用apache /xslf将映像添加到PowerPoint ppt表单元中?
EN

Stack Overflow用户
提问于 2013-01-24 06:34:51
回答 1查看 4.8K关注 0票数 0

我想使用apache *poi*将插入到my ppt表的tablecell中,其他单元格具有文本数据。

我没有找到任何api把图像写到桌布上。我试着用台球的抽签法,但得到了一个例外。

代码语言:javascript
复制
      File file = new File("E:\PPTGeneratorJars\Search Definition.png");  
  BufferedImage image = ImageIO.read(file); 
  Graphics graphics= image.getGraphics();
  cell.draw((Graphics2D) graphics);

异常

代码语言:javascript
复制
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)

有人能帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-24 00:06:02

据我从您的问题中了解到,您想要生成一个包含表元素的powerpoint幻灯片,而在表中,其中一个单元格应该包含一个图像。

当您检查OOXML模式时,可以指定给定单元格的背景填充(dml-table.xsd -> tr -> tc -> tcPr -> blipFill)。

在下面的片段中,一个pptx是从头创建的,blipFill引用的是一个gif (支持jpeg/png/gif/tiff)。当您想要更改图像的大小时,您需要知道相应单元格的边框。然后,fillRect可以受到%-值的限制(例如,从左/右的填充值为30000 = 30% ),也就是说,如果您知道单元格的宽度为X(像素),并且图片宽度为Y(像素),则需要将像素转换为EM-单位,并根据图像的大小计算填充.类似于(未验证!):

代码语言:javascript
复制
100000d*(cellSize-imageSize)/imageSize

下面是示例程序:

代码语言:javascript
复制
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();
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14495288

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档