2017-01-25 2 views
-1

테이블에 이미지를 추가하는 방법 (java)의 poi api에서 powerpoint poi api의 이미지를 추가하는 방법, poi latest jar poi를 통해 CTBlipFillProperties를 가져올 수 없습니다. poi- 자바 파워 포인트 POI API를 테이블 (XSLFTable) 셀에 이미지를 추가하는 방법을 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(); 
     } 
+0

스택 오버플로에 오신 것을 환영합니다! 훌륭한 답을주기 위해, 아직하지 않았다면 한눈에 알아볼 수 있습니다. [mcve]를 제공 할 수 있다면 유용 할 수도 있습니다. – Mat

+0

POI를 통해 pptx에서 XSLFTextParagraph에 이미지를 추가하는 방법 –

답변

2

포이 최신 항아리 POI - 3.15.jar 통해 CTBlipFillProperties을 얻을 수 없습니다. 다음은 POI 트렁크 (POI 3.16-beta2)에서 테스트되었지만 POI 3.15에서도 작동해야합니다 ...

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(); 
    } 
} 
관련 문제