java实现word转pdf

java实现将word文件转换成pdf文件

1. 源代码如下:

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;

import java.io.*;

/**
 * @Description
 * @ClassName WordSwitchToPdf
 * @Author yuhuofei
 * @Date 2022/3/16 23:54
 * @Version 1.0
 */
public class WordSwitchToPdf {

    /**
     * 将word文件转换成pdf文件
     *
     * @param wordFilePath
     * @param pdfFilePath
     */
    public static void wordSwitchPdf(String wordFilePath, String pdfFilePath) {
        File file = new File(wordFilePath);
        if (file.exists()) {
            BufferedInputStream bufferedInputStream = null;
            FileInputStream fileInputStream = null;
            try {
                fileInputStream = new FileInputStream(file);
                bufferedInputStream = new BufferedInputStream(fileInputStream);
                Document document = new Documented(bufferedInputStream);
                document.save(pdfFilePath, SaveFormat.PDF);
            } catch (Exception e) {
                System.out.println("word文件转换为pdf文件异常" + e);
            } finally {
                if (bufferedInputStream != null) {
                    try {
                        bufferedInputStream.close();
                    } catch (IOException ignored) {
                        System.out.println("关闭流异常" + ignored);
                    }
                }
                if (fileInputStream != null) {
                    try {
                        fileInputStream.close();
                    } catch (IOException ignored) {
                        System.out.println("关闭流异常" + ignored);
                    }
                }
            }
        }
    }

    //激活license
    static {
        final License license = new License();
        InputStream inputStream = null;
        try {
            inputStream = WordSwitchToPdf.class.getResourceAsStream("/license.xml");
            license.setLicense(inputStream);
        } catch (Exception e) {
            System.out.println("license激活失败" + e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

2. 需要引入的依赖:

<dependency>
     <groupId>com.asposegroupId>
     <artifactId>aspose-wordsartifactId>
     <version>21.6.0version>
dependency>

3. 需要找个能用的license.xml文件,放在静态资源(resources)目录下


<License>
    <Data>
        <Products>
            <Product>Aspose.Total for JavaProduct>
            <Product>Aspose.Words for JavaProduct>
        Products>
        <EditionType>EnterpriseEditionType>
        <SubscriptionExpiry>20991231SubscriptionExpiry>
        <LicenseExpiry>20991231LicenseExpiry>
        <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7SerialNumber>
    Data>
    <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=Signature>
License>

(如果没有license文件,那么通过word转换出来的pdf,会带有水印)

你可能感兴趣的:(Java,java)