JAVA word转pdf各个版本都支持,aspose-words

以下提供两种方法实现,建议使用aspose

一、第一种,使用aspose-words Word转PDF

        第一步:首先需要下载aspose-words-15.8.0包,官方的地址很慢,并且包下载不下来,有需要可以去GITHUB上寻找,这里我提供一个离线包CSDN下载地址:aspose-words-15.8.0.rar-其它文档类资源-CSDN下载

        第二步:把jar包使用pom或则web项目引入

        此处演示pom.xml导入,下载以上CSDN中资源放入Maven的repository中即可导入

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-words</artifactId>
    <version>15.8.0</version>
    <classifier>jdk16</classifier>
</dependency>

        第三步:配置一下License.xml,和水印说byebye,智能破解(xml文件放入当前包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>

        第四步:pdf转word

/**
 * @author : 
 * @date : 
 * description : Aspose工具类
 */
public class AsposeUtil {

    /**
     * 加载license 用于破解 不生成水印
     *
     * @author LCheng
     * @date 2020/12/25 13:51
     */
    @SneakyThrows
    private static void getLicense() {
        try (InputStream is = AsposeUtil.class.getClassLoader().getResourceAsStream("License.xml")) {
            License license = new License();
            license.setLicense(is);
        }
    }

    /**
     * word转pdf
     *
     * @param wordPath word文件保存的路径
     * @param pdfPath  转换后pdf文件保存的路径
     * @author LCheng
     * @date 2020/12/25 13:51
     */
    @SneakyThrows
    public static void wordToPdf(String wordPath, String pdfPath) {
        getLicense();
        File file = new File(pdfPath);
        try (FileOutputStream os = new FileOutputStream(file)) {
            Document doc = new Document(wordPath);
            doc.save(os, SaveFormat.PDF);
        }
    }
}

二、第二种,使用poi和最初的itextpdf包(这个方法和最新版的poi包有冲突,不能兼容最新版的poi包)

第一步pom导入:

<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>ooxml-schemas</artifactId>
   <version>1.3</version>
   </dependency>
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi-ooxml</artifactId>
  <version>3.15</version>
</dependency>
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>4.0.1</version>
</dependency>

第二步:转换


import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import com.lowagie.text.Font;
import com.lowagie.text.pdf.BaseFont;
import lombok.SneakyThrows;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.*;


public class WordToPdf {
    private final static String DOC_X = "E:\\out\\汛期简报第23期.docx";
    private final static String PDF = "E:\\out\\test.pdf";

    /**
     * 第二种方法
     * @param wordPath
     * @param resOut
     */
    public static void convertPdf (String wordPath,OutputStream resOut){
        //读取word
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(wordPath);
            XWPFDocument doc = new XWPFDocument(inputStream);
            PdfOptions options = PdfOptions.create();
            options.fontProvider((familyName, encoding, size, style, color) -> {
                BaseFont bfChinese = null;
                try {
                    bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/STSONG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Font fontChinese = new Font(bfChinese, size, style, color);
                    if (familyName != null)
                        fontChinese.setFamily(familyName);
                    return fontChinese;
            });
            PdfConverter.getInstance().convert(doc, resOut, options);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 第一种方法,很完整的生成,不影响包冲突(aspose-words)
     * @param wordPath
     * @param resOut
     */
    public static void convertPdfFinish(String wordPath,OutputStream resOut){
        //破解
        getLicense();
        try {
            Document doc = new Document(wordPath);
            doc.save(resOut,SaveFormat.PDF);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 加载license 用于破解 不生成水印
     *
     * @author LCheng
     * @date 2020/12/25 13:51
     */
    @SneakyThrows
    private static void getLicense() {
        try (InputStream is = WordToPdf.class.getClassLoader().getResourceAsStream("License.xml")) {
            License license = new License();
            license.setLicense(is);
        }
    }
    /**
     * word转pdf
     *
     * @param wordPath word文件保存的路径
     * @param pdfPath  转换后pdf文件保存的路径
     * @author LCheng
     * @date 2020/12/25 13:51
     */
    @SneakyThrows
    public static void wordToPdf(String wordPath, String pdfPath) {
        getLicense();
        File file = new File(pdfPath);
        try (FileOutputStream os = new FileOutputStream(file)) {
            Document doc = new Document(wordPath);
            doc.save(os, SaveFormat.PDF);
        }
    }


    public static void main(String[] args) {
        wordToPdf(DOC_X,PDF);
    }

    /*public static void main(String[] args) {
        try {
            InputStream is = new FileInputStream(DOC_X);
            XWPFDocument doc = new XWPFDocument(is);
            // 在没有字体的服务器上发布要用到下面 options,同时在resource目录下加入字体文件, windows 服务器上可不加
            PdfOptions options = PdfOptions.create();
            options.fontProvider((familyName, encoding, size, style, color) -> {
                try {
                    BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/STSONG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
                    Font fontChinese = new Font(bfChinese, size, style, color);
                    if (familyName != null)
                        fontChinese.setFamily(familyName);
                    return fontChinese;
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            });
            OutputStream out = new FileOutputStream(PDF);
            PdfConverter.getInstance().convert(doc, out, options);
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/
}

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: aspose-words-19.3-jdk17.jar-java 是一个 Java 库,用于处理 Word 文档。它提供了一套丰富的功能和方法,允许开发人员在 Java 应用程序中读取、创建、编辑和Word 文档。 使用 aspose-words-19.3-jdk17.jar-java,可以轻松地将 Word 文档导入到 Java 应用程序中,并以编程方式访问和修改其中的内容。这个库支持各种 Word 文档格式,如 DOC、DOCX、RTF 等。可以使用它来提取文本、插入或删除段落、添加或删除图片、设置样式和格式等。 这个库还提供了强大的换功能,可以将 Word 文档换为其他格式,比如 PDF、HTML、EPUB 等。同时,也可以将其他格式的文档换为 Word 文档。 aspose-words-19.3-jdk17.jar-java 在处理 Word 文档时提供了高度的可定制性。开发人员可以根据自己的需求设置字体、颜色、段落格式、页眉页脚等等。此外,它还支持在文档中插入书签、超链接、表格、图表等,以满足更复杂的需求。 总之,aspose-words-19.3-jdk17.jar-java 是一个功能强大、易于使用的 Java 库,适用于处理 Word 文档。它可为开发人员提供丰富的功能和方法,让他们能够方便地读取、创建、编辑和Word 文档。无论是对于个人使用还是开发团队,这个库都是一个非常有价值的工具。 ### 回答2: aspose-words-19.3-jdk17.jar是一个Java库,用于处理Word文档。它提供了丰富的功能和API,可以读取、修改和创建Word文档。通过它,我们可以在Java应用程序中实现对Word文档的自动化处理。 使用aspose-words-19.3-jdk17.jar,我们可以轻松地读取现有的Word文档,并提取其中的文本、表格、图像等元素。同时,还可以编辑文档的样式、格式和布局。例如,我们可以插入、删除或合并段落,设置字体、字号、颜色等。 这个库还提供了对Word文档的导出和换功能。我们可以将文档保存为PDF、HTML、XML或其他流行的格式。此外,我们还可以将Word文档换为其他Office应用程序的格式,如Excel或PowerPoint。 aspose-words-19.3-jdk17.jar还支持Word模板的创建和填充。我们可以使用预定义的模板,向其添加数据,并将其保存为新的Word文档。这对于生成报告、信函、合同等标准格式的文档非常有用。 总而言之,aspose-words-19.3-jdk17.jar是一个功能强大的Java库,提供了处理Word文档的丰富功能和API。它可以帮助我们在Java应用程序中实现对Word文档的读取、编辑、导出和换。无论是处理现有文档还是生成新的文档,它都是一个非常有用的工具。 ### 回答3: Aspose.Words是一个强大的Java库,用于处理和操作Microsoft Word文档。版本19.3是指该库的19.3版本,该版本具有很多功能和改进。 Aspose.Words可以在Java应用程序中创建,读取,操作和Word文档。使用该库,您可以轻松地访问文档的内容,如文本,图片,表格,样式和格式。您可以添加,删除和修改文档的内容,包括段落,表格,图像等。您还可以应用不同的格式和样式,如字体,颜色,段落间距等。 此外,Aspose.Words支持文档的换功能。您可以将Word文档换为其他格式,如PDF,HTML,纯文本等。同样,您也可以将其他格式的文件换为Word文档。这使得Aspose.Words成为一种非常有用的工具,适用于处理和换各种类型的文档。 Aspose.Words 19.3版特别为JDK17设计,以确保与最新的Java开发环境兼容。这意味着您可以在使用JDK17的项目中无缝集成和使用Aspose.Words库,无需担心任何兼容性问题。 总而言之,Aspose.Words 19.3-jdk17.jar是一个功能强大的Java库,它为您提供了处理和操作Microsoft Word文档的能力。它支持多种功能,如创建,读取,修改和Word文档。此库与JDK17兼容,可以轻松地集成到Java项目中,并为您的文档处理需求提供强大的支持

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值