- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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);
这没有意义,因为:
527
的宽度将被忽略。您可以添加 table.setLockedWidth(true);
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 创建一个简单的程序,其中有一个按钮,单击后即可创建 PDF。我正在使用 iText JAVA 库来生成 PDF。我已将 iText .jar 文件包含在“Java 构建路径”中,但
在最新版本的 itextpad v5.4.0 中找不到类“com.itextpdf.text.pdf.PdfAWriter”。我什至尝试下载额外的 jar,但无济于事。 我们将不胜感激。 最佳答案 我
我是 android 的新手,我正在尝试在 android 中将位图转换为 Pdf。我正在使用 itextpdf 5.5.4 jar 文件。我使用的代码如下: import com.itextpdf.
我使用以下代码的目的是将 HTML 文件转换为 PDF try { com.itextpdf.text.Document document = new com.itextpdf.text
我正在尝试将表格放在已在“PdfPageEventHelper”中横向旋转的 PDF 页面上: public void onStartPage(PdfWriter writer_,Document d
嘿哟,我的这段代码有问题: private void createPDF(String[] url, String name) throws FileNotFoundException, Docu
我在使用 itext 7 时遇到两个问题。其中一个是,如果您引入的链接长于一行,则第一行将显示为其自己的链接,只有第二行引用整个内容。第二个问题是关于在链接周围添加边框。该边框仅出现在链接的后半部分(
iTextPdf 是否允许设置表格中单元格之间的间距? 我有一个包含 2 列的表格,我正在尝试在单元格上绘制边框底部。我希望每个边框之间的空间与单元格填充相同。 我正在使用下面的代码: Pdf
我有一堆 PDF,此时我已将它们合并到代码中。在合并的 PDF 的开头,我有一个内容页面,分别链接到所述 PDF。这些 pdfAction.gotoLocalPage 链接有时无法正常工作,而是跳转到
以下是我使用 iText 从 Java 生成 PDF 的代码: import java.io.File; import java.io.FileOutputStream; import java.io
是否可以通过 iTextPDF 库从现有的 pdf 文件中提取页面并将整个页面另存为图像。 例如,如果我的 pdf 文件包含 2 个页面,那么将生成 2 个图像,每个图像将是特定页面的快照。 最佳答案
大家好! 我对文档有一些疑问: itextpdf 是否可以选择在特定页面上设置不同的边距?我试过用 document.setMargin 然后 document.newPage 但似乎每一页都有相同的
我在 Ubuntu 上使用 itextpdf 来填写带有数据的 PDF 表单。事实上,我使用了这个工具包中包含的一个类(实用程序):PdfFormFiller。数据由 Unicode 字符串表示。为了
我想动态对齐 iText PdfTable。 如何在 iTextPDF 中设置基于 x 和 y 位置的对齐方式。 PdfPCell cell; cell = new PdfPCell(testTabl
我正在使用 iTextPdf 为 pdf 构建表格。每页将有 9 到 15 列,确切数字直到运行时才知道。 iTextPDF 非常适合在整个页面宽度上创建大小相同的列。但我想不通的是如何创建不同宽度的
我使用 iTextPdf 从 html 页面生成 pdf 文件,例如: iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp
本文整理了Java中com.itextpdf.text.ZapfDingbatsList类的一些代码示例,展示了ZapfDingbatsList类的具体用法。这些代码示例主要来源于Github/Sta
如何在 iTextpdf 中设置表格的行位置和宽度?这里我需要如何设置每个单元格的位置以及如何在 itextpdf 中设置行间距 testTable11_close_right.getDefaultC
我正在使用 itextpdf 将一些 pdf 合并为一个。 itextpdf pdfcontentbyte addtemplate的参数是什么意思,没有文档描述。 最佳答案 下面的代码成功了,感谢帮助
我有这个移动应用程序,我需要将数据库的一些行导出为 pdf。我需要有一个 pdf 结构才能正确放置信息。这是我的代码,但由于某种原因 PDF 是空白的。主要目标是在开头有一个标题,然后是数据库中的一行
我是一名优秀的程序员,十分优秀!