如何使用 Apache POI 设置 PowerPoint 文本框的边距?

How can I set the margin of a PowerPoint TextBox with Apache POI?

我有一个使用 Apache POI 创建 power point 演示文稿的应用程序。我有一些文本框,我需要将这些文本框的左右边距设置为 0。 假设它只是这两行:

xslfTextParagraph.setLeftMargin(0.0d);
xslfTextParagraph.setRightMargin(0.0d);

然而这似乎对我不起作用。

相关代码片段:

TextBox<XSLFShape, XSLFTextParagraph> textShape = shapeGroup.createTextBox();
Rectangle rect = new Rectangle(x, y, width, height);
textShape.setAnchor(rect.getBounds2D());
XSLFTextParagraph xslfTextParagraph = textShape.getTextParagraphs().get(0);
xslfTextParagraph.setLeftMargin(0.0); // <- This does not change the margin of the textbox
xslfTextParagraph.setRightMargin(0.0); // <- This does not change the margin of the textbox
XSLFTextRun r = xslfTextParagraph.addNewTextRun();
r.setText("Some text");
r.setFontColor(new Color(0,0,0));
r.setFontSize(14.0);
r.setBold(true);

Java 版本: 1.8, Apache POI 版本:5.0.0

您有什么建议可以将页边距设置为零吗?

我怀疑您要设置文本正文的正文属性。它们具有顶部、底部、左侧和右侧插入属性。

但要获得这些,需要 XSLFTextShape 而不是 TextBox<XSLFShape, XSLFTextParagraph> 界面。

...
  XSLFTextShape textShape = (XSLFTextShape)shapeGroup.createTextBox();
  Rectangle rect = new Rectangle(x, y, width, height);
  textShape.setAnchor(rect);
  XDDFTextBody textBody = textShape.getTextBody();
  XDDFBodyProperties bodyProperties = textBody.getBodyProperties();
  bodyProperties.setTopInset(0d);
  bodyProperties.setBottomInset(0d);
  bodyProperties.setLeftInset(0d);
  bodyProperties.setRightInset(0d);
...

完整示例:

import java.io.FileOutputStream;

import org.apache.poi.sl.usermodel.*;
import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.xddf.usermodel.text.*;

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

public class CreatePPTXGroupShape {

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

  SlideShow slideShow = new XMLSlideShow();

  Slide slide = slideShow.createSlide();

  int groupLeft = 100;
  int groupTop = 50;
  int groupWidth = 200;
  int groupHeight = 100;
  int groupPadding= 10;

  GroupShape shapeGroup = slide.createGroup();
  shapeGroup.setInteriorAnchor(new Rectangle(groupLeft, groupTop, groupWidth, groupHeight));
  shapeGroup.setAnchor(new Rectangle(groupLeft+groupPadding, groupTop+groupPadding, groupWidth-groupPadding, groupHeight-groupPadding));
  
  int x = groupLeft+20;
  int y = groupTop+20;
  int width = 100;
  int height = 20;

  XSLFTextShape textShape = (XSLFTextShape)shapeGroup.createTextBox();
  Rectangle rect = new Rectangle(x, y, width, height);
  textShape.setAnchor(rect);
  XDDFTextBody textBody = textShape.getTextBody();
  XDDFBodyProperties bodyProperties = textBody.getBodyProperties();
  bodyProperties.setTopInset(0d);
  bodyProperties.setBottomInset(0d);
  bodyProperties.setLeftInset(0d);
  bodyProperties.setRightInset(0d);
  
  XSLFTextParagraph xslfTextParagraph = textShape.getTextParagraphs().get(0);
  XSLFTextRun r = xslfTextParagraph.addNewTextRun();
  r.setText("Some text");
  r.setFontColor(new Color(0,0,0));
  r.setFontSize(14.0);
  r.setBold(true);

  FileOutputStream out = new FileOutputStream("CreatePPTXGroupShape.pptx");
  slideShow.write(out);
  out.close();
 }
}