下面是我在三列页面设置中设置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个圆柱页面中,以便我可以设法最大限度地减少纸张空间的使用。
任何人?抱歉我的英语不好。
请指导我,甚至帮助我。
答案 0 :(得分:1)
您将仅在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));
中对此进行了解释