首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Apache POI和XDOCREPORT NullPointerException

Apache POI和XDOCREPORT NullPointerException
EN

Stack Overflow用户
提问于 2019-02-27 23:28:09
回答 1查看 1.9K关注 0票数 0

我正在做一个占位符替换在docx文件,之后我需要转换成PDF文件。我所有的努力都结束于

代码语言:javascript
复制
fr.opensagres.poi.xwpf.converter.core.XWPFConverterException: java.lang.NullPointerException

at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:71)
at fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:39)
at fr.opensagres.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:46).

我正在使用这些依赖项:

代码语言:javascript
复制
    implementation("org.apache.poi:poi-ooxml:3.17")
implementation("fr.opensagres.xdocreport:fr.opensagres.xdocreport.converter.docx.xwpf:2.0.1")

如果我尝试转换源(未更改的) docx文件,一切正常,但当我替换占位符并保存文档时,一切都崩溃了。我的一段代码:

代码语言:javascript
复制
        FileInputStream fis = new FileInputStream(COPIED);
        XWPFDocument doc = new XWPFDocument(fis);

        doc.createStyles();



        for (XWPFParagraph p : doc.getParagraphs()) {
            List<XWPFRun> runs = p.getRuns();
            if (runs != null) {
                for (XWPFRun r : runs) {
                    String text = r.getText(0);
                    StringSubstitutor substitutor = new StringSubstitutor(fieldsForReport);
                    String replacedText = substitutor.replace(text);
                    r.setText(replacedText, 0);
                }
            }
        }
        for (XWPFTable tbl : doc.getTables()) {
            for (XWPFTableRow row : tbl.getRows()) {
                for (XWPFTableCell cell : row.getTableCells()) {
                    for (XWPFParagraph p : cell.getParagraphs()) {
                        for (XWPFRun r : p.getRuns()) {
                            String text = r.getText(0);
                            StringSubstitutor substitutor = new StringSubstitutor(fieldsForReport);
                            String replacedText = substitutor.replace(text);
                            r.setText(replacedText, 0);
                        }
                    }
                }
            }
        }

        FileOutputStream fos = new FileOutputStream(COPIED);
        doc.write(fos);
        doc.close();



        FileInputStream fis = new FileInputStream(COPIED);
        XWPFDocument document = new XWPFDocument(fis);
        PdfOptions options = PdfOptions.create();
        PdfConverter converter = (PdfConverter) PdfConverter.getInstance();
        converter.convert(document, new FileOutputStream(DEST), options);

        document.close();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-01 02:18:05

使用最新的apache poi版本4.0.1fr.opensagres.poi.xwpf.converter.core和consorts的最新版本的2.0.2,我可以使用下面的代码。

代码语言:javascript
复制
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;

//needed jars: fr.opensagres.poi.xwpf.converter.core-2.0.2.jar, 
//             fr.opensagres.poi.xwpf.converter.pdf-2.0.2.jar,
//             fr.opensagres.xdocreport.itext.extension-2.0.2.jar,
//             itext-4.2.1.jar                                  
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;

//needed jars: apache poi and it's dependencies
//inclusive    ooxml-schemas-1.4.jar
import org.apache.poi.xwpf.usermodel.*;

public class DOCXToPDFConverterSampleMin {

 public static void main(String[] args) throws Exception {

  String docPath = "./WordDocument.docx";
  String pdfPath = "./WordDocument.pdf";

  InputStream in = new FileInputStream(new File(docPath));
  XWPFDocument document = new XWPFDocument(in);

  for (XWPFParagraph paragraph : document.getParagraphs()) {
   for (XWPFRun run : paragraph.getRuns()) {
    String text = run.getText(0);
    if (text != null && text.contains("$name$")) {
     text = text.replace("$name$", "Axel Richter");
     run.setText(text, 0);
    } else if (text != null && text.contains("$date$")) {
     text = text.replace("$date$", "2019-02-28");
     run.setText(text, 0);
    }
   }
  }
  for (XWPFTable table : document.getTables()) {
   for (XWPFTableRow row : table.getRows()) {
    for (XWPFTableCell cell : row.getTableCells()) {
     for (XWPFParagraph paragraph : cell.getParagraphs()) {
      for (XWPFRun run : paragraph.getRuns()) {
       String text = run.getText(0);
       if (text != null && text.contains("$name$")) {
        text = text.replace("$name$", "Axel Richter");
        run.setText(text,0);
       } else if (text != null && text.contains("$date$")) {
        text = text.replace("$date$", "2019-02-28");
        run.setText(text, 0);
       }
      }
     }
    }
   }
  }

  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run = paragraph.createRun();
  run.setText("This is new Text in this document.");

  PdfOptions options = PdfOptions.create();
  OutputStream out = new FileOutputStream(new File(pdfPath));
  PdfConverter.getInstance().convert(document, out, options);

  document.close();
  out.close();

 }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54908930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档