Java编程工具之 word转pdf
Posted 最小的帆也能远航
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java编程工具之 word转pdf相关的知识,希望对你有一定的参考价值。
1、依赖,或者直接下载aspose-words-15.8.0-jdk16-Java文档类资源-CSDN下载亲测可用更多下载资源、学习资料请访问CSDN下载频道.https://download.csdn.net/download/weixin_44364444/85500059,通过(project Structrue——Modeules——右边的Dependecies)把包引进来。通过这种方式引入aspose-words,pom中就无需引入
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>wordToPdf</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.7.RELEASE</version>
</dependency>
</dependencies>
</project>
2、证书 resource根目录下 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>
3、工具类
package com.xxh;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* @author xxh
* @date 2022/5/30 14:05
*/
public class Word2PdfAsposeUtil
/**
*
* @return
*/
public static boolean getLicense()
boolean result = false;
InputStream is = null;
try
Resource resource = new ClassPathResource("license.xml");
is = resource.getInputStream();
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
catch (Exception e)
e.printStackTrace();
finally
if (is != null)
try
is.close();
catch (IOException e)
e.printStackTrace();
return result;
/**
* @return boolean
* @Param [inPath, outPath]
**/
public static boolean doc2pdf(String inPath, String outPath)
// 验证License 若不验证则转化出的pdf文档会有水印产生
if (!getLicense())
return false;
FileOutputStream os = null;
try
long old = System.currentTimeMillis();
// 新建一个空白pdf文档
File file = new File(outPath);
os = new FileOutputStream(file);
// Address是将要被转化的word文档
Document doc = new Document(inPath);
// 全面支持DOC, DOCX, OOXML, RTF html, OpenDocument, PDF,
doc.save(os, SaveFormat.PDF);
// EPUB, XPS, SWF 相互转换
long now = System.currentTimeMillis();
// 转化用时
System.out.println("pdf转换成功,共耗时:" + ((now - old) / 1000.0) + "秒");
catch (Exception e)
e.printStackTrace();
return false;
finally
if (os != null)
try
os.flush();
os.close();
catch (IOException e)
e.printStackTrace();
return true;
4.测试类
import com.xxh.Word2PdfAsposeUtil;
import java.io.File;
import java.io.IOException;
/**
* @author xxh
* @date 2022/5/30 14:15
*/
public class Test
public static void main(String[] args)
String sourcePath="D:\\\\code\\\\wordToPdf\\\\src\\\\main\\\\resources\\\\Java知识点复习.docx";
String distPath="D:\\\\code\\\\wordToPdf\\\\src\\\\main\\\\resources\\\\Java知识点复习temp.pdf";
try
File file = new File(distPath);
if(!file.exists())
file.createNewFile();
Word2PdfAsposeUtil.doc2pdf(sourcePath,distPath);
catch (IOException e)
e.printStackTrace();
效果如图:
后缀名加上.pdf就可以了。控制台红色部分出现的原因:由于aspose比较吃内存,操作大一点的文件就会堆溢出,所以请先设置好java虚拟机参数:-Xms1024m -Xmx1024m(参考值)
以上是关于Java编程工具之 word转pdf的主要内容,如果未能解决你的问题,请参考以下文章