首页 > 其他分享 >poi pdf 转word 中文不显示问题

poi pdf 转word 中文不显示问题

时间:2022-12-26 18:44:46浏览次数:51  
标签:map run get word XWPFDocument cell doc poi pdf

出错原因

  • 每一列没设置字体

代码

  • 生成word
  public static String FILE_SRC = "templates/xxx.docx";

    public XWPFDocument searchAndReplace(Map<String, String> map) throws GeneralServiceException, IOException {


        try {
            InputStream fileInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(FILE_SRC);

            XWPFDocument document = new XWPFDocument(fileInputStream);
            fileInputStream.close();
            /**
             * 替换段落中的指定文字
             */
            Iterator<XWPFParagraph> itPara = document.getParagraphsIterator();
            while (itPara.hasNext()) {
                XWPFParagraph paragraph = (XWPFParagraph) itPara.next();
                Set<String> set = map.keySet();
                Iterator<String> iterator = set.iterator();
                while (iterator.hasNext()) {
                    String key = iterator.next();
                    List<XWPFRun> run = paragraph.getRuns();
                    for (int i = 0; i < run.size(); i++) {
                        if (run.get(i).getText(run.get(i).getTextPosition()) != null &&
                                run.get(i).getText(run.get(i).getTextPosition()).equals(key)) {
                            /**
                             * 参数0表示生成的文字是要从哪一个地方开始放置,设置文字从位置0开始
                             * 就可以把原来的文字全部替换掉了
                             */
                            run.get(i).setText(map.get(key), 0);
                        }
                    }
                }
            }

            /**
             * 替换表格中的指定文字
             */
            Iterator<XWPFTable> itTable = document.getTablesIterator();
            while (itTable.hasNext()) {
                XWPFTable table = (XWPFTable) itTable.next();
                int count = table.getNumberOfRows();
                for (int i = 0; i < count; i++) {
                    XWPFTableRow row = table.getRow(i);
                    List<XWPFTableCell> cells = row.getTableCells();
                    for (XWPFTableCell cell : cells) {
                        for (Map.Entry<String, String> e : map.entrySet()) {
                            if (cell.getText().equals(e.getKey())) {
                          
                                cell.removeParagraph(0);
                                cell.setText(e.getValue());
                            }
                            //  忽略的核心问题
                            List<XWPFParagraph> paragraphs = cell.getParagraphs();
                            for (XWPFParagraph xwpfParagraph : paragraphs) {
                                List<XWPFRun> runs = xwpfParagraph.getRuns();
                                for (XWPFRun run : runs) {
                                    run.setFontFamily("宋体");
                                }
                            }
                        }

                    }
                }
            }
            return document;
        } catch (Exception e) {
            log.error("errrrr:{}", e);
            throw new GeneralServiceException(e);
        } finally {
        }
    }
  • 转pdf
 XWPFDocument doc = getDocument(xxxID);  //查数据 返回doc
        fr.opensagres.poi.xwpf.converter.pdf.PdfOptions options = PdfOptions.create();
        PdfConverter.getInstance().convert(doc, out, options);
  • 查数据
  private XWPFDocument getDocument(Long applyID) throws IOException {
   
        Map<String, String> map = exportWordUtils.setData( data );
        XWPFDocument doc = exportWordUtils.searchAndReplace(map);
        return doc;
    }
```java

标签:map,run,get,word,XWPFDocument,cell,doc,poi,pdf
From: https://www.cnblogs.com/smart-1/p/17006468.html

相关文章