【问题标题】:How can I set the margin of a PowerPoint TextBox with Apache POI?如何使用 Apache POI 设置 PowerPoint 文本框的边距?
【发布时间】:2022-01-06 18:01:19
【问题描述】:

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

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

但是这似乎对我不起作用。

相关代码sn-p:

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

您有什么建议我如何将边距设置为零?

【问题讨论】:

    标签: java apache-poi powerpoint


    【解决方案1】:

    我怀疑你想设置正文的正文属性。它们具有 top-、bottom-、left- 和 right-inset 属性。

    但要获得这些,需要XSLFTextShape 而不是TextBox&lt;XSLFShape, XSLFTextParagraph&gt; 接口。

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

    【讨论】:

    • 我可以确认这就像一个魅力!这正是我所需要的。很好,详细的答案。你应该得到一枚奖牌,谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-24
    • 2021-05-21
    • 2011-06-16
    相关资源
    最近更新 更多