Java 使用3列页面设置生成从数据库到ITEXT7 PDF的表
下面是我在三列页面设置中设置pdf的代码,现在在“InsertCodehere”中,我想从数据库中插入一个表Java 使用3列页面设置生成从数据库到ITEXT7 PDF的表,java,mysql,itext,Java,Mysql,Itext,下面是我在三列页面设置中设置pdf的代码,现在在“InsertCodehere”中,我想从数据库中插入一个表水果清单+价格。 那我该怎么做呢 public void createPdf(String dest) throws IOException { //Initialize PDF document PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); // Initialize documen
水果清单+价格。
那我该怎么做呢
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数据库获取表的代码
现在我该怎么做呢?将表格插入3列页面集中,这样我就可以尽量减少纸张空间的使用。
有人吗?对不起,我的英语不好。
请指导我,甚至帮助我。您将只在iText 7中有效的代码(
document.setRenderer(新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);
您还可以将单元格
对象传递给表。例如:
table.addHeaderCell(new Cell().add("fruits").setFontColor(Color.ORANGE));
教程对此进行了解释,这与您在评论中提出的问题不同,您在评论中要求导入最初存储在PDF文件中的内容。现在您正在谈论添加存储在数据库中的内容。我误解了你评论中的问题了吗?另外:你把只在iText 7中有效的代码(
document.setRenderer(new ColumnDocumentRenderer(document,columns));
)与只在iText 5中有效的代码混合在一起(在iText 7中没有PdfPTable
)。那是行不通的。你为什么不看文档?看看你是否想知道如何创建一张桌子。布鲁诺爵士谢谢你指导我。谢谢你,我已经做到了。如果是这样,请点击分数旁边的复选框接受答案。(现在,分数为0的不被接受的答案看起来好像我的答案没有帮助。)我只是添加了您给出的其他代码加上这个代码。非常感谢你
table.addHeaderCell(new Cell().add("fruits").setFontColor(Color.ORANGE));