Java – 将Doc或Docx格式的Word文档转换为PDF文件

2,137 阅读3分钟

我们发送Word文档给别人的时候,可以考虑将文档转换为PDF文件,使其能够在不同的设备上呈现出相同的外观,同时也方便查看者在不安装MS Word的情况下查看。本文将演示如何通过Java将Word文档转换为PDF、PDF/A和密码保护的PDF文件。

本文包含以下内容:

  • Word文档转换为PDF格式
  • Word文档转换为PDF/A格式
  • Word文档转换为带密码的PDF文档

添加依赖项

方法一:Maven添加

如果你使用Maven,将下面的代码复制到项目中的pom.xml文件中,即可轻松添加JAR文件到你的应用程序中。

<repositories>

    <repository>

        <id>com.e-iceblue</id>

        <name>e-iceblue</name>

        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>

    </repository>

</repositories>

<dependencies>

    <dependency>

        <groupId>e-iceblue</groupId>

        <artifactId>spire.doc.free</artifactId>

        <version>5.2.0</version>

    </dependency>

</dependencies>

方法二:手动添加

如果没有使用Maven,可以从Free Spire.Doc for Java官网下载JAR文件,解压下载的zip文件后,将lib文件夹下的Spire.Doc.jar文件导入到你的项目中即可。

将Word文档转换为PDF格式

用Spire.Doc for Java将Word文档转换为PDF格式的操作非常简单,只需要下面的两个步骤:

  • 创建 Document 类的对象,并将Word文档的文件路径作为参数传给该类的构造函数。
  • 调用 Document.saveToFile(filePath, FileFormat.PDF) 方法,将文档保存为PDF格式。
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class wordToPDF {
    public static void main(String[] args) {
        //创建 Document 类的对象,并载入Word文档
        Document doc = new Document("示例.docx");

        //将文档保存为PDF格式
        doc.saveToFile("Word转PDF.pdf", FileFormat.PDF);
    }
}

2022-08-02_163349.png

将Word文档转换为PDF/A格式

PDF/A是一种特殊的PDF格式,用于长期保存电子文档。Spire.Doc for Java支持将Word转换为以下PDF/A文档:

  • PDF/A-1a
  • PDF/A-1b
  • PDF/A-2a
  • PDF/A-2b
  • PDF/A-2u
  • PDF/A-3a
  • PDF/A-3b
  • PDF/A-3u
  • PDF/x-1a:2001

以下是将Word文档转换为PDF/A文档的操作步骤:

  • 创建 Document 类的对象,并将Word文档的文件路径作为参数传递给该类的构造函数。
  • 创建 ToPdfParameterList 类的对象。
  • 使用 Topdfparameterlist.setpdfconformancelevel(PdfConformanceLevel) 方法设置PDF文档的一致性级别。
  • 调用 Document.saveToFile(filePath,ToPdfParameterList) 方法将Word文档保存为PDF格式。
import com.spire.doc.Document;
import com.spire.doc.ToPdfParameterList;
import com.spire.pdf.PdfConformanceLevel;

public class wordToPDFA {
    public static void main(String[] args) {
        //创建 Document 类的对象,并载入Word文档
        Document doc = new Document("示例.docx");

        //创建ToPdfParameterList类的对象
        ToPdfParameterList parameterList = new ToPdfParameterList();

        //设置PDF文档的一致性级别
        parameterList.setPdfConformanceLevel(PdfConformanceLevel.Pdf_A_1_A);

        //将文档保存为PDF/A格式
        doc.saveToFile("Word转PDFA.pdf", parameterList);
    }
}

2022-08-02_164141.png

将Word文档转换为带密码的PDF文档

你也可以在转换Word文档为PDF文档时对其加密,以下是操作步骤:

  • 创建 Document 类的对象,将Word文档的文件路径作为参数传给该类的构造函数。
  • 创建 ToPdfParameterList 类的对象。
  • ToPdfParameterList.getPdfSecurity().encrypt(openPassword, permissionPassword, PdfPermissionsFlags, PdfEncryptionKeySize) 方法为PDF设置打开密码和权限密码。
  • 调用 Document.saveToFile(filePath, ToPdfParameterList) 方法,将Word文档保存为PDF格式。
import com.spire.doc.Document;
import com.spire.doc.ToPdfParameterList;
import com.spire.pdf.security.PdfEncryptionKeySize;
import com.spire.pdf.security.PdfPermissionsFlags;

public class wordToPDFWithPassword {
    public static void main(String[] args) {
        //创建 Document 类的对象,并载入Word文档
        Document doc = new Document("示例.docx");

        //创建ToPdfParameterList类的对象
        ToPdfParameterList toPdf = new ToPdfParameterList();

        //设置PDF打开密码和权限密码
        String password = "password";
        toPdf.getPdfSecurity().encrypt(password, password, PdfPermissionsFlags.None, PdfEncryptionKeySize.Key_128_Bit);

        //将文档保存为带密码的PDF文档
        doc.saveToFile("Word转带密码PDF.pdf", toPdf);
    }
}

2022-08-02_164958.png

拓展

本文介绍了如何使用Free Spire.Doc for Java提供的Document.saveToFile()方法将Word转换为PDF。除了PDF,你还可以使用Document.saveToFile()方法将Word文档转换成其他文件格式,如Rtf, Html, Odt, Txt, Epub, PostScript, Xml, Svg, XPS等。