java + springboot + asposediagram 实现visio丶vsd丶vsdx文件转换不同格式

本文介绍了如何在Java项目中使用Aspose.Diagram库将Visio文件转换为其他格式,包括设置Maven仓库、引入依赖、配置license.xml以及处理转换后可能存在的空白区域。还提供了代码示例和一个用于修剪图片空白边界的工具类。
摘要由CSDN通过智能技术生成

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文件

在这里插入图片描述

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是一个简单的示例代码,可以将Visio文件转换为mxGraph XML格式: ```java import com.mxgraph.io.mxCodec; import com.mxgraph.model.mxCell; import com.mxgraph.model.mxGeometry; import com.mxgraph.model.mxICell; import com.mxgraph.model.mxGraphModel; import com.mxgraph.util.mxXmlUtils; import com.mxgraph.view.mxGraph; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Map; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class VisioToMxGraphConverter { public static void main(String[] args) throws IOException { String visioFile = "example.vsd"; String mxGraphFile = "example.xml"; convert(visioFile, mxGraphFile); } public static void convert(String visioFile, String mxGraphFile) throws IOException { mxGraph graph = new mxGraph(); mxGraphModel model = (mxGraphModel) graph.getModel(); // Load Visio file as XML document File file = new File(visioFile); InputStream in = new FileInputStream(file); Document doc = mxXmlUtils.parseXml(mxXmlUtils.getXml(in)); Element root = doc.getDocumentElement(); // Map for storing cells by ID Map<String, mxICell> cells = new HashMap<String, mxICell>(); // Find all shapes in Visio file NodeList shapes = root.getElementsByTagName("Visio:Shape"); // Create mxCells for each Visio shape for (int i = 0; i < shapes.getLength(); i++) { Element shape = (Element) shapes.item(i); String id = shape.getAttribute("ID"); String name = shape.getAttribute("Name"); String type = shape.getAttribute("Type"); mxCell cell = new mxCell(name, new mxGeometry(), type); cells.put(id, cell); model.add(cell); } // Connect Visio shapes to form edges NodeList connectors = root.getElementsByTagName("Visio:Connect"); for (int i = 0; i < connectors.getLength(); i++) { Element connector = (Element) connectors.item(i); String from = connector.getAttribute("FromSheet"); String to = connector.getAttribute("ToSheet"); mxICell source = cells.get(from); mxICell target = cells.get(to); model.beginUpdate(); try { graph.insertEdge(graph.getDefaultParent(), null, "", source, target); } finally { model.endUpdate(); } } // Write mxGraph XML to file mxCodec codec = new mxCodec(); String xml = mxXmlUtils.getXml(codec.encode(model)); mxXmlUtils.writeFile(xml, mxGraphFile); } } ``` 该代码使用mxGraph库将Visio文件转换为mxGraph XML格式。它首先将Visio文件加载为XML文档,然后创建一个mxGraph对象和一个mxGraphModel对象。然后,它遍历每个Visio形状,并为每个形状创建一个mxCell对象,然后将其添加到mxGraphModel中。接下来,它遍历每个连接器,并使用从和到属性连接Visio形状以创建边缘。最后,它使用mxCodec将mxGraphModel编码为mxGraph XML格式,并将其写入文件中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值