iText + HTMLWorker - 如何更改默认字体? [英] iText + HTMLWorker - How to change default font?

查看:493
本文介绍了iText + HTMLWorker - 如何更改默认字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从HTML源代码创建一个PDF文件。目前,我正在处理与输出文件中的特殊(波兰语)字符有关的问题,正是由于缺少这些字符。

I have to create a PDF file from a HTML source. Currently, I'm coping with problem concerning special (polish) characters in the output file, precisely with their lack.

HTML源代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<table width="100%" border="0.5" align="center" cellpadding="0" style="border-collapse:collapse; border:1px solid black; font-family:Arial, Helvetica, sans-serif; font-size:16px">
  <tr>
    <td align="center" ><b>Test: ąęłóćńśŁÓŃĆŻŹąśżźłęó</b></td>
  </tr>
</table>

Java源代码:

Document document = new Document(PageSize.A4, 38, 38, 50, 38);  
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("iTextExample.pdf"));  
document.open();  
HTMLWorker htmlWorker = new HTMLWorker(document);  
htmlWorker.parse(new StringReader(readFileAsString("index.html")));  
document.close();


public static String readFileAsString(String filePath) throws IOException {
    DataInputStream dis = new DataInputStream(new FileInputStream(filePath));
    try {
        long len = new File(filePath).length();
        if (len > Integer.MAX_VALUE) {
            throw new IOException("File " + filePath + " too large, was " + len + " bytes.");
        }
        byte[] bytes = new byte[(int) len];
        dis.readFully(bytes);
        return new String(bytes, "UTF-8");
    } finally {
        dis.close();
    }
}

我的问题是:如何更改默认字体(Helvetica)到例如。 Arial Bold在整个PDF文档中?

我测试了许多与StyleSheet连接的示例,但没有一个工作。我必须更改默认字体,因为没有波兰字符 - 这是我希望的解决方案。

I've tested many examples connected with StyleSheet and none of them worked. I have to change a default font, because there's no polish characters - that's the solution I hope is going to work.

感谢任何帮助!

编辑

My FontProvider:  
    class defaultFontProvider extends FontFactoryImp {

    private String _default;

    public defaultFontProvider(String def) {
        _default = def;
    }

    public Font getFont(String fontName, String encoding, boolean embedded, float size, int style, BaseColor color, boolean cached) {
        if (fontName == null || size == 0) {
            fontName = _default;
        }

        return super.getFont(fontName, encoding, embedded, size, style, color, cached);
    }
    }

上面的代码嵌入 arial .ttf 这是确定的,但是如何使它成为整个文档的默认字体(而不是Helvetica)。

The code above embeds arial.ttf which is OK, but how do I make it the default font (instead of Helvetica) for the whole document.

然后..

        Map<String,Object> providers = new HashMap<String, Object>();

        defaultFontProvider dfp = new defaultFontProvider("arial.ttf");


        providers.put(HTMLWorker.FONT_PROVIDER, dfp);

        HTMLWorker htmlWorker = new HTMLWorker(document);
        htmlWorker.setProviders(providers);


推荐答案

在样式标签中,也许你可以使用CSS3更改字体:

In your style tag, perhaps you can use CSS3 to change the font:

<style>
@font-face {
font-family: myFont;
src: url(Filename);
}
</style>

不确定这是否是您要求的。

Not sure if this is what you were asking.

这篇关于iText + HTMLWorker - 如何更改默认字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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