700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java 将doc格式的文档转化为pdf格式(实现Word文件预览)

java 将doc格式的文档转化为pdf格式(实现Word文件预览)

时间:2019-11-15 00:47:50

相关推荐

java  将doc格式的文档转化为pdf格式(实现Word文件预览)

doc格式的文档要实现预览功能,首先要将文档转化为pdf格式的文档

doc文档转pdf文档步骤

导入jar包

将aspose-words-jdk16-14.6.0.0.jar包导入项目中;

创建一个格式转化类

package com.test;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import com.aspose.words.Document;import com.aspose.words.License;import com.aspose.words.SaveFormat;public class BTest {public static void main(String[] args) { doc2pdf("C:/Users/Desktop/产品资质项目接口文档4.08.docx","C:/Users/Desktop/1.pdf"); } public static boolean getLicense() { boolean result = false; try { InputStream is = BTest.class.getClassLoader().getResourceAsStream("license.xml"); // license.xml应放在..\WebRoot\WEB-INF\classes路径下 License aposeLic = new License(); aposeLic.setLicense(is); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } public static void doc2pdf(String inPath, String outPath) { /*if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生 return; } */try { long old = System.currentTimeMillis(); File file = new File("C:/Users/Desktop/2.pdf"); // 新建一个空白pdf文档 FileOutputStream os = new FileOutputStream(file); Document doc = new Document(inPath); // Address是将要被转化的word文档 doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, // EPUB, XPS, SWF 相互转换 long now = System.currentTimeMillis(); System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时 } catch (Exception e) { e.printStackTrace(); } } }

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。