簡體   English   中英

如何使用Apache POI hslf / xslf將圖像添加到PowerPoint ppt表單元格?

[英]How to add image to PowerPoint ppt table cell using apache POI hslf/xslf?

我想使用apache * poi *在我的ppt表的tablecell之一中插入圖像 ,其他單元格具有文本數據。

我沒有找到任何將圖像寫入Tablecell的API。 我嘗試使用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)

誰能幫我這個忙嗎?

根據我對您的問題的理解,您想生成一張包含表格元素的PowerPoint幻燈片,並且表格中的一個單元格應包含一張圖像。

當您查看OOXML Schema時,可以指定給定單元格的背景填充(dml-table.xsd-> tr-> tc-> tcPr-> blipFill)。

在下面的代碼段中,從頭開始創建了一個pptx,blipFill引用了一個gif(支持jpeg / png / gif / tiff)。 當您想要更改圖像的大小時,您需要知道相應單元格的邊界框。 然后可以通過%值來限制fillRect(例如30000 =左右填充30%),即,如果您知道單元格的寬度為X(EM),而圖片的寬度為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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM