[英]TABLE from database to ITEXT7 PDF generated with 3 column page setting
下面是我在三列页面设置中设置pdf的代码,现在在“在此处插入代码”中,我想从数据库中插入表格。
例如 水果清单+价格。
那我该怎么办呢?
public void createPdf(String dest) throws IOException {
//Initialize PDF document
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
// Initialize document
Document document = new Document(pdf);
//Set column parameters
Rectangle[] columns = {
new Rectangle(20, 15, 175, 802),
new Rectangle(207, 15, 175, 802),
new Rectangle(394, 15, 175, 802) };
document.setRenderer(new ColumnDocumentRenderer(document, columns));
PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
PdfFont bold = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
document.setTextAlignment(TextAlignment.JUSTIFIED)
.setFont(font)
.setHyphenation(new HyphenationConfig("en", "uk", 3, 3));
????
???? INSERT CODE HERE
//Close document
document.close();
}
这是关于如何从mysql数据库获取表的代码
try {
Class.forName(driver);
conn = DriverManager.getConnection(url+db, user, pass);
Statement st = conn.createStatement();
String zero = dates.getSelectedItem().toString();
String sql = "select fruits,price from fruitstable";
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
Rectangle react = writer.getPageSize();
PdfPTable table2 = new PdfPTable(new float[] { 5,5});
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("fruits");
String v2 = rs.getString("price");
table2.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT);
table2.addCell(new Paragraph(""+v1+"", FontFactory.getFont(FontFactory.TIMES_ROMAN,14,BaseColor.BLACK)));
table2.addCell(new Paragraph(""+v2+"", FontFactory.getFont(FontFactory.TIMES_ROMAN,14,BaseColor.BLACK)));
}
table2.addCell(new Paragraph(" ", FontFactory.getFont(FontFactory.TIMES_ROMAN,8,BaseColor.BLACK)));
table2.addCell(new Paragraph(" ", FontFactory.getFont(FontFactory.TIMES_ROMAN,8,BaseColor.BLACK)));
document.add(table2);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
现在我该怎么办? 将表格插入3列的页面集中,这样我就可以尽量减少纸张的使用空间。
任何人? 对不起,我的英语不好。
请指导我甚至帮助我。
您正在将仅在iText 7中有效的代码( document.setRenderer(new ColumnDocumentRenderer(document, columns));
等等)与仅在iText 5中有效的代码混合(例如,iText 7中没有PdfPTable
)。 那永远都行不通。
在不测试代码是否正常的情况下,我将您的iText 5代码修改为iText 7代码:
Class.forName(driver);
conn = DriverManager.getConnection(url+db, user, pass);
Statement st = conn.createStatement();
String zero = dates.getSelectedItem().toString();
String sql = "select fruits,price from fruitstable";
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
Table table = new PdfPTable(2); // create a table with 2 columns
// Why did you create a table with width 527?
// You are already divising your page in 3 columns.
// Why would you try to stuff a table that spans the complete width
// of a page in a column that is only 1/3 of a page???
table.setWidthPercent(100);
// Also: you were setting the colspan of a cell to 8
// in a table with only two columns. Why???
table.addHeaderCell("Fruits");
table.addHeaderCell("Price");
while (rs.next()) {
table.addCell(rs.getString("fruits"));
table.addCell(rs.getString("price"));
}
document.add(table);
如果希望表使用其他字体,则可以这样更改字体:
PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
table.setFont(font);
您还可以将Cell
对象传递给表。 例如:
table.addHeaderCell(new Cell().add("fruits").setFontColor(Color.ORANGE));
在教程iText 7:构建块中对此进行了全部解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.