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

  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. 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. 4

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

我的项目找不到了,通过网上搜索找了一个不确定可以不;仅供参考:

 

我找到的资料地址:https://www.cnblogs.com/qiwu1314/p/6121696.html

我有收藏,地址无法使用评论区告诉我;

 

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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值