Aspose.word java 实现word转pdf

1、使用的依赖

	<dependencies>
		<dependency>
			<groupId>com.aspose</groupId>
			<artifactId>aspose-words</artifactId>
			<version>14.9.0</version>
		</dependency>
	</dependencies>

	<repositories>
		<repository>
			<id>com.e-iceblue</id>
			<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
		</repository>

		<repository>
			<id>aspose-maven-repository</id>
			<url>http://artifact.aspose.com/repo/</url>
		</repository>
	</repositories>

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目录下

<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>
</License>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值