Itext 动态定位使用jfree图表生成的表格和条形图

Itext 动态定位使用jfree图表生成的表格和条形图,itext,jfreechart,Itext,Jfreechart,Hi All Iam使用itextpdf-5.1.0.jar,显示表格(此表格中的值是根据输入屏幕中的开始日期和结束日期从数据库中获取的)和上表中值的条形图 问题是我想将条形图定位在表格下方,iam从数据库中获取的值可能会因开始日期和结束日期而异,现在iam通过提供一些静态值(234567)来定位表格和条形图,如果表格值和条形图的值太大,则会被覆盖。是否有其他方法可以动态定位表格和条形图 Document document = new Document(PageSize.A4_LANDSCAPE

Hi All Iam使用itextpdf-5.1.0.jar,显示表格(此表格中的值是根据输入屏幕中的开始日期和结束日期从数据库中获取的)和上表中值的条形图

问题是我想将条形图定位在表格下方,iam从数据库中获取的值可能会因开始日期和结束日期而异,现在iam通过提供一些静态值(234567)来定位表格和条形图,如果表格值和条形图的值太大,则会被覆盖。是否有其他方法可以动态定位表格和条形图

Document document = new Document(PageSize.A4_LANDSCAPE, 10, 10, 10, 10);
document.open();
document.add(new Paragraph("Batch Report:", FontFactory.getFont(FontFactory.COURIER, 10, Font.BOLD, new CMYKColor(255, 255, 255, 255))));
Paragraph paragraph1 = new Paragraph();
paragraph1.setSpacingBefore(4);
document.add(paragraph1);
PdfPTable table = new PdfPTable(5);
table.setWidthPercentage(100);
PdfPCell c1;

for (BoardBean bean : listHeader) {
addColumn(bean.getID(),c1,table,myColor,btableheadercolor);   
}
向表中添加列值

private void addColumn(String text,PdfPCell c1,PdfPTable dataTable,BaseColor   myColor,BaseColor btablecolumncolor) {
  try {
  final Font tabletdcolor = new Font(Font.FontFamily.HELVETICA, 6, Font.NORMAL, BaseColor.BLACK);
  c1 = new PdfPCell(new Paragraph(text, tabletdcolor));
  cellStyle(c1, myColor, btablecolumncolor);
  dataTable.addCell(c1);
  } catch (Exception ex) {
ex.printStackTrace();
  }
}
生成条形图

JFreeChart reportBarChart = genBatchReportBarChart(listHeader);
PdfTemplate reportTemplate = contentByte.createTemplate(280, 230);
        Graphics2D reportGraphics = reportTemplate.createGraphics(280, 230, new DefaultFontMapper());
Rectangle2D reportRectangle = new Rectangle2D.Double(0, 0, 280, 230);
reportBarChart.draw(reportGraphics, reportRectangle);
reportGraphics.dispose();
contentByte.addTemplate(reportTemplate, 10, height+150);

现在,在上面的代码中,iam固定了条形图的位置,如果数值较低,则效果良好,但如果数值较大,则条形图将覆盖表格值。根据表格值,条形图需要对齐,我如何才能实现这一点

将表格添加到文档后,您可以询问表格的总高度,并使用该表格高度决定将图表放在何处

或者(更容易实现):您可以将
PdfTemplate
包装在
图像
对象中:

Image img = Image.getInstance(reportTemplate);

添加表后立即使用
document.Add()
添加该图像(假设您正在使用
document.Add()
添加表)。

你好,bruno感谢您回答它的工作,我还有一个问题,我们可以使用document.Add()并排添加两个图像吗?您可以将两个图像合并到一个表中(每个图像一个单元格),或者可以将图像包装到块中: