1. 使用pdfbox 3.0.2版本
2. 默认的情况下,pdfbox是不能直接加载ttc字体来使用。需要增加pdfbox-font包来作处理
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>3.0.2</version>
</dependency>
3. 需要使用TrueTypeCollection来加载TTC字体,读取里面的TTF字体,里面注意获取想要的字体。
File file = new File("simsun.ttc");
TrueTypeFont trueTypeFont = null;
try(TrueTypeCollection trueTypeCollection = new TrueTypeCollection(file)){
trueTypeFont = trueTypeCollection.getFontByName("SimSun");
}catch (Exception e){
e.printStackTrace();
}
PDType0Font font1 = null;
if (trueTypeFont != null) {
font1 = PDType0Font.load(doc, trueTypeFont, true);
}
4. 这样就能像使用TTF字体一样使用TTC字体了。