java创建文件
JAVA使⽤aspose实现word⽂档转pdf⽂件
引⼊jar包
下载地址:
然后打开下载的⽬录打开cmd执⾏
mvn install:install-file -Dfile=aspose-words-15.8.0-jdk16.jar -DgroupId=com.aspose -DartifactId=aspose-words -Dversion=15.8.0 -Dpackaging=jar
这是把jar包安装到本地仓库中
这样在pom⽂件⾥引⼊
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>15.8.0</version>
</dependency>
当然也可以直接使⽤jar包
然后在项⽬根⽬录中创建⼀个⽂件(SpringBoot项⽬直接在resources下)
<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>
使⽤⼯具类
AsposeUtil.java
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
/**
* @author yvioo。
*/
public class AsposeUtil {
/**
* 验证License 若不验证则转化出的pdf⽂档会有⽔印产⽣
* @return
*/
public boolean getLicense() {
boolean result = false;
try {
InputStream is =Class().getClassLoader().getResourceAsStream("l");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) throws Exception {
AsposeUtil bean = new AsposeUtil();
bean.word2Pdf2("C:\\1.docx","D:\\TEST.pdf");
}
/**
* word转pdf
* inpath: 输⼊word的路径
* outpath: 输出pdf的路径
*/
public void word2Pdf2(String inpath,String outpath) throws Exception {
if (!getLicense()) {
System.out.println("⾮法------------");
return;
}
long old = System.currentTimeMillis();
File file = new File(outpath);
FileOutputStream os = new FileOutputStream(file);
//解决乱码
//如果是windows执⾏,不需要加这个
/
/TODO 如果是linux执⾏,需要添加这个*****
//FontSettings.setFontsFolder("/usr/share/fonts",true);
Document doc = new Document(inpath);
//全⾯⽀持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
doc.save(os, SaveFormat.PDF);
long now = System.currentTimeMillis();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
}
/**
* word转pdf
* @param path      pdf输出路径
* @param wordInput word输⼊流
* @param wordName  word⽂档的名称
*/
public void word2pdf(String path, InputStream wordInput, String wordName) throws FileNotFoundException { if (!getLicense()) {
System.out.println("⾮法");
return;
}
//新建⼀个空⽩pdf⽂档
long old = System.currentTimeMillis();
File file = new File(path + wordName + ".pdf");
FileOutputStream os = new FileOutputStream(file);
//Address是将要被转化的word⽂档
Document doc = null;
try {
doc = new Document(wordInput);
} catch (Exception e) {
e.printStackTrace();
}
try {
//全⾯⽀持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
doc.save(os, SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
}
long now = System.currentTimeMillis();
//转化⽤时
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
}
}