Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用ApachePOI hslf/xslf将图像添加到PowerPoint ppt表格单元格?_Java_Image_Nullpointerexception_Apache Poi_Powerpoint - Fatal编程技术网

Java 如何使用ApachePOI hslf/xslf将图像添加到PowerPoint ppt表格单元格?

Java 如何使用ApachePOI hslf/xslf将图像添加到PowerPoint ppt表格单元格?,java,image,nullpointerexception,apache-poi,powerpoint,Java,Image,Nullpointerexception,Apache Poi,Powerpoint,我想使用apache*poi*在我的ppt表格的表格单元格中插入图像,其他单元格有文本数据 我没有找到任何api将图像写入tablecell。我尝试使用tablecell的draw方法,但遇到了一个异常 File file = new File("E:\PPTGeneratorJars\Search Definition.png"); BufferedImage image = ImageIO.read(file); Graphics graphics= image.g

我想使用apache*poi*在我的ppt表格的表格单元格中插入图像,其他单元格有文本数据

我没有找到任何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);
例外情况
有人能帮我吗?

从你的问题中我了解到,你想生成一张包含表格元素的powerpoint幻灯片,表格中的一个单元格应该包含图像

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

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

下面是示例程序:

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();
    }
}

为了更快地获得更好的帮助,请发布一个。
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();
    }
}