java + springboot + asposediagram 实现visio丶vsd丶vsdx文件转换不同格式
实现visio文件转换格式
·
asposediagram实现visio文件转换格式
pom文件设置仓库地址并引入相关jar包
<!-- 远程仓库地址 -->
<repositories>
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>http://repository.aspose.com/repo/</url>
</repository>
</repositories>
<!--visio文件转换依赖-->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-diagram</artifactId>
<version>24.2</version>
</dependency>
添加秘钥文件license.xml
注:不添加秘钥转换的文件会不全并且带有水印,秘钥文件放入项目某个配置目录下,后面转换时要读取
<License>
<Data>
<Products>
<Product>Aspose.Total for Java</Product>
<Product>Aspose.Words for Java</Product>
</Products>
<EditionType>Enterprise</EditionType>
<SubscriptionExpiry>20991231</SubscriptionExpiry>
<LicenseExpiry>20991231</LicenseExpiry>
<SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
</Data>
<Signature>
sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=
</Signature>
<!-- <Signature>Rgt4VMu3wfLGG8ZRjQ6/zGvG0NDiqDy8xd4E6H4uRgLOO6l1Q7psY5YG7ByQyv9ybUv9inqp00s6AwpzhSzMyjC1m6nCtMMyHKJ1jMNN3t7rrV08DUeBCOg9JOqc8pyJNWBiS3+gU+24+L0EG7ExpaG4J2OcHtsnpmAwOLekxwg=</Signature>-->
<!-- <Signature>0nRuwNEddXwLfXB7pw66G71MS93gW8mNzJ7vuh3Sf4VAEOBfpxtHLCotymv1PoeukxYe31K441Ivq0Pkvx1yZZG4O1KCv3Omdbs7uqzUB4xXHlOub4VsTODzDJ5MWHqlRCB1HHcGjlyT2sVGiovLt0Grvqw5+QXBuinoBY0suX0=</Signature>-->
</License>
例如我这边就放在项目resources/config下
转换代码示例
@Test
public void test() {
//visio文件地址
String visioPath = "测试.vsd";
//visio转换png保存地址
String visioPngPath = "测试.png";
try {
//获取秘钥
InputStream isLicense = this.getClass().getResourceAsStream("/config/license.xml");
License license = new License();
license.setLicense(isLicense);
// 创建Diagram类的实例
Diagram diagram = new Diagram(visioPath);
// 创建图像保存选项
ImageSaveOptions options = new ImageSaveOptions(SaveFileFormat.PNG);
// 将Visio图另存为PNG
diagram.save(visioPngPath, options);
} catch (Exception e) {
e.printStackTrace();
}
}
如果需要转别的文件格式,只需要SaveFileFormat.PNG换成支持转换的格式,SaveFileFormat是个常量类,里面有支持转换的格式
转换后图片区域空白问题
由于转换成png后存在空白区域问题,这边提供了一个工具类,可以去除图片多余空白区域
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/**
* 图片空白区域裁剪工具类
*/
public class ImgMarginTrimUtils {
private BufferedImage img;
// 图片右边和底部保留一点白色背景,否则太难看了
private static final int REMAIN_WIDTH = 20;
public ImgMarginTrimUtils(File input) {
try {
img = ImageIO.read(input);
} catch (IOException e) {
throw new RuntimeException( "Problem reading image", e );
}
}
public void trim() {
int width = getTrimmedWidth();
int height = getTrimmedHeight();
BufferedImage newImg = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = newImg.createGraphics();
g.drawImage( img, 0, 0, null );
img = newImg;
}
public void write(File f) {
try {
ImageIO.write(img, "png", f);
} catch (IOException e) {
throw new RuntimeException( "Problem writing image", e );
}
}
private int getTrimmedWidth() {
int height = this.img.getHeight();
int width = this.img.getWidth();
int trimmedWidth = 0;
for(int i = 0; i < height; i++) {
for(int j = width - 1; j >= 0; j--) {
if(img.getRGB(j, i) != Color.WHITE.getRGB() &&
j > trimmedWidth) {
trimmedWidth = j;
break;
}
}
}
trimmedWidth += REMAIN_WIDTH;
if(trimmedWidth>width){
trimmedWidth = width;
}
return trimmedWidth;
}
private int getTrimmedHeight() {
int width = this.img.getWidth();
int height = this.img.getHeight();
int trimmedHeight = 0;
for(int i = 0; i < width; i++) {
for(int j = height - 1; j >= 0; j--) {
if(img.getRGB(i, j) != Color.WHITE.getRGB() &&
j > trimmedHeight) {
trimmedHeight = j;
break;
}
}
}
trimmedHeight += REMAIN_WIDTH;
if(trimmedHeight>height){
trimmedHeight = height;
}
return trimmedHeight;
}
public static void main(String[] args) {
ImgMarginTrimUtils trim = new ImgMarginTrimUtils(new File("测试.png"));
trim.trim();
trim.write(new File("测试.png"));
}
}
最后附上源visio文件以及转换成png并且处理后的文件
visio文件
转换成png后的的visio文件
更多推荐
所有评论(0)