【问题标题】:Apache POI converter, docx to pdf exceptionApache POI 转换器,docx 到 pdf 异常
【发布时间】:2017-03-17 16:06:06
【问题描述】:

我需要将表格添加到现有的 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"));

它向我展示了里面的格式良好的表格:

所以我无法弄清楚为什么我收到“PdfPTable 构造函数中的列数必须大于零。”异常,当我创建的表有 3 行和 3 列时。 看起来 pdf 转换器有问题,或者我以错误的方式创建表格。 也许有人可以向我提出一些建议?

【问题讨论】:

标签: java apache pdf apache-poi docx


【解决方案1】:

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

在这部分:

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

【讨论】:

    猜你喜欢
    • 2020-12-13
    • 2016-03-27
    • 2011-11-28
    • 2019-12-20
    • 2019-02-19
    • 1970-01-01
    • 2018-12-28
    • 2018-06-28
    相关资源
    最近更新 更多