栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Aspose.word java 实现word转pdf

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Aspose.word java 实现word转pdf

1、使用的依赖
	
		
			com.aspose
			aspose-words
			14.9.0
		
	

	
		
			com.e-iceblue
			https://repo.e-iceblue.cn/repository/maven-public/
		

		
			aspose-maven-repository
			http://artifact.aspose.com/repo/
		
	
2、编写aspose.word 工具类
public class AsposeWordUtils {

    private static final Logger log = LoggerFactory.getLogger(AsposeWordUtils.class);

    static {
        String fileName = "license.xml";
        try (InputStream license = AsposeWordUtils.class.getClassLoader().getResource(fileName).openStream()) {
            License asposeLicense = new License();
            asposeLicense.setLicense(license);
        } catch (Exception e) {
            log.error("引入license文件失败!", e);
        }
    }

	public static byte[] byteToPdf(byte[] content) {
        try(ByteArrayOutputStream bos = new ByteArrayOutputStream();
            InputStream inputStream = new ByteArrayInputStream(content)) {
            document document = new document(inputStream);
            document.save(bos, SaveFormat.PDF);
            return bos.toByteArray();
        } catch (Exception e) {
            log.error("字节数组转pdf字节数组失败!", e);
            return null;
        }
    }

	public static byte[] docToDocx(byte[] content) {
        try(ByteArrayOutputStream bos = new ByteArrayOutputStream();
            InputStream inputStream = new ByteArrayInputStream(content)) {
            document document = new document(inputStream);
            document.save(bos, SaveFormat.DOCX);
            return bos.toByteArray();
        } catch (Exception e) {
            log.error("doc字节数组转docx字节数组失败!", e);
            return null;
        }
    }
 }
3、测试代码
	@Test
    public void wordToPdf(){

        String filePath = "C:\Users\desktop\docx文件的.docx";

	    byte[] content = new byte[0];
	    try {
	          content = Files.readAllBytes(Paths.get(filePath));
	    } catch (IOException e) {
	          e.printStackTrace();
	    }

		// 如果doc文件转成的pdf打不开的话,可先将doc字节流转成docx字节流,再转成pdf
        // content = AsposeWordUtils.docToDocx(content);
		// word字节数组转pdf字节数组
        byte[] result = AsposeWordUtils.byteToPdf(content);
        
        InputStream inputStream = new ByteArrayInputStream(result);
        FileOutputStream fos = new FileOutputStream("C:\Users\desktop\导出的pdf文件.pdf");
        byte[] buffer = new byte[1024];
        int r = 0;
        while ((r = inputStream.read(buffer)) != -1) {
            fos.write(buffer, 0, r);
        }
        inputStream.close();
        
    }		
4、写一个license.xml文件,SpringBoot项目放resource目录下

  
    
      Aspose.Total for Java
      Aspose.Words for Java
    
    Enterprise
    20991231
    20991231
    8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7
  
  sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/754906.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号