gpt4 book ai didi

java - org.apache.poi.xslf.usermodel 在文本后面发送图像

转载 作者:行者123 更新时间:2023-12-02 00:59:23 26 4
gpt4 key购买 nike

我正在制作一个应用程序来创建幻灯片放映。我设法在每张幻灯片上放置一个图像,但它覆盖了书面文本。如何将文字放在图片前面?

这是我的代码。


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 类来实现。但它比改变形状树的顺序要直接得多。

示例:

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

关于java - org.apache.poi.xslf.usermodel 在文本后面发送图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60908372/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com