Java ${}修改world内容 并转pdf,亲测可以

@world转pdf

前期准备工作:

1.先将world要修改的内容用 $ 表示
2.将world 另存为xml,再将xml文件后缀名改为.ftl
3.将用编辑器打开ftl文件,将修改的内容用${name}补充完整

java代码编写

要修改的内容参数

private static Map initWordMap() {
        Map wordData = new HashMap();
        wordData.put("contractId", "ht00011");
        wordData.put("person", "作者1111");
        wordData.put("userName", "张三22222");
        wordData.put("account", "账号001");
        wordData.put("sYear", "2022");
        wordData.put("sMonth", "10");
        wordData.put("sDay", "23");
        wordData.put("eYear", "2032");
        wordData.put("eMonth", "10");
        wordData.put("eDay", "23");
        wordData.put("bigMoney", "贰仟");
        wordData.put("smellMoney", "27890");
        wordData.put("signYear", "2022");
        wordData.put("signMonth", "11");
        wordData.put("signDay", "16");
        return wordData;
    }

** 修改world内容**

public static void downloadWord(Map wordData) throws Exception {
        String docName = "";
        Writer writer = null;
        String wordModePath = "F:\\";
        String wordModeFile ="财务合同.ftl";
        // 2. 设置配置内容
        // 设置版本
        Configuration configuration = new Configuration(new Version(VERSION));
        // 指定加载Word模板的路径
        configuration.setDirectoryForTemplateLoading(new File(wordModePath));
        // 以UTF-8的编码格式,读取模板文档
        Template template = configuration.getTemplate(wordModeFile, CHARSET);
        String contractId = wordData.get("contractId").toString();//合同编号
        // 3. 输出文档路径及名称
        docName = wordModePath+contractId+"_"+System.currentTimeMillis()  + ".doc";
        File outFile = new File(docName);
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), CHARSET), 10240);
        // 输出
        template.process(wordData, writer);
        writer.flush();
        writer.close();
        System.out.println(docName+":导出已完成");
        String pdfName = wordModePath+contractId+"_"+System.currentTimeMillis()  + ".pdf";
        worldToPdf(docName,pdfName);
    }

world 转pdf
path:world 文件的存放路径
path2: pdf文件的存放路径

private static Boolean worldToPdf(String path,String path2) {
        Boolean flag = false;
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(path);
            XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);
            PdfOptions pdfOptions = PdfOptions.create();
            fileOutputStream = new FileOutputStream(path2);
            PdfConverter.getInstance().convert(xwpfDocument,fileOutputStream,pdfOptions);
            flag = true;
            System.out.println("生成成功!"+path2);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try{
                if(fileInputStream != null) fileInputStream.close();
                if(fileOutputStream != null) fileOutputStream.close();
            }catch (IOException e){e.printStackTrace();}
        }
        return flag;
    }

全部代码

{

    /**
     * 设置使用的编码格式
     */
    private static final String CHARSET = "UTF-8";
    /**
     * 设置使用的版本
     */
    private static final String VERSION = "2.3.0";

    public static void main(String[] args) throws Exception{
        //将world转为pdf
        downloadWord("F:\\","财务合同。ftl");
    }

    public static void downloadWord(String wordModePath,String wordModeFile) throws Exception {
        String docName = "";
        Writer writer = null;
        // 2. 设置配置内容
        // 设置版本
        Configuration configuration = new Configuration(new Version(VERSION));
        // 指定加载Word模板的路径
        configuration.setDirectoryForTemplateLoading(new File(wordModePath));
        // 以UTF-8的编码格式,读取模板文档
        Template template = configuration.getTemplate(wordModeFile, CHARSET);
        // 3. 输出文档路径及名称
        docName = wordModePath+"_"+System.currentTimeMillis()  + ".doc";
        File outFile = new File(docName);
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), CHARSET), 10240);
        // 输出
        template.process(initWordMap(), writer);
        writer.flush();
        writer.close();
        System.out.println(docName+":导出已完成");
        String pdfName = wordModePath+System.currentTimeMillis()  + ".pdf";
        worldToPdf(docName,pdfName);
    }


    private static Boolean worldToPdf(String path,String path2) {
        Boolean flag = false;
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(path);
            XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);
            PdfOptions pdfOptions = PdfOptions.create();
            fileOutputStream = new FileOutputStream(path2);
            PdfConverter.getInstance().convert(xwpfDocument,fileOutputStream,pdfOptions);
            flag = true;
            System.out.println("生成成功!"+path2);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try{
                if(fileInputStream != null) fileInputStream.close();
                if(fileOutputStream != null) fileOutputStream.close();
            }catch (IOException e){e.printStackTrace();}
        }
        return flag;
    }

    private static Map initWordMap() {
        Map wordData = new HashMap();
        wordData.put("contractId", "ht00011");
        wordData.put("person", "作者1111");
        wordData.put("userName", "张三22222");
        wordData.put("account", "账号001");
        wordData.put("sYear", "2022");
        wordData.put("sMonth", "10");
        wordData.put("sDay", "23");
        wordData.put("eYear", "2032");
        wordData.put("eMonth", "10");
        wordData.put("eDay", "23");
        wordData.put("bigMoney", "贰仟");
        wordData.put("smellMoney", "27890");
        wordData.put("signYear", "2022");
        wordData.put("signMonth", "11");
        wordData.put("signDay", "16");
        return wordData;
    }
}

你可能感兴趣的:(编程软件,java,pdf,开发语言)