简体   繁体   English

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

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

What is the correct way of getting the line width of a simple textbox with apache poi 5.0.0 from a pptx-file?从 pptx 文件中获取带有 apache poi 5.0.0 的简单文本框的线宽的正确方法是什么? I create a small project with maven apache poi, poi-ooxml and poi-scratchpad.我用 maven apache poi、poi-ooxml 和 poi-scratchpad 创建了一个小项目。

When i create a pptx named test.pptx with three textboxes with当我使用三个文本框创建一个名为test.pptx的 pptx 时

  • no border (has width 0.0)无边框(宽度为 0.0)
  • default border (has width 0.75)默认边框(宽度为 0.75)
  • border with width 2.0宽度为 2.0 的边框

then the following code outputs然后以下代码输出

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

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

In the documentation is said that width 0.0 is no border.在文档中说宽度0.0是没有边框的。 But how can i differentiate no border and default border, when both return 0.0 .但是,当两者都返回0.0时,我如何区分无边框和默认边框。 This should not be null from color.从颜色上看,这不应该是 null。

If a PowerPoint shape has line setting using default line width, then the width is not set.如果PowerPoint形状具有使用默认线宽的线设置,则不设置宽度。 Only the line itself is set having color settings.只有线本身设置有颜色设置。 In shape's XML this looks like:在形状的XML ,它看起来像:

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

But a line also may have gradient color, then this looks like:但是一条线也可能有渐变色,那么这看起来像:

<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>

Then no explicit line color is set and XSLFSimpleShape.getLineColor will return null .然后没有设置明确的线条颜色, XSLFSimpleShape.getLineColor将返回null

So to check whether a line color is set will not always get whether there is a line or not.所以检查是否设置了线条颜色并不总是得到是否有线条。

The correct way would be to check whether there is a line set in shape properties or not.正确的方法是检查形状属性中是否设置了线条。 But there is no such method in high level apache poi classes.但是在高级apache poi类中没有这样的方法。 So that only is possible using the underlying low level org.openxmlformats.schemas.presentationml.x2006.main.* classes.所以只有使用底层的低级org.openxmlformats.schemas.presentationml.x2006.main.*类才有可能。

Example for a method to check whether a shape has a line set:检查形状是否具有线集的方法示例:

 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