相关文章推荐
果断的汉堡包  ·  java - ...·  2 年前    · 
快乐的香菜  ·  spring boot 报错 Could ...·  2 年前    · 
聪明伶俐的柠檬  ·  深入理解 ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am doing a placeholder replacements in docx file and after that I need to convert file to PDF. All of my efforts are ending in

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).

I am using these dependencies:

    implementation("org.apache.poi:poi-ooxml:3.17")
implementation("fr.opensagres.xdocreport:fr.opensagres.xdocreport.converter.docx.xwpf:2.0.1")

If I try to convert source (unchanged) docx file, everything works as it should, but when I do replace placeholders and save document, everything crashes. Piece of my code:

        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();
                Can you please tell me what is wrong with my answer? It is not very polite simply not to react anymore when somebody tries helping you.
– Axel Richter
                Mar 9, 2019 at 11:26

The following works for me using the newest apache poi version 4.0.1 and the newest version 2.0.2 of fr.opensagres.poi.xwpf.converter.core and consorts.

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();
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.