5

Java 将RTF转换为PDF格式

 2 years ago
source link: https://blog.51cto.com/u_15656056/5513606
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Java 将RTF转换为PDF格式

原创

RTF文件的出色之处在于它的灵活性。RTF文件的性质使其非常适合那些使用不同软件进行工作的人。当然,这并不意味着文件应该永远保存为RTF文件。 有时我们希望将内容“锁定”在RTF文件中,使其无法编辑。那我们只需要将RTF转换为PDF格式即可。本文将为大家演示如何通过Java将RTF转换为 PDF格式。以下是我整理的思路和步骤,并附上Java代码供大家参考。

Jar包引入

方法1:手动引入。将​ ​Free Spire.Doc for Java​​下载到本地,解压,找到lib文件夹下的Spire.Doc.jar文件。在IDEA中打开如下界面,将本地路径中的jar文件引入Java程序

Java 将RTF转换为PDF格式_PDF

方法2: 如果您想通过 ​ ​Maven​​安装,则可以在 pom.xml 文件中添加以下代码导入 JAR 文件。

<repositories>
<repository>
<id>com.e-iceblue</id>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>

将RTF转换为PDF格式

  • 实例化Document类的对象。
  • 调用Document.loadFromFile()方法加载RTF文档。
  • 调用Document.saveToFile()方法将RTF保存为PDF格式。

Java

import com.spire.doc.*;
public class RTFtoPDF {
public static void main(String[] args) {
//Create Document instance.
Document document = new Document();
//Load a sample RTF document
document.loadFromFile("西湖.rtf", FileFormat.Rtf);
//Save the document to PDF
document.saveToFile("西湖rtfToPdf.pdf", FileFormat.PDF);
}
Java 将RTF转换为PDF格式_RTF_02

测试代码中的文件路径为程序Debug路径,仅供参考,文件路径可自定义为其他路径。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK