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