我试图使用java以编程方式合并一些pptx文档.我想知道如何使用Apache POI实质上这样做,但我试图合并的文件不起作用.
经过重要的搜索和反复试验,我发现原因是pptx文档没有主题信息(例如,如果我点击powerpoint并检查幻灯片主视图是否为空白).如果我转到设计功能区中的主题并选择"办公室主题"或其他主题,则保存.文件将迷人地合并.否则,我遇到以下错误:
Exception in thread "main" java.lang.IllegalArgumentException: Failed to fetch default style for otherStyle and level=0
at org.apache.poi.xslf.usermodel.XSLFTextParagraph.getDefaultMasterStyle(XSLFTextParagraph.java:1005)
at org.apache.poi.xslf.usermodel.XSLFTextParagraph.fetchParagraphProperty(XSLFTextParagraph.java:1029)
at org.apache.poi.xslf.usermodel.XSLFTextParagraph.isBullet(XSLFTextParagraph.java:654)
at org.apache.poi.xslf.usermodel.XSLFTextParagraph.copy(XSLFTextParagraph.java:1044)
at org.apache.poi.xslf.usermodel.XSLFTextShape.copy(XSLFTextShape.java:631)
at org.apache.poi.xslf.usermodel.XSLFSheet.appendContent(XSLFSheet.java:358)
at com.apsiva.main.Snippet.main(Snippet.java:28)
Run Code Online (Sandbox Code Playgroud)
以下是我运行的代码:
package com.apsiva.main;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xslf.usermodel.SlideLayout;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFSlideLayout;
public class Snippet {
/** Merge the pptx files in the array <decks> to the desired destination
* chosen in <outputPath> */
public static void main(String[] args) {
try {
FileInputStream empty …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Apache POI XSLF库为pptx文件设置背景填充颜色.我的代码看起来像这样:
XSLFSlideMaster defaultMaster = ppt.getSlideMasters().get(0);
XSLFSlideLayout layout = defaultMaster.getLayout(SlideLayout.BLANK);
XSLFBackground background = layout.getBackground();
background.setFillColor(Color.BLACK);
Run Code Online (Sandbox Code Playgroud)
结果
Exception in thread "main" java.lang.IllegalStateException: CTShapeProperties was not found.
at org.apache.poi.xslf.usermodel.XSLFShape.getSpPr(XSLFShape.java:240)
at org.apache.poi.xslf.usermodel.XSLFSimpleShape.setFillColor(XSLFSimpleShape.java:549)
Run Code Online (Sandbox Code Playgroud)
我试过在SlideMaster的背景,布局的背景和幻灯片的背景上调用它,都会导致同样的错误.
到目前为止,我只有一个工作代码,用于从ppt幻灯片笔记中检索文本
try {
FileInputStream is = new FileInputStream("C:\\sample\\test.ppt");
SlideShow ppt = new SlideShow(is);
Slide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
System.out.println(i);
TextRun[] runs = slide[i].getNotesSheet().getTextRuns();
if (runs.length < 1) {
System.out.println("null");
} else {
for (TextRun run : runs) {
System.out.println(" > " + run.getText());
}
}
}
} catch (IOException ioe) {
}
Run Code Online (Sandbox Code Playgroud)
但是如何从pptx幻灯片笔记中检索文本?
当我使用POI XSLF创建PPTX时,我得到一张空白幻灯片:
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
XSLFTextBox shape = slide.createTextBox();
XSLFTextParagraph p = shape.addNewTextParagraph();
XSLFTextRun r1 = p.addNewTextRun();
r1.setText("the");
r1.setFontColor(Color.blue);
r1.setFontSize(24);
OutputStream out = new FileOutputStream("d:/font.pptx");
ppt.write(out);
out.close();
Run Code Online (Sandbox Code Playgroud)
为什么幻灯片没有任何文字?