[英]Drawing a line between two points in PowerPoint slide using Apache POI
I'm starting to think I'm just not able to see the obvious. 我开始认为我看不到明显的东西。
Given the following code, I would like to draw a line from the coordinates [x1, y1] to [x2, y2]. 给定以下代码,我想从坐标[x1,y1]到[x2,y2]画一条线。
int x1 = 20;
int y1 = 10;
int x2 = 30;
int y2 = 5;
XSLFSlide pptSlide = ...
XSLFAutoShape shape = pptSlide.createAutoShape();
shape.setShapeType(ShapeType.LINE);
shape.setAnchor(x1, y1, <width>, <height>);
From what I can see the line starts at the anchor of [x1, y1] but then I have to enter a width and height instead of the coordinates of the target point. 从中可以看到,线从 [x1,y1]的锚点开始 ,但是随后我必须输入宽度和高度,而不是目标点的坐标。 But the y component of the target coordinate is less than that if the start coordinate so I tried setting the height to a negative values, which results in an error when PowerPoint tries to open the generated PPTX document (" PowerPoint found a problem with content in the file out.pptx. "); 但是目标坐标的y分量小于起始坐标的y分量,因此我尝试将高度设置为负值,这会在PowerPoint尝试打开生成的PPTX文档时导致错误(“ PowerPoint发现内容存在问题文件out.pptx。 ”);
I'm pretty sure I'm simply overlooking the obvious solution to this so can anybody help me in finding out how to draw a line for one point within the document to another point? 我敢肯定,我只是忽略了显而易见的解决方案,因此有人可以帮助我找出如何在文档中为一个点画线到另一点吗?
SetAnchor()
takes an AWT Rectangle2D
, which actually doesn't care if your width or height is negative (though a rectangle with negative height is not a real object after all, is it?). SetAnchor()
采用AWT Rectangle2D
,实际上它并不关心您的宽度或高度是否为负(尽管负高度的矩形毕竟不是真正的对象,对吗?)。 But POI doesn't interpret it that way, and unfortunately doesn't throw an exception to let you know. 但是POI不会以这种方式解释它,并且不幸的是,它不会抛出异常让您知道。
As I understand your scenario, you just need to choose the lower starting coordinates between x1
and x2
, y1
and y2
so that a positive width and height agree with your desired endpoint. 据我了解,您只需要在x1
和x2
, y1
和y2
之间选择较低的起始坐标,以使正的宽度和高度与所需的端点一致。
Something like this: 像这样:
// using Apache POI ooxml 3.17
static void drawBetweenTwoPoints(XSLFAutoShape shape, double x1, double x2, double y1, double y2) {
shape.setAnchor(new Rectangle2D.Double(
x1 <= x2 ? x1 : x2, // choose the lowest x value
y1 <= y2 ? y1 : y2, // choose the lowest y value
Math.abs(x2 - x1), // get the actual width
Math.abs(y2 - y1) // get the actual height
));
shape.setFlipVertical(y2 < y1); // lines are drawn from rectangle top-left to
// bottom right by default.
// When y2 is less than y1, flip the shape.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.