PDFBox 2.0 and TTC Fonts

独自空忆成欢 提交于 2019-12-08 12:13:11

问题


I am trying to use PDFBox 2.0 (snapshot of 20151009) due to the availability of TTC support. But I haven't found any documentation on how to use this feature. I found a ticket here https://issues.apache.org/jira/browse/PDFBOX-2752 and I found how to load TTC file:

    InputStream is = MyClass.class.getResourceAsStream("font.ttc");
    TrueTypeCollection coll = new TrueTypeCollection(is);

but I don't know how to embed TrueTypeFont into my PDDocument. In PDFBox 1.8 I was using something similar to the following:

public String addFont(String key, PDFont font){

    PDResources res = pdfForm.getDefaultResources();

    if (res == null){
        res = new PDResources();
    }

    String fontName = res.addFont(font);

    pdfForm.setDefaultResources(res);

    return fontName;
}

but know I have a TrueTypeFont not a PDFont. How can I "convert" a TrueTypeFont into PDFont ? Or am I using something in a wrong way ?

Thanks


回答1:


This is brandnew (https://issues.apache.org/jira/browse/PDFBOX-3018), and will be in the upcoming 2.0 release (but not in RC1). Here's a sample code for windows:

PDFont font = PDType0Font.load(document, new TrueTypeCollection(new File("c:/windows/fonts/MSGothic.ttc")).getFontByName().get("MS-Gothic"), true);


来源:https://stackoverflow.com/questions/33044069/pdfbox-2-0-and-ttc-fonts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!