繁体   English   中英

如何从 apache poi XSLF 获取文本框的线宽?

[英]How to get line width of a textbox from apache poi XSLF?

从 pptx 文件中获取带有 apache poi 5.0.0 的简单文本框的线宽的正确方法是什么? 我用 maven apache poi、poi-ooxml 和 poi-scratchpad 创建了一个小项目。

当我使用三个文本框创建一个名为test.pptx的 pptx 时

  • 无边框(宽度为 0.0)
  • 默认边框(宽度为 0.75)
  • 宽度为 2.0 的边框

然后以下代码输出

    FileInputStream fis = new FileInputStream("test.pptx");
    XMLSlideShow ppt = new XMLSlideShow(fis);
    fis.close();

    for (XSLFSlide slide : ppt.getSlides()) {
        for (XSLFShape shape : slide.getShapes()) {

            if (shape instanceof XSLFTextBox) {
                XSLFTextBox textBox = (XSLFTextBox) shape;
                
                String text = textBox.getText();
                System.out.println(text);
            
                double borderWidth = textBox.getLineWidth();
                System.out.println("line: "+borderWidth+", "+textBox.getLineColor());

            }
        }
    }
  • 无边框: line: 0.0, null
  • 默认值: line: 0.0, java.awt.Color[r=91,g=155,b=213]
  • 边框 2.0: line: 2.0, java.awt.Color[r=91,g=155,b=213]

在文档中说宽度0.0是没有边框的。 但是,当两者都返回0.0时,我如何区分无边框和默认边框。 从颜色上看,这不应该是 null。

如果PowerPoint形状具有使用默认线宽的线设置,则不设置宽度。 只有线本身设置有颜色设置。 在形状的XML ,它看起来像:

<p:sp>
...
 <p:spPr>
 ...
  <a:ln>
   <a:solidFill>
    <a:schemeClr val="..."/>
   </a:solidFill>
  </a:ln>
  ...
 </p:spPr>
 ...
</p:sp>

但是一条线也可能有渐变色,那么这看起来像:

<p:sp>
 ...
 <p:spPr>
 ...
  <a:ln>
   <a:gradFill>
    <a:gsLst>
    ...
    </a:gsLst>
    <a:lin scaled="1" ang="5400000"/>
   </a:gradFill>
  </a:ln>
  ...
 </p:spPr>
 ...
</p:sp>

然后没有设置明确的线条颜色, XSLFSimpleShape.getLineColor将返回null

所以检查是否设置了线条颜色并不总是得到是否有线条。

正确的方法是检查形状属性中是否设置了线条。 但是在高级apache poi类中没有这样的方法。 所以只有使用底层的低级org.openxmlformats.schemas.presentationml.x2006.main.*类才有可能。

检查形状是否具有线集的方法示例:

 boolean isShapeLineSet(XSLFShape shape) {
  boolean result = false;
  org.apache.xmlbeans.XmlObject shapeXmlObjekt = shape.getXmlObject();
  if (shapeXmlObjekt instanceof org.openxmlformats.schemas.presentationml.x2006.main.CTShape) {
   org.openxmlformats.schemas.presentationml.x2006.main.CTShape cTShape = (org.openxmlformats.schemas.presentationml.x2006.main.CTShape)shapeXmlObjekt;
   if (cTShape.getSpPr() != null) {
    if (cTShape.getSpPr().getLn() != null) {
     result = true;
    }
   }       
  }
  return result;     
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM