代码是
org.apache.poi
poi
4.1.2
org.apache.poi
poi-ooxml
4.1.2
commons-io
commons-io
2.6
public static void main(String[] args) throws IOException {
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
XSLFTextBox shape = slide.createTextBox();
XSLFTextParagraph p = shape.addNewTextParagraph();
p.setTextAlign(TextParagraph.TextAlign.CENTER);
String x2 = "2.96"; //水平位置
String y2 = "13.91"; //垂直位置
String w2 = "10.56"; //宽度
String h2 = "0.50"; //高度
shape.setAnchor(new Rectangle2D.Double(Double.valueOf(x2) / 3.53 * 100, Double.valueOf(y2) / 3.53 * 100, Double.valueOf(w2) / 3.53 * 100, Double.valueOf(h2) / 3.53 * 100));
p.clearTabStops();
p.setTextAlign(TextParagraph.TextAlign.JUSTIFY_LOW);
XSLFTextRun r1 = p.addNewTextRun();
r1.setCharacterSpacing(0);
r1.setText("The");
r1.setFontColor(Color.blue);
r1.setFontSize(24.0);
XSLFTextRun r2 = p.addNewTextRun();
r2.setText(" quick");
r2.setFontColor(Color.red);
r2.setBold(true);
XSLFTextRun r3 = p.addNewTextRun();
r3.setText(" brown");
r3.setFontSize(12.0);
r3.setItalic(true);
r3.setStrikethrough(true);
XSLFTextRun r4 = p.addNewTextRun();
r4.setText(" fox");
r4.setUnderlined(true);
//2.使用 FileOutputStream 类将更改保存到PPT文档:
String filePath = "D:" + File.separator + "test.pptx";
//使用了File对象打开文件然后读取
File file = new File(filePath);
FileOutputStream out = new FileOutputStream(file);
//3.保存文件
ppt.write(out);
out.close();
System.out.println(filePath);
}
生成的pptx文字前面有换行符,影响了文字位置
试过poi3.9版本生成pptx的文件前面没有换行符
但是poi3.9不支持内嵌字体,需求要做内嵌字体
求大佬带带我