如何使用itext更改模拟粗体字体的权重 [英] How do I change the weight of a simulated bold font using itext

查看:1406
本文介绍了如何使用itext更改模拟粗体字体的权重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iText库生成文本。我正在加载Arial Unicode MS字体,它不包含粗体样式,因此iText模拟粗体。这样可以正常工作,但与使用Java API或甚至使用Microsoft Word生成的文本相比,粗体字体的重量显得过重。
我试图从FontDescriptor获得权重,但返回的值总是0.0

I am using the iText library to generate text. I am loading the Arial Unicode MS font which does not contain a bold style so iText is simulating the bold. This works fine, but the weight of the bold font appears too heavy compared with text generated using the Java API or even using Microsoft Word. I tried to get the weight from the FontDescriptor, but the value returned is always 0.0

float weight = font.getBaseFont().getFontDescriptor(BaseFont.FONT_WEIGHT, fontSize);

有没有办法可以改变模拟粗体字体的重量?

Is there a way I can change the weight of a simulated bold font?

推荐答案

作为@Chris回答的附录:你不需要构造那些 Object [] s因为有一个便利方法:

As an addendum to @Chris' answer: You do not need to construct those Object[]s as there is a Chunk convenience method:

    BaseFont arialUnicodeMs = BaseFont.createFont("c:\\Windows\\Fonts\\ARIALUNI.TTF", BaseFont.WINANSI, BaseFont.EMBEDDED);
    Font arial12 = new Font(arialUnicodeMs, 12);

    Paragraph p = new Paragraph();
    for (int i = 1; i < 100; i++)
    {
        Chunk chunk = new Chunk(String.valueOf(i) + " ", arial12);
        chunk.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE, i/100f, null);
        p.add(chunk);
    }
    document.add(p);

结果

这篇关于如何使用itext更改模拟粗体字体的权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆