how to add image in table(XSLFTable) cell in power

2019-09-21 21:58发布

问题:

how to add image in table(XSLFTable) cell in powerpoint poi api in java, we are not able to get the CTBlipFillProperties through poi latest jar poi-3.15.jar how to add image in table(XSLFTable) cell in powerpoint poi api in java, we are not able to get the CTBlipFillProperties through poi latest jar poi-3.15.jar

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

回答1:

You were quite close ... the following was tested on the POI trunk (POI 3.16-beta2), but should work on POI 3.15 too ...

import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.FileOutputStream;

import org.apache.poi.POIXMLDocumentPart.RelationPart;
import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFPictureData;
import org.apache.poi.xslf.usermodel.XSLFRelation;
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;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTableCell;

public class TablePics {
    public static void main(String[] args) throws Exception {
        XMLSlideShow pptx = new XMLSlideShow();
        XSLFPictureData pd = pptx.addPicture(new File("wrench.emf"), PictureType.EMF);

        XSLFSlide slide = pptx.createSlide();

        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 = ((CTTableCell)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);

        RelationPart rp = slide.addRelation(null, XSLFRelation.IMAGES, pd);
        blib.setEmbed(rp.getRelationship().getId());

        FileOutputStream fos = new FileOutputStream("test2.pptx");
        pptx.write(fos);
        fos.close();
    }
}