【问题标题】:Having trouble displaying data from sql database to PDF无法将 sql 数据库中的数据显示为 PDF
【发布时间】:2013-10-14 15:40:28
【问题描述】:

当我运行此代码时,它会生成 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()));
}

【问题讨论】:

  • 您共享的代码在哪里有问题?在 temptr.getFltTyreCost() 中?
  • 是的,temptr.getStrTyreBrand() 在 pdf 中显示品牌。但是 temptr.getFltTyreCost() 没有。

标签: java itext


【解决方案1】:

我假设在你的行中

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)

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

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

【讨论】:

  • 呵呵,谢谢,我在看这里之前已经解决了 :) 但确实有效。再次感谢。
  • @hadsi 如果mkl答对了,请给答案加分。
猜你喜欢
  • 2014-02-10
  • 1970-01-01
  • 2017-04-18
  • 2020-12-09
  • 1970-01-01
  • 2015-05-09
  • 1970-01-01
  • 2021-09-22
相关资源
最近更新 更多