簡體   English   中英

org.apache.poi.xslf.usermodel 在文字后面發送圖片

[英]org.apache.poi.xslf.usermodel send image behind the text

我正在制作一個可以創建幻燈片放映的應用程序。 我設法在每張幻燈片上放一張圖片,但它覆蓋了書面文字。 怎么把文字放在圖片前面?

這是我的代碼。


public void createNewSlide() throws FileNotFoundException, IOException{
        XMLSlideShow ppt = new XMLSlideShow();

        XSLFSlideMaster master = ppt.getSlideMasters().get(0);
        XSLFSlideLayout layout = master.getLayout(SlideLayout.TITLE_ONLY);

        //propiedades do slide
        ppt.setPageSize(new Dimension(1280,720));
        XSLFSlide slide = ppt.createSlide(layout);

        //primeiro slide, começando com texto
        XSLFTextShape title = slide.getPlaceholder(0);
        title.clearText();
        XSLFTextParagraph paragraph = title.addNewTextParagraph();
        XSLFTextRun textRun = paragraph.addNewTextRun();
        textRun.setText("Simple Text");
        textRun.setFontColor(Color.decode("#00ff99"));
        textRun.setFontSize(60.);
        title.setAnchor(new Rectangle(100,100,500,500));

        //adicionando imagem ao ppt
        byte[] picData = IOUtils.toByteArray(new FileInputStream("espace.jpg"));
        XSLFPictureData pcData = ppt.addPicture(picData, PictureData.PictureType.JPEG);
        XSLFPictureShape pictureShape = slide.createPicture(pcData);
        pictureShape.setAnchor(new Rectangle(0,0,1280,720));



        FileOutputStream out = new FileOutputStream("AprentacaoTeste.pptx");
        ppt.write(out);
        out.close();
        ppt.close();
    }

這些是我的依賴項

<dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.1</version>
        </dependency>
    </dependencies>

我使用 netbeans 作為 IDE

您插入的圖片是形狀樹中的最后一個形狀。 所以它在形狀樹中的所有其他形狀之前。 它必須是形狀樹中的第一個形狀,在所有其他形狀之后。 但是如果已經添加了形狀,則很難更改形狀樹的順序。 在添加其他形狀之前,占位符已經存在於布局中。

但我認為,您要做的是為幻燈片設置背景圖片。 ooxml-schemas這也只能使用底層ooxml-schemas類。 但這比更改形狀樹的順序要直接得多。

例子:

import java.io.FileOutputStream;
import java.io.FileInputStream;

import org.apache.poi.util.IOUtils;
import org.apache.poi.sl.usermodel.PictureData;
import org.apache.poi.xslf.usermodel.*;

import org.openxmlformats.schemas.presentationml.x2006.main.CTBackgroundProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTRelativeRect;
import org.openxmlformats.schemas.drawingml.x2006.main.CTBlip;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Color;

public class CreatePPTXPictureBehindText {

 static void setBackgroundPicture(XSLFSlide slide, String picturePath, PictureData.PictureType pictureType) throws Exception {
  byte[] picData = IOUtils.toByteArray(new FileInputStream(picturePath));
  XSLFPictureData pcData = slide.getSlideShow().addPicture(picData, pictureType);
  CTBackgroundProperties backgroundProperties = slide.getXmlObject().getCSld().addNewBg().addNewBgPr();
  CTBlipFillProperties blipFillProperties = backgroundProperties.addNewBlipFill();
  CTRelativeRect ctRelativeRect = blipFillProperties.addNewStretch().addNewFillRect();
  String idx = slide.addRelation(null, XSLFRelation.IMAGES, pcData).getRelationship().getId();
  CTBlip blib = blipFillProperties.addNewBlip();
  blib.setEmbed(idx);
 }

 public static void main(String[] args) throws Exception {

  XMLSlideShow ppt = new XMLSlideShow();

  //set page size
  ppt.setPageSize(new Dimension(1280,720));

  //create first slide title layout
  XSLFSlideMaster master = ppt.getSlideMasters().get(0);
  XSLFSlideLayout layout = master.getLayout(SlideLayout.TITLE_ONLY);
  XSLFSlide slide = ppt.createSlide(layout);

  //set title placeholder's text and anchor
  XSLFTextShape title = slide.getPlaceholder(0);
  title.clearText();
  XSLFTextParagraph paragraph = title.addNewTextParagraph();
  XSLFTextRun textRun = paragraph.addNewTextRun();
  textRun.setText("Simple Text");
  textRun.setFontColor(Color.decode("#00ff99"));
  textRun.setFontSize(60.);
  title.setAnchor(new Rectangle(100,100,500,500));

  //set background picture for slide
  setBackgroundPicture(slide, "./espace.jpeg", PictureData.PictureType.JPEG);

  FileOutputStream out = new FileOutputStream("./AprentacaoTeste.pptx");
  ppt.write(out);
  out.close();
  ppt.close();
 }
}

暫無
暫無

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

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