无法将数据从sql数据库显示为PDF

时间:2013-10-07 21:52:07

标签: java itext

当我运行此代码时,它会生成PDF,但只显示品牌而不是成本。它似乎只显示字符串,而不是整数,浮点数等。如果我要创建一个表并使用.addCell(temptr.getFltTyreCost());不仅它不起作用,但我收到错误

  

找不到适合addCell(float)的方法。

代码:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("TyreTread2013DTAPU");

EntityManager em = emf.createEntityManager();

List<Tyrerange> tr = em.createNamedQuery("Tyrerange.findAll").getResultList();
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(tireFile));
document.open();

Image ttlogo = Image.getInstance(ttLogo);
ttlogo.scaleAbsolute(525, 85);
document.add(ttlogo);

document.add(new Paragraph("Inventory Tire Stock on Hand Report",
        FontFactory.getFont(FontFactory.TIMES_BOLDITALIC, 18, Font.BOLD, BaseColor.RED)));
document.add(new Paragraph(new Date().toString()));
document.add(new Paragraph(" "));

document.add(new Paragraph("Brand \t\t Cost"));

for (Tyrerange temptr : tr) {
    document.add(new Paragraph(temptr.getStrTyreBrand()));
    document.add(new Paragraph(temptr.getFltTyreCost()));
}

1 个答案:

答案 0 :(得分:6)

我假设你的行

document.add(new Paragraph(temptr.getFltTyreCost()));

你想要添加一个新段落,其中包含temptr.getFltTyreCost返回的浮点的文本表示。不幸的是,带有float参数的Paragraph构造函数不会将float解释为要显示的内容,而是将其解释为:

/**
 * Constructs a <CODE>Paragraph</CODE> with a certain leading.
 *
 * @param   leading     the leading
 */
public Paragraph(float leading)

因此,您首先必须将float转换为字符串,例如:

document.add(new Paragraph(String.valueOf(temptr.getFltTyreCost())));