【问题标题】:Setting Font Color in PdfPCell/Paragraph Weirdness在 PdfPCell/段落怪异中设置字体颜色
【发布时间】:2018-05-17 02:20:28
【问题描述】:

我遇到了我认为设置颜色的奇怪问题。以下代码有效:

Font nameFont=new Font(FontFamily.TIMES_ROMAN, 20.f, Font.BOLD,new BaseColor(mon.getColor().getRGB()));
Paragraph name=new Paragraph(mon.getName(),nameFont);
PdfPCell c=new PdfPCell(name);

但是,此代码不起作用(它使用前一个单元格的颜色):

Font nameFont=new Font(FontFamily.TIMES_ROMAN, 20.f, Font.BOLD,new BaseColor(mon.getColor().getRGB()));
Paragraph name=new Paragraph(mon.getName());
name.setFont(nameFont);
PdfPCell c=new PdfPCell(name);

有人能解释为什么会这样吗? TIA。

【问题讨论】:

    标签: java colors itext paragraph


    【解决方案1】:

    设置段落的字体不会更改其中所有文本的字体,它只是设置默认字体以用于进一步添加。因此,您的

    Paragraph name=new Paragraph(mon.getName());
    name.setFont(nameFont);
    

    保留现有内容mon.getName() 的字体。如果你做了类似的事情

    name.add("some additional text");
    

    之后,您会看到附加文本正在使用您的字体。

    ParagraphPhrase 继承此行为。

    【讨论】:

    • 这有点令人困惑,但现在我明白它在做什么了。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 2010-09-27
    • 1970-01-01
    • 2016-01-19
    • 2016-09-21
    • 2011-06-01
    相关资源
    最近更新 更多