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

362 views Asked by At

What is the correct way of getting the line width of a simple textbox with apache poi 5.0.0 from a pptx-file? I create a small project with maven apache poi, poi-ooxml and poi-scratchpad.

When i create a pptx named test.pptx with three textboxes with

  • no border (has width 0.0)
  • default border (has width 0.75)
  • border with width 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
  • default: 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]

In the documentation is said that width 0.0 is no border. But how can i differentiate no border and default border, when both return 0.0. This should not be null from color.

1

There are 1 answers

0
Axel Richter On

If a PowerPoint shape has line setting using default line width, then the width is not set. Only the line itself is set having color settings. In shape's XML this looks like:

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

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. So that only is possible using the underlying low level org.openxmlformats.schemas.presentationml.x2006.main.* classes.

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