Aspose实现word转换PDF

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_41620182/article/details/80731033
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import com.aspose.slides.License;
import com.aspose.words.Document;


public class Aspose_WordToPDF {


public Aspose_WordToPDF() {


}


private static boolean getLicense() {
boolean result = false;
try {
// 获取并配置license.xml
InputStream is = Aspose_WordToPDF.class.getClassLoader().getResourceAsStream("license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}


public static void doc2pdf(String wordPath, String pdfPath) {
// 验证License 若不验证则转化出的pdf文档会有水印产生
if (!getLicense()) {
boolean a = getLicense();
System.out.println(a);
return;
}
try {
boolean a = getLicense();
System.out.println(a);
// 转换开始前时间
long old = System.currentTimeMillis();
// 新建的PDF文件路径
File file = new File(pdfPath);
FileOutputStream os = new FileOutputStream(file);
// 要转换的word文档的路径
Document doc = new Document(wordPath);
// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
doc.save(os, com.aspose.words.SaveFormat.PDF);
// 转换结束后时间
long now = System.currentTimeMillis();
os.close();
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
doc2pdf("D:\\app\\download\\新建 Word 2007.doc", "D:\\app\\download\\新建 Word 2007.pdf");
}
}
展开阅读全文

没有更多推荐了,返回首页