aspose 完美解决word转pdf


import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;


@Slf4j
@Component
public class Word2PdfAsposeUtil {

    public   void docToPdf(String docPath, HttpServletResponse response) throws FileNotFoundException {
        FileInputStream inputStream = new FileInputStream(docPath);
        docToPdf(inputStream, response);
    }

    public   void docToPdf(MultipartFile file, HttpServletResponse response) throws IOException {
        InputStream inputStream = file.getInputStream();
        this.docToPdf(inputStream, response);
    }

    public void docToPdf(InputStream inputStream, HttpServletResponse response) throws FileNotFoundException {

        String path = this.getClass().getClassLoader().getResource("").getPath();
        File fileLicense = new File(path + "license.xml");
        InputStream is = new FileInputStream(fileLicense);
        OutputStream outputStream = null;
        try {
            try {

//            去除水印
                License license = new License();
                license.setLicense(is);
            } catch (Exception e) {
                log.error("docToPdf去除水印失败");
            }
            //到处
            Document document = new Document(inputStream);
            response.reset();
            outputStream = response.getOutputStream();
            document.save(outputStream, SaveFormat.PDF);
        } catch (Exception e) {
            log.error("docToPdf转化失败:" + e.getMessage());
        } finally {
            try {
                outputStream.flush();
                outputStream.close();
//                is.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }

//    public  File docToPdf(InputStream docPathInputStream, String pdfPath) {
//        File pdfFile = new File(pdfPath);
//        ByteArrayInputStream is = new ByteArrayInputStream(licenseStr.getBytes());
//        try {
//            License license = new License();
//            license.setLicense(is);
//            Document document = new Document(docPathInputStream);
//            FileOutputStream outputStream = new FileOutputStream(pdfFile);
//            document.save(outputStream, SaveFormat.PDF);
//            outputStream.close();
//            is.close();
//        } catch (Exception e) {
//            System.out.println("转化失败");
//            e.printStackTrace();
//        }
//        return pdfFile;
//    }

}

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>
</License>

依赖jar包

  <dependency>
            <groupId>com.aspose.cells</groupId>
            <artifactId>aspose-cells</artifactId>
            <version>20.4-SNAPSHOT</version>
                        <scope>system</scope>
                        <systemPath>${project.basedir}/../../start/src/main/resources/libs/aspose-cells-20.4 - c.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>com.aspose.words</groupId>
            <artifactId>aspose-words</artifactId>
            <version>19.5-RELEASE</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/../../start/src/main/resources/libs/aspose-words-18.8.jar</systemPath>
        </dependency>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值