Apache POI 形状之间的多个连接器

问题描述

最近我正在使用 Spring Boot 使用 Apache POI,但我遇到了一个问题。我想表示实体之间的数据库连接,但在可视化多个连接时遇到问题。
所以我的基本问题是分离连接。

这是我的开始:1

你看不到但是这两个矩形之间有多个连接,但是由于相同的起点和终点,它们彼此重叠。

生成的 XML 代码如下:

<p:cxnSp>
   <p:nvCxnSpPr>
       <p:cNvPr name="Connector 7" id="7" />
       <p:cNvCxnSpPr>
           <a:stCxn id="2" idx="3" />
           <a:endCxn id="3" idx="1" />
       </p:cNvCxnSpPr>
       <p:nvPr />
   </p:nvCxnSpPr>
   <p:spPr>
       <a:xfrm flipV="1">
           <a:off y="2199409" x="2616200" />
           <a:ext cy="1318491" cx="2413000" />
       </a:xfrm>
       <a:prstGeom prst="curvedConnector3">
           <a:avLst />
       </a:prstGeom>
       <a:ln w="9525">
           <a:solidFill>
               <a:srgbClr val="000000" />
           </a:solidFill>
       </a:ln>
   </p:spPr>
</p:cxnSp> 

所以我想要做的是将弯曲的连接器中点设置为不同的值,就像在这图片中一样(例如,我手动执行此操作): 2

我尝试向连接器添加一些其他 GeomGuide 元素,但结果是一个不稳定的 pptx 文件

<a:prstGeom prst="curvedConnector3">
    <a:avLst>
        <a:gd name="adj1" fmla="val 57365" />
    </a:avLst>
</a:prstGeom>

相关的java代码片段:

XSLFConnectorShape connector1 = slide.createConnector();
CTNonVisualConnectorProperties cx = ctConnector.getNvCxnSpPr().getCNvCxnSpPr();
        CTConnection start = cx.addNewStCxn();
        start.setId(shapeIdStart);
        start.setIdx(rel.getStartSide());

        CTConnection end = cx.addNewEndCxn();
        end.setId(shapeIdEnd);
        end.setIdx(rel.getEndSide());


         CTGeomGuideList ctGeomGuideList = ctConnector.getSpPr().getPrstGeom().getAvLst();
         CTGeomGuide ctGeomGuide = ctGeomGuideList.addNewGd();
         ctGeomGuide.setName("adj");
         ctGeomGuide.setFmla("val 45000");

解决方法

您提供的代码片段不完整。所以不清楚这是否是唯一的问题。但无论如何 ctGeomGuide.setName("adj"); 都是错误的。调整手柄已编号,并且没有仅称为 adj 的调整手柄。它必须是 ctGeomGuide.setName("adj1");

以下代码是一个 Minimal,Reproducible Example,它可以工作并创建您想要的结果。

import java.io.FileOutputStream;

import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.sl.usermodel.*;
import org.openxmlformats.schemas.presentationml.x2006.main.*;
import org.openxmlformats.schemas.drawingml.x2006.main.*;

import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.awt.Color;

public class CreatePPTXConnectorShapes {
    
 private static XSLFConnectorShape createConnector(XSLFSlide slide,XSLFAutoShape shape1,XSLFAutoShape shape2) {
  XSLFConnectorShape connector = slide.createConnector();
  connector.setShapeType(ShapeType.CURVED_CONNECTOR_3);
  connector.setAnchor(new Rectangle2D.Double( //connector is diagonal in a rectangle
   shape1.getAnchor().getMaxX(),// top left x of that rectangle is x position of right edge of shape1
   shape2.getAnchor().getCenterY(),// top left y of that rectangle is center y of shape2 as shape2 is above shape1
   shape2.getAnchor().getX()-shape1.getAnchor().getMaxX(),// width of that rectanle is x of shape2 minus x position of right edge of shape1 as shape2 is right of shape1
   shape1.getAnchor().getCenterY()-shape2.getAnchor().getCenterY() // height of that rectanle is center y of shape1 minus center y of shape2 as shape2 is above shape1
   ));
  connector.setFlipVertical(true); // the rectangle needs to be flipped vertically as the connector shall be diagonal from bottom left to top right

  CTConnector ctConnector = (CTConnector)connector.getXmlObject();
  CTNonVisualConnectorProperties cx = ctConnector.getNvCxnSpPr().getCNvCxnSpPr();
  CTConnection start = cx.addNewStCxn();
  start.setId(shape1.getShapeId());
  start.setIdx(3); // connecting point 3 is center of right edge
  CTConnection end = cx.addNewEndCxn();
  end.setId(shape2.getShapeId());
  end.setIdx(1); // connecting point 1 is center of left edge
  return connector;  
 }

 public static void main(String[] args) throws Exception {

  XMLSlideShow slideShow = new XMLSlideShow();

  XSLFSlide slide = slideShow.createSlide();

  XSLFAutoShape shape1 = slide.createAutoShape();
  shape1.setShapeType(ShapeType.RECT);
  shape1.setFillColor(Color.GREEN);
  shape1.setAnchor(new Rectangle(50,150,100));
  
  XSLFAutoShape shape2 = slide.createAutoShape();
  shape2.setShapeType(ShapeType.RECT);
  shape2.setFillColor(Color.GREEN);
  shape2.setAnchor(new Rectangle(500,50,100));
  
  // first connector 
  XSLFConnectorShape connector1 = createConnector(slide,shape1,shape2);
  CTConnector ctConnector = (CTConnector)connector1.getXmlObject(); 
  CTGeomGuideList ctGeomGuideList = ctConnector.getSpPr().getPrstGeom().getAvLst();
  CTGeomGuide ctGeomGuide = ctGeomGuideList.addNewGd();
  ctGeomGuide.setName("adj1");
  ctGeomGuide.setFmla("val 45000");
  
  //second connector
  XSLFConnectorShape connector2 = createConnector(slide,shape2);
  ctConnector = (CTConnector)connector2.getXmlObject();  
  ctGeomGuideList = ctConnector.getSpPr().getPrstGeom().getAvLst();
  ctGeomGuide = ctGeomGuideList.addNewGd();
  ctGeomGuide.setName("adj1");
  ctGeomGuide.setFmla("val 57365");

  FileOutputStream out = new FileOutputStream("CreatePPTXConnectorShapes.pptx");
  slideShow.write(out);
  out.close();
 }
}

结果: enter image description here

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...