当前位置: 首页 > 知识库问答 >
问题:

Apache POI转换器,docx到pdf异常

唐利
2023-03-14

我需要添加表到现有的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.java:45)

但是如果只使用以下方法将我编辑的docx文档(W/O转换)写入文件:

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

所以我不明白为什么我会收到“PdfPTable构造函数中的列数必须大于零”。当我创建的表有3行3列时,出现异常。看起来像一些问题与pdf转换器或我创建表在错误的方式。也许有人能给我提点建议?

共有1个答案

沈宇定
2023-03-14

将包“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);
 类似资料:
  • 问题内容: 以下代码不适用于Apache poi 3.16。有人可以提供正确的解决方案吗,在我的项目中,有些人只能使用 例外: 问题答案: 这样做的主要问题是这些和不是项目的一部分。它们是由开发的,第一个版本的命名错误和。这些老班没有更新从2014年开始,需要版本 的 使用。 请使用更多 最新版本的fr.opensagres.poi.xwpf.converter.pdf,该版本可以使用最新的稳定版

  • 我在运行时生成了一个docx文档,我想将其转换为PDF,而无需实际将文件保存在本地 PdfConverter无效。我怎样才能做到这一点?

  • 我正在尝试将. docx文件转换为. pdf文件。现在我有以下代码: 以下是版本: 但是当我运行它时,我只得到一个空的pdf文件。我还得到以下堆栈跟踪: 我还应该做些什么?

  • 我正在尝试将包含表格和图像的文件转换为格式文件。 我四处寻找但没有得到妥善的解决方案,请求给出妥善正确的解决方案: 这里我尝试了: 请建议。 使用的罐子:

  • 我正在努力寻找用Python将PDF文件转换为.docx文件的方法。 我见过其他与此相关的帖子,但在我的情况下,它们似乎都不正常。 我特别使用 这给了我输出[1],但在我的文件夹中找不到任何.docx文档。 我已经安装了LibreOffice 5.3。 有什么线索吗? 提前谢谢你!