1

I am using html to generate PDF document, since its an EMR record I have to use Monospaced fonts.

PDF is getting generated fine, but css style for bold and italics are getting ignored, as I am using single .otf file for font hence no bold and italics.

I was wondering how to enable the same. Below are the code snippets.

Font Factory:

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 Generation Code:

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();
    }

In the above code css supplied does not produce any effect on output as I mentioned due to single font defined, if I want to have bold and italics how can that be achieved?

Really appreciate if anyone provides pointers or help regarding same.

Thanks.

Note: If I remove Monospaced fonts css gets applied.

4

1 回答 1

1

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

Inconsolata 是一个由不同字体组成的字体系列:

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

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

我不知道任何粗体、斜体或粗体斜体版本,因为我认为“Inconsolata 没有粗体或斜体”。如果没有其他样式的字体程序,您不应该期望 iText 支持这些样式 (*)。

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

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

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

于 2013-03-04T11:48:06.897 回答