免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
java poi操作word轉(zhuǎn)pdf

替換word文檔內(nèi)容


package com.docx.test;

import org.apache.poi.xwpf.usermodel.*;
import org.junit.Test;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DocxUnitl {

    /**
     * 用一個(gè)docx文檔作為模板,然后替換其中的內(nèi)容,再寫(xiě)入目標(biāo)文檔中。
     * @throws Exception
     */
    @Test
    public void testTemplateWrite() throws Exception {

        String pdfPath = "D:\\HHKJ\\project\\test\\lgwdha.pdf";
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("county", "桂林");
        params.put("time", "2019年1月15日");
        params.put("time2", "2019年1月16日");
        params.put("str", "具體整改要求,不管多少個(gè)字");
        params.put("time3", "2019年1月15日");

        String filePath = "D:\\HHKJ\\project\\test\\lgwdh.docx";
        InputStream is = new FileInputStream(filePath);
        XWPFDocument doc = new XWPFDocument(is);
        //替換段落里面的變量
        this.replaceInPara(doc, params);
        //替換表格里面的變量
//        this.replaceInTable(doc, params);
        OutputStream os = new FileOutputStream("D:\\HHKJ\\project\\test\\lgwdha.docx");
        doc.write(os);

        this.close(os);
        this.close(is);
    }


    /**
     * 替換段落里面的變量
     * @param doc 要替換的文檔
     * @param params 參數(shù)
     */
    private void replaceInPara(XWPFDocument doc, Map<String, Object> params) {
        Iterator<XWPFParagraph> iterator = doc.getParagraphsIterator();
        XWPFParagraph para;

        while (iterator.hasNext()) {
            para = iterator.next();
//            CTPPr pr = para.getCTP().getPPr();
            this.replaceInPara(para, params);
        }
    }

    /**
     * 替換段落里面的變量
     * @param para 要替換的段落
     * @param params 參數(shù)
     */
    private void replaceInPara(XWPFParagraph para, Map<String, Object> params) {
        List<XWPFRun> runs;
        Matcher matcher;
        if (this.matcher(para.getParagraphText()).find()) {
            runs = para.getRuns();
            System.out.println("2:"+runs);
            for (int i=0; i<runs.size(); i++) {
                XWPFRun run = runs.get(i);
                String runText = run.toString();
                matcher = this.matcher(runText);
                if (matcher.find()) {
                    while ((matcher = this.matcher(runText)).find()) {
                        runText = matcher.replaceFirst(String.valueOf(params.get(matcher.group(1))));
                    }
                    //直接調(diào)用XWPFRun的setText()方法設(shè)置文本時(shí),在底層會(huì)重新創(chuàng)建一個(gè)XWPFRun,把文本附加在當(dāng)前文本后面,
                    //所以我們不能直接設(shè)值,需要先刪除當(dāng)前run,然后再自己手動(dòng)插入一個(gè)新的run。

                    run.setText(runText,0);
//                    int fontSize = run.getFontSize();
//                    String fontFamily = run.getFontFamily();
//                    para.removeRun(i);
//                    para.insertNewRun(i).setText(runText);
//                    para.insertNewRun(i).setFontSize(fontSize);
//                    para.insertNewRun(i).setFontFamily(fontFamily);
                }
            }
        }
    }

    /**
     * 替換表格里面的變量
     * @param doc 要替換的文檔
     * @param params 參數(shù)
     */
    private void replaceInTable(XWPFDocument doc, Map<String, Object> params) {
        Iterator<XWPFTable> iterator = doc.getTablesIterator();
        XWPFTable table;
        List<XWPFTableRow> rows;
        List<XWPFTableCell> cells;
        List<XWPFParagraph> paras;
        while (iterator.hasNext()) {
            table = iterator.next();
            rows = table.getRows();
            for (XWPFTableRow row : rows) {
                cells = row.getTableCells();
                for (XWPFTableCell cell : cells) {
                    paras = cell.getParagraphs();
                    for (XWPFParagraph para : paras) {
                        this.replaceInPara(para, params);
                    }
                }
            }
        }
    }

    /**
     * 正則匹配字符串
     * @param str
     * @return
     */
    private Matcher matcher(String str) {
        Pattern pattern = Pattern.compile("\\{(.+?)\\}", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(str);
        return matcher;
    }

    /**
     * 關(guān)閉輸入流
     * @param is
     */
    private void close(InputStream is) {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 關(guān)閉輸出流
     * @param os
     */
    private void close(OutputStream os) {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

word轉(zhuǎn)pdf


package com.docx.test;

import java.io.*;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class word2pdf {

    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     */
    public static void main(String[] args) throws IOException {
        String docPath = "D:\\HHKJ\\project\\test\\lgwdha.docx";
        String pdfPath = "D:\\HHKJ\\project\\test\\lgwdha.pdf";

        XWPFDocument document;
        InputStream doc = new FileInputStream(docPath);
        document = new XWPFDocument(doc);
        PdfOptions options = PdfOptions.create();
        OutputStream out = new FileOutputStream(pdfPath);
        PdfConverter.getInstance().convert(document, out, options);

        doc.close();
        out.close();
    }

}

所需jar包
dom4j-1.6.1-hudson-1.jar
itext-4.2.0.jar
itext-asian-5.2.0.jar
itext-asiancmaps-5.1.1.jar
itextpdf-5.4.0.jar
jsoup-1.11.3.jar
ooxml-schemas-1.1.jar
org.apache.poi.xwpf.converter.core-1.0.4.jar
org.apache.poi.xwpf.converter.pdf-1.0.4.jar
xdocreport-2.0.1.jar
xmlbeans-5.3.0-rc1.jar
xmlgraphics-commons-2.2.jar

poi-3.9-20121203.jar
poi-examples-3.9-20121203.jar
poi-excelant-3.9-20121203.jar
poi-ooxml-3.9-20121203.jar
poi-scratchpad-3.9-20121203.jar

所遇問(wèn)題
1.java.lang.ClassNotFoundException: org/openxmlformats/schemas/wordprocessingml/x2006/main/FontsDocument$Factory
原博地址:https://blog.csdn.net/lex1993/article/details/47062141
解決辦法:導(dǎo)入ooxml-schemas-1.1.jar這個(gè)包,去掉poi-ooxml-3.9-20121203.jar
2.jar包版本問(wèn)題
解決辦法:因?yàn)轫?xiàng)目環(huán)境需要用jdk1.6,按照上述所示版本下載即可

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
POI讀寫(xiě)Word docx文件
html 生成PDF
POI 使用 HWPFDocument 和 XWPFDocument 分割 word2003 和 word2010示例
Java程序員從笨鳥(niǎo)到菜鳥(niǎo)之(一百零三)java操作office和pdf文件(一)java讀取word,excel和pdf文檔內(nèi)容
java 往 pdf 插入數(shù)據(jù) (pdfbox+poi)
PoiDemo【Android將表單數(shù)據(jù)生成Word文檔的方案之二(基于Poi4.0.0)】
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服