【问题标题】:iText monospaced fonts in html view with bold, italic from css stylehtml视图中的iText等宽字体,css样式中的粗体斜体
【发布时间】:2013-03-04 05:40:08
【问题描述】:

我正在使用 html 生成 PDF 文档,因为它是 EMR 记录,我必须使用等宽字体。

PDF 生成良好,但粗体和斜体的 css 样式被忽略,因为我使用单个 .otf 文件作为字体,因此没有粗体和斜体。

我想知道如何启用它。下面是代码sn-ps。

字体工厂:

public static class MyFontFactory implements FontProvider,Serializable {
        public Font getFont(String fontname,
                String encoding, boolean embedded, float size,
                int style, BaseColor color) {
                BaseFont bf3 = null;
            try {
                bf3 = BaseFont.createFont("Inconsolata.otf",BaseFont.CP1252, BaseFont.EMBEDDED);
            } catch (Exception e) {
                e.printStackTrace();
            } 

            return new Font(bf3, 6);
        }

        public boolean isRegistered(String fontname) {
            return false;
        }
    }

PDF 生成代码:

public void createPdf(Object object) throws Exception, DocumentException{
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(new File("test.pdf")));
        // step 3
        document.open();
       // create extra properties
        HashMap<String,Object> map = new HashMap<String, Object>();
        map.put(HTMLWorker.FONT_PROVIDER, new MyFontFactory());
        // step 4
        String snippet;
        // create the snippet
        snippet = createHtmlSnippet(object);
        Map<Object,Object> model = new HashMap<Object,Object>();
        model.put("object", object);

        StyleSheet css = new StyleSheet();
        Map<String, String> stylemap = new HashMap<String, String>();
        stylemap.put("font-style", "italic");
        stylemap.put("font-size", "small");
        stylemap.put("font-weight", "bold");
        css.loadStyle("header",(HashMap<String, String>) stylemap);
        css.loadStyle("strongClass", "text-decoration", "underline");
        List<Element> objects = HTMLWorker.parseToList(new StringReader(snippet), css, map);

       for (Element element : objects)
              document.add(element);
        // step 5
        document.close();
    }

在上面的代码中提供的 css 不会对输出产生任何影响 正如我提到的由于定义了单一字体,如果我想要粗体和 斜体如何实现?

如果有人提供有关相同的指示或帮助,我将不胜感激。

谢谢。

注意:如果我删除等宽字体,css 会被应用。

【问题讨论】:

    标签: java pdf-generation itext


    【解决方案1】:

    您将字体系列与字体混淆了。

    Inconsolata 是一个由不同字体组成的字体家族:

    • inconsolata.ttf 中定义的常规 Inconsolata
    • inconsolata-Bold.ttf 中定义的粗体字

    http://code.google.com/p/googlefontdirectory/source/browse/ofl/inconsolata/

    我不知道任何粗体、斜体或粗斜体版本,因为我假设"there is no bold or italic for Inconsolata." 如果没有其他样式的字体程序,您不应该期望 iText 支持这些样式 (*)。

    然后我找到了一个带有粗体字体 TTF 的存储库:http://code.google.com/p/googlefontdirectory/source/browse/ofl/inconsolata/

    搜索 StackOverflow 我在 StackOverflow 上阅读了关于 Inconsolata Italic in MacVim 的问题;不幸的是,这些字体不能在 iText 中使用。

    (*) 当字体不支持粗体或斜体时,iText 可以通过更改渲染模式和/或倾斜来模仿这些样式。但是,选择另一种等宽字体会获得更好的效果。

    【讨论】:

    • 感谢您的回答,它应该会指导我把事情做好
    猜你喜欢
    • 2010-12-13
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    相关资源
    最近更新 更多