java - 结果集从数据库到itextpdf

标签 java itext

我是 Java 新手,我使用 itextpdf 作为我的输出。
现在我对这个问题很慌张。

我的问题是我想以 pdf 格式显示数据库中的结果集。

例如,我将设置一个包含 3 列的表格,
这些是来自数据库的结果集,

姓名
约翰

玛丽
桑尼
基尔

所以现在 itextpdf 中的输出应该是这样的

|__列名_|__列名_|_列名__|
|_____约翰_____|_____简_____|_____玛丽___|
|_____桑尼_____|_____基尔_____|___________|


我希望将结果插入每一列,但我不知道该怎么做。

有人吗?如果有人可以指导我,那就太好了。

Document document = new Document(PageSize.A4, 25, 25, 25, 25);
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("D:\\PURCHASEORDER\\"+see+".pdf"));
document.open();
try {
    Class.forName(driver);
    conn = DriverManager.getConnection(url+db, user, pass);
    Statement st = conn.createStatement();
    String zero = dates.getSelectedItem().toString();
    String sql = "select name as hehe from names where  servedate = '"+zero+"'";
    pst=conn.prepareStatement(sql);
    rs=pst.executeQuery();

    Rectangle react = writer.getPageSize();
    PdfPTable table2 = new PdfPTable(new float[] { 3,3,3});
    table2.setTotalWidth(527);
    table2.getDefaultCell().setBorder(Rectangle.NO_BORDER);
    PdfPCell cell = new PdfPCell(new Paragraph(""));
    cell.setColspan(8);
    cell.setHorizontalAlignment(Element.ALIGN_LEFT);
    cell.setBackgroundColor(BaseColor.GRAY);
    table2.addCell(cell);
    table2.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);

    while(rs.next()){
        String v1 = rs.getString("hehe");
        FontFactory.getFont(FontFactory.TIMES_BOLD,14,BaseColor.BLACK)));
        table2.addCell(new Paragraph("TOTAL Number: "+v1+"", FontFactory.getFont(FontFactory.TIMES_ROMAN,12,BaseColor.BLACK)));
        table2.addCell(new Paragraph("TOTAL Number: "+v1+"", FontFactory.getFont(FontFactory.TIMES_ROMAN,12,BaseColor.BLACK)));
        table2.addCell(new Paragraph("TOTAL Number: "+v1+"", FontFactory.getFont(FontFactory.TIMES_ROMAN,12,BaseColor.BLACK)));

    }
    table2.setWidthPercentage(100);
    document.add(table2);

} catch (Exception e) {
    JOptionPane.showMessageDialog(null, e);
}
document.close();
String pdfFile="D:\\PURCHASEORDER\\"+see+".pdf";
File f = new File(pdfFile);
if (pdfFile.toString().endsWith(".pdf")) {
    Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + pdfFile);
} else {
    //For cross platform use
    Desktop desktop = Desktop.getDesktop();

    desktop.open(f);
}

最佳答案

这是错误的:

while(rs.next()){
    String v1 = rs.getString("hehe");
        FontFactory.getFont(FontFactory.TIMES_BOLD,14,BaseColor.BLACK)));
    table2.addCell(new Paragraph("TOTAL Number: "+v1+"", FontFactory.getFont(FontFactory.TIMES_ROMAN,12,BaseColor.BLACK)));
    table2.addCell(new Paragraph("TOTAL Number: "+v1+"", FontFactory.getFont(FontFactory.TIMES_ROMAN,12,BaseColor.BLACK)));
    table2.addCell(new Paragraph("TOTAL Number: "+v1+"", FontFactory.getFont(FontFactory.TIMES_ROMAN,12,BaseColor.BLACK)));
}

首先,让我们通过创建一个可以重用的 Font 对象来清理脏代码:

Font font = FontFactory.getFont(FontFactory.TIMES_ROMAN,12,BaseColor.BLACK);

其次,让我们解决您向每一行添加相同值的问题:

String v1;
while(rs.next()){
    v1 = rs.getString("hehe");
    table2.addCell(new Paragraph("TOTAL Number: " + v1, font));
}

最后,我们知道 iText 只会向表中添加完整行,而在您的示例中,Sonny 和 Kiel 只填充一行三行中的两个单元格,因此我们需要完成最后一行:

table2.completeRow();

现在我们可以将 table2 添加到文档中。

其他一些说明:

这是矫枉过正:

PdfPTable table2 = new PdfPTable(new float[] { 3,3,3});

如果您想要一个包含 3 列且每列具有相同宽度的列,则无需传递具有相等值的 float[],您可以这样做:

PdfPTable table2 = new PdfPTable(3);

这一行没有意义:

table2.setTotalWidth(527);

这没有意义,因为:

  1. 您没有锁定宽度,因此 527 的宽度将被忽略。您可以添加 table.setLockedWidth(true);
  2. 您还有 table2.setWidthPercentage(100);,它将宽度定义为可用宽度的 100%。

您必须选择其中之一:绝对宽度或相对宽度。两者都没有意义。请阅读How to define the width of a cell?

这是完全错误的:

cell.setColspan(8);

您正在定义一个包含3 列 的表格,因此您将colspan 设置为好像有8 列。在具有 3 列的表中,最大 colspan 为 3。

最后:

您是新手,并且正在使用 iText 5。为什么不使用 iText 7,因为您才刚刚开始? iText 7 是对 iText 5 的重写。它有一个不同的、更面向 future 的 API。表格说明here .

关于java - 结果集从数据库到itextpdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41214891/

相关文章:

java - 从 Android 应用程序在 openfire 上创建一个组

java - 为什么从方法返回时我的 ResultSet 变空?

java - 当文件是参数时无法删除它

java - 使用 ITextRenderer pdf 中没有中文单词

java - 如何将 ByteArrayOutputStream 表示的单页文档插入到另一个文档中?

java - 如何编写代码来计算数组中特定项的出现次数?

java - Exception 子类中 `Super` 关键字的影响

c# - 单元格背景颜色影响其他线条的颜色

vb.net - iTextSharp - 如何从项目资源输入图像(PNG)?

java - 如何仅重命名pdf中第一个找到的重复acrofield?