我需要将表添加到现有docx文档中,然后将其转换为Pdf文件,因此我使用Apache POI和Apache POI转换器库。
有我的代码:

import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.apache.xmlbeans.XmlCursor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGrid;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
....
public static void main(String[] args) throws Exception {
   FileInputStream fis = new FileInputStream("e:\\projects\\1.docx");
   XWPFDocument doc = new XWPFDocument(OPCPackage.open(fis));
   fis.close();
   XWPFTable table = doc.createTable();

//added to satisfy poi docx->pdf converter and avoid future npe on getCTTbl().getTblGrid()...
   CTTblGrid ctg = table.getCTTbl().getTblGrid();
   table.getCTTbl().setTblGrid(ctg);

   fillTable(table);

   OutputStream pdfFile = new FileOutputStream(new File("e:\\projects\\1.pdf"));
   PdfOptions options= PdfOptions.create().fontEncoding("UTF-8");
   PdfConverter.getInstance().convert(doc, pdfFile, options);
}

private static XWPFTable fillTable(XWPFTable table) {
        //create first row
        XWPFTableRow tableRowOne = table.getRow(0);
        tableRowOne.getCell(0).setText("col one, row one");
        tableRowOne.getCell(1).setText("col two, row one");
        tableRowOne.getCell(2).setText("col three, row one");
        //create second row
        XWPFTableRow tableRowTwo = table.getRow(1);
        tableRowTwo.getCell(0).setText("col one, row two");
        tableRowTwo.getCell(1).setText("col two, row two");
        tableRowTwo.getCell(2).setText("col three, row two");
        //create third row
        XWPFTableRow tableRowThree = table.getRow(2);
        tableRowThree.getCell(0).setText("col one, row three");
        tableRowThree.getCell(1).setText("col two, row three");
        tableRowThree.getCell(2).setText("col three, row three");

        //align center
        CTTblPr tblPr = table.getCTTbl().getTblPr();
        CTJc jc = (tblPr.isSetJc() ? tblPr.getJc() : tblPr.addNewJc());
        jc.setVal(STJc.CENTER);

        //added to satisfy poi docx->pdf converter and avoid npe on getTcPr().getWidth()
        for(int i = 0; i < table.getNumberOfRows(); i++){
            XWPFTableRow row = table.getRow(i);
            int numCells = row.getTableCells().size();
            for(int j = 0; j < numCells; j++){
                XWPFTableCell cell = row.getCell(j);

                CTTcPr ct = cell.getCTTc().getTcPr();
                cell.getCTTc().setTcPr(ct);

            }
        }

        return table;
    }


但是我收到这样的异常:


  org.apache.poi.xwpf.converter.core.XWPFConverterException:
  java.lang.IllegalArgumentException:PdfPTable中的列数
  构造函数必须大于零。在
  org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:70)
    在
  org.apache.poi.xwpf.converter.pdf.PdfConverter.doConvert(PdfConverter.java:38)
    在
  org.apache.poi.xwpf.converter.core.AbstractXWPFConverter.convert(AbstractXWPFConverter.java:45)


但是,如果仅使用以下命令将编辑后的docx文档(不进行转换)写入文件:

doc.write(new File("e:\\projects\\1.docx"));


它向我显示了格式正确的表格:
java - Apache POI转换器,docx至pdf异常-LMLPHP

因此,我无法弄清楚为什么收到“ PdfPTable构造函数中的列数必须大于零。”例外,当我创建的表有3行3列时。
看起来与pdf转换器有关,或者我以错误的方式创建表。
也许有人可以给我一些建议?

最佳答案

将软件包“ org.apache.poi.xwpf.converter.pdf”更改为软件包“ fr.opensagres.poi.xwpf.converter.pdf”。

在这一部分:

 PdfOptions options= PdfOptions.create().fontEncoding("UTF-8");
 PdfConverter.getInstance().convert(doc, pdfFile, options);


至。:

  fr.opensagres.poi.xwpf.converter.pdf.PdfOptions options = fr.opensagres.poi.xwpf.converter.pdf.PdfOptions.create().fontEncoding("windows-1250");
  fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.getInstance().convert(document, pdfFile, options);

07-24 22:15