gpt4 book ai didi

java - 如何将apache的幻灯片形状元素转换为SVG PATH?

转载 作者:行者123 更新时间:2023-12-02 10:14:14 24 4
gpt4 key购买 nike

我使用 apache POI 将(SVG 路径 d="")转换为形状对象。代码是:

String d = "M100.000,100.000 L-0.000,100.000 L-0.000,-0.000 L100.000,-0.000 L100.000,100.000 Z";
PathParser parser = new PathParser();
AWTPathProducer pathProducer = new AWTPathProducer();
parser.setPathHandler(pathProducer);
parser.parse(d);
XSLFFreeformShape shape = this.slide.createFreeform();
shape.setPath(new Path2D.Double(pathProducer.getShape(),getTransform(translate, rotate, scale)));

导出为 PowerPoint 后正常显示。

现在,我正在尝试执行导入功能,但遇到问题。

XSLFFreeformShape shape = (XSLFFreeformShape) xslfShape;

我有一个形状对象,但无法将其转换为路径。

如何获取“路径”属性“d”?

如何转换?

请给我一些建议。

最佳答案

我不相信已经有这样的事情了。但你可以get PathIterator来自 Path2D.Double 并使用 PathIterator返回其边界的几何形状。然后将该边界几何图形转换为路径字符串应该是可能的。

示例:

import java.io.FileOutputStream;

import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.sl.usermodel.*;

import java.awt.Rectangle;
import java.awt.geom.Path2D;
import java.awt.geom.PathIterator;
import java.awt.geom.AffineTransform;

public class CreatePPTXFreeformShape {

static String getPathString(Path2D.Double path2D) {
double[] coords = new double[6];
StringBuilder pathStringBuilder = new StringBuilder();
for (PathIterator pathIterator = path2D.getPathIterator(new AffineTransform()); !pathIterator.isDone(); pathIterator.next()) {
int type = pathIterator.currentSegment(coords);
if (type == PathIterator.SEG_MOVETO) {
pathStringBuilder.append("M");
pathStringBuilder.append(coords[0] + "," + coords[1]);
pathStringBuilder.append(" ");
} else if (type == PathIterator.SEG_LINETO) {
pathStringBuilder.append("L");
pathStringBuilder.append(coords[0] + "," + coords[1]);
pathStringBuilder.append(" ");
} else if (type == PathIterator.SEG_QUADTO) {
pathStringBuilder.append("Q");
pathStringBuilder.append(coords[0] + "," + coords[1] + "," + coords[2] + "," + coords[3]);
pathStringBuilder.append(" ");
} else if (type == PathIterator.SEG_CUBICTO) {
pathStringBuilder.append("C");
pathStringBuilder.append(coords[0] + "," + coords[1] + "," + coords[2]+ "," + coords[3] + "," + coords[4] + "," + coords[5]);
pathStringBuilder.append(" ");
} else if (type == PathIterator.SEG_CLOSE) {
pathStringBuilder.append("Z");
pathStringBuilder.append(" ");
}
}
return pathStringBuilder.toString();
}

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

XMLSlideShow slideShow = new XMLSlideShow();
XSLFSlide slide = slideShow.createSlide();

Path2D.Double path2D = null;

XSLFFreeformShape shape1 = slide.createFreeform();

path2D = new Path2D.Double();
path2D.moveTo(100.000,100.000);
path2D.lineTo(-0.000,100.000);
path2D.lineTo(-0.000,-0.000);
path2D.lineTo(100.000,-0.000);
//path2D.lineTo(100.000,100.000);
path2D.closePath();

shape1.setPath(path2D);

shape1.setLineWidth(1.0);
shape1.setLineColor(java.awt.Color.BLACK);
shape1.setAnchor(new Rectangle(100, 100, 100, 100));

XSLFFreeformShape shape2 = slide.createFreeform();
path2D = new Path2D.Double();
path2D.moveTo(100.000,100.000);
path2D.curveTo(60.000,0.000, 40.000,0.000, 0.000,100.000);
path2D.closePath();

shape2.setPath(path2D);

shape2.setLineWidth(1.0);
shape2.setLineColor(java.awt.Color.BLACK);
shape2.setAnchor(new Rectangle(300, 100, 100, 100));

XSLFFreeformShape shape3 = slide.createFreeform();
path2D = new Path2D.Double();
path2D.moveTo(100.000,100.000);
path2D.quadTo(50.000,0.000, 0.000,100.000);
path2D.closePath();

shape3.setPath(path2D);

shape3.setLineWidth(1.0);
shape3.setLineColor(java.awt.Color.BLACK);
shape3.setAnchor(new Rectangle(500, 100, 100, 100));

String pathString = getPathString(shape1.getPath());
System.out.println(pathString);

pathString = getPathString(shape2.getPath());
System.out.println(pathString);

pathString = getPathString(shape3.getPath());
System.out.println(pathString);

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

关于java - 如何将apache的幻灯片形状元素转换为SVG PATH?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54785158/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com