【发布时间】:2021-09-28 15:46:26
【问题描述】:
我有一个使用 Apache POI 的 Java 应用程序,它会生成一个 PPT(PowerPoint 演示文稿),其中包含这些形状上的形状和文本。我希望这些文本自动适应相应的形状。在 PowerPoint 中肯定有一个选项,它工作正常并称为“溢出时收缩文本”
使用 Apache POI,我可以从代码中设置此选项。下面的例子:
private void drawRectWithText(XSLFSlide slide, int x, int y, int width, int height, String text) {
XSLFAutoShape shape1 = slide.createAutoShape();
Rectangle rect1 = new Rectangle(x, y, width, upperRectHeight);
shape1.setAnchor(rect1.getBounds2D());
shape1.setShapeType(ShapeType.RECT);
shape1.setText(text);
shape1.setTextDirection(TextDirection.HORIZONTAL);
shape1.setVerticalAlignment(VerticalAlignment.MIDDLE);
XSLFTextParagraph xslfTextParagraph = shape1.getTextParagraphs().get(0);
xslfTextParagraph.setTextAlign(TextParagraph.TextAlign.CENTER);
xslfTextParagraph.setFontAlign(FontAlign.AUTO);
XSLFTextShape xslfTextShape = xslfTextParagraph.getParentShape();
xslfTextShape.setTextAutofit(TextAutofit.NORMAL); // <-- This sets the 'Shrink text on overflow'
xslfTextShape.setAnchor(rect1.getBounds2D());
}
这确实正确设置了“溢出时收缩文本”选项,但什么也没发生,文本不会收缩。
当我打开生成的 PPT 时,文本有原来的大小,并且溢出,但我可以看到设置了正确的选项。但是,如果我在此形状上设置任何内容或重置幻灯片,文本会突然缩小。因此,该选项设置正确,直到我刷新形状才会生效。
Apache POI 中有没有办法以某种方式强制刷新形状?或者有没有其他方法可以实现“溢出时收缩文本”的行为?
我正在使用 Apache POI 5.0。
【问题讨论】:
标签: java apache-poi