java代码将word、excel文档转换成pdf

 时间:2026-02-18 18:10:03

1、新建javaweb工程

下载aspouse-word-14.11.0-jdk16.jar包和aspose-cells-8.5.2.jar包并引入

word转pdf的java函数

package webViewer;

import java.io.*; 

import com.aspose.words.*;         //引入aspouse-word-14.11.0-jdk16.jar包

public class Word2Pdf { 

public static boolean getLicense() {       

boolean result = false;       

try {           

InputStream is = Test.class.getClassLoader().getResourceAsStream("wordlicense.xml"); //  wordlicense.xml应放在..\WebRoot\WEB-INF\classes路径下           

com.aspose.words.License aposeLic = new com.aspose.words.License();           

aposeLic.setLicense(is);           

 result = true;       

 }

catch (Exception e) {                          

e.printStackTrace();       

}       

return result;   

}  

public static void word2pdf(String Address) {               

 if (!getLicense()) {          // 验证License 若不验证则转化出的PDP文档会有水印产生           

 return;       

 try {              

File file = new File("C:/inetpub/wwwroot/web/file/pdf1.pdf");  //新建一个空白pdf文档           

FileOutputStream os = new FileOutputStream(file);           

Document doc = new Document(Address);                    //Address是将要被转化的word文档           

doc.save(os, SaveFormat.PDF);                            //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换            os.close();       

}

catch (Exception e) {           

e.printStackTrace();       

}    

}

}

2、excel转换成pdf的java函数

package webViewer;

import java.io.*; 

import com.aspose.cells.*;      //引入aspose-cells-8.5.2.jar包

public class Excel2Pdf {

public static boolean getLicense() {       

boolean result = false;       

try {           

InputStream is = Test.class.getClassLoader().getResourceAsStream("xlsxlicense.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 excel2pdf(String Address) {               

if (!getLicense()) {          // 验证License 若不验证则转化出的pdf文档会有水印产生           

return;       

}       

try {           

File pdfFile = new File("C:/inetpub/wwwroot/web/file/pdf1.pdf");// 输出路径           

Workbook wb = new Workbook(Address);// 原始excel路径                        FileOutputStream fileOS = new FileOutputStream(pdfFile);           

wb.save(fileOS, SaveFormat.PDF);             

fileOS.close();                 

}

catch (Exception e) {           

e.printStackTrace();       

}   

}

}

3、再利用测试函数进行检测

package webViewer;public class Test { public static void main(String[] args){

 Word2Pdf.word2pdf("C:/inetpub/wwwroot/web/file/4.docx");  

 Excel2Pdf. excel2pdf("C:/inetpub/wwwroot/web/file/5.xlsx"); }}

4、还可以将函数运用到jsp页面再使用pdf.js插件实现页面预览

  • PL/SQL Developer过滤表
  • idea如何给项目添加jdk
  • myeclipse怎么使用svn提交代码
  • eclipse怎么打开svn的窗口
  • tomcat乱码在server.xml里怎么修改
  • 热门搜索
    谒怎么读 不倒翁怎么做 疖怎么读 雪莲果怎么吃 鱿鱼丝怎么做好吃 养老保险怎么买最划算 笔记本电脑连不上无线网怎么回事 留言条格式怎么写 方便面怎么做好吃 瘦身汤的做法大全