PDFBox 2.0 and TTC Fonts

2019-06-14 06:18发布

问题:

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


标签: java pdfbox