zoukankan      html  css  js  c++  java
  • word转pdf

     
    /* 验证License*/
        private static boolean getLicense() {
            boolean result = false;
            try {
                InputStream is = PdfUtil.class.getClassLoader().getResourceAsStream("word/license.xml"); // license.xml应放在..WebRootWEB-INFclasses路径下
                License aposeLic = new License();
                aposeLic.setLicense(is);
                result = true;
                is.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
    
            }
            return result;
        }
    
        /**
         * @param wordPath 需要被转换的word全路径带文件名
         * @param pdfPath  转换之后pdf的全路径带文件名
         */
        public static void doc2pdf(String wordPath, String pdfPath) {
            if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
                return;
            }
            if (System.getProperty("os.name").toUpperCase().contains("linux")) {
                FontSettings.setFontsFolder(File.separator + "usr"
                        + File.separator + "share" + File.separator + "fonts" + File.separator + "Fonts", true);
            }
    
            try {
                long old = System.currentTimeMillis();
                File file = new File(pdfPath); //新建一个pdf文档
                FileOutputStream os = new FileOutputStream(file);
                Document doc = new Document(wordPath); //Address是将要被转化的word文档
    
                doc.save(os, com.aspose.words.SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
                long now = System.currentTimeMillis();
                os.close();
                System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); //转化用时
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    license.xml(注意存放的路径验证getLicense要引用)
    <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>


    jar包以及依赖

    链接:https://pan.baidu.com/s/1wjguGqizLkJraxgMkfOYYg
    提取码:60eg

  • 相关阅读:
    跟我extjs5(38--单个模块的设计[6获得模块列表数据])
    Visual Prolog 的 Web 专家系统 (8)
    ssh, maven and eclipse 那些破事
    在工厂模式
    IOS获取来电去电来电归属系统通知达到效果(一)
    基于CORS的geoserver同源访问策略
    springMVC1 springmvc的基础知识
    mybatis0212 mybatis逆向工程 (MyBatis Generator)
    mybatis0211 mybatis和spring整合
    mybatis0210 mybatis和ehcache缓存框架整合
  • 原文地址:https://www.cnblogs.com/Iron-1995/p/14365263.html
Copyright © 2011-2022 走看看