【问题标题】:Trying to print ResultSet to PDF file尝试将 ResultSet 打印到 PDF 文件
【发布时间】:2018-09-03 11:10:31
【问题描述】:

我发现了这个名为 Boxable 的 API,它也使用 PDFBox,现在我正在尝试在线实现示例但无法使其工作。 这是我的代码。

public static ArrayList <String[]> result = new ArrayList<String[]>();

我只是像示例中一样复制了这个函数

private static PDPage addNewPage(PDDocument doc) {
    PDPage page = new PDPage();
    doc.addPage(page);
    return page;
}

这些是我主要功能的一部分

对于我的查询执行

        Connection con = null;
        PreparedStatement ps = null;
        String query = "SELECT * FROM tablename";
        try{
            con = getConnection();
            ps = con.prepareStatement(query);
            ResultSet rs = ps.executeQuery();
            int columnCount = rs.getMetaData().getColumnCount();
            while(rs.next()){
                String[] row = new String[columnCount];
                for (int i=0;i<columnCount;i++){
                    row[i] = rs.getString(i+1);
                }
                result.add(row);
            }
        } catch (SQLException ex) {
        Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        }

表格生成部分

        try{
            float margin = 10;
            PDDocument doc = new PDDocument();
            PDPage page = addNewPage(doc);
            float tableWidth = page.getMediaBox().getWidth()-(2*margin);
            float yStartNewPage = page.getMediaBox().getHeight()-(2*margin);
            boolean drawContent = true;
            boolean drawLines =  true;
            float yStart = yStartNewPage;
            float bottomMargin = 70;
            BaseTable table = new BaseTable(yStart,yStartNewPage,bottomMargin,tableWidth,margin,doc,page,drawLines,drawContent);
            Row<PDPage> headerRow = table.createRow(15f);
            Cell<PDPage> cell = headerRow.createCell(100,"Sample Table");
            cell.setFont(PDType1Font.HELVETICA_BOLD);
            cell.setFillColor(Color.BLACK);
            table.addHeaderRow(headerRow);
            for (String[] print : result){
                Row<PDPage> row = table.createRow(10f);
                cell = row.createCell((100/3.0f)*2,print[0]);
                for (int i=1;i<print.length;i++){
                    cell = row.createCell((100/9f),print[i]);
                }
            }
            table.draw();
            doc.save(new File("java\\sample.pdf"));
            doc.close();
        } catch (IOException ex) {
        Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
        }

编译器给了我这个异常

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at be.quodlibet.boxable.utils.FontUtils.<clinit>(FontUtils.java:23)
at be.quodlibet.boxable.Cell.<init>(Cell.java:110)
at be.quodlibet.boxable.Cell.<init>(Cell.java:71)
at be.quodlibet.boxable.Row.createCell(Row.java:51)
at connect.NewClass.main(NewClass.java:115)

115上面的线路是Cell&lt;PDPage&gt; cell = headerRow.createCell(100,"Sample Table");

我真的不知道出了什么问题,而且对 Java 还是很陌生。还尝试阅读类的代码,但无法检测到错误。 非常感谢您对此的意见。

【问题讨论】:

    标签: java mysql netbeans pdfbox boxable


    【解决方案1】:

    NoClassDefFoundError 表示您的类路径中没有引用的依赖项。这通常可以通过使用 Maven 等依赖管理器或手动将 jar 复制到项目根目录来解决。

    具体来说,在您的问题中,您在编译时缺少 org.slf4j.LoggerFactory 包。

    【讨论】:

    • 看来slf4j也是个API,我就下载下来试试,放在classpath里面。
    • 非常感谢,我将其包含在内并解决了问题。
    • 虽然它仍然给了我这个。 SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”。 SLF4J:默认为无操作 (NOP) 记录器实现 SLF4J:有关详细信息,请参阅 slf4j.org/codes.html#StaticLoggerBinder
    • 我不确定。您必须查看您刚刚引用的文档。
    猜你喜欢
    • 1970-01-01
    • 2013-07-01
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多