これは、Resources.class 内のメソッドです。
public static Font loadFont(String fontFileName)
{
BaseFont base = null;
try
{
base = BaseFont.createFont(Resource.class.getResource(fontFileName + "_font.ttf").toString(), BaseFont.WINANSI, true);
}
catch (DocumentException | IOException e)
{
e.printStackTrace();
}
Font font = new Font(base, Font.BOLD, 15);
return font;
}
私のプログラムの構造は次のとおりです。
src (folder)
core (package)
//all (but one) classes used for program
resources (package)
class Resources (used to load resources into the "core" classes)
wingding_font.ttf
これは、機能していないコードのスニペットです。
p = new Phrase("some random text");
p.setFont(Resource.loadFont("wingding"));
pa = new Paragraph(p);
pa.setFont(Resource.loadFont("wingding"));
document.add(pa);
PDF を開くと、テキストは表示されますが、デフォルトのフォントであると思われるフォントが使用されています。
注 1: フォントを Phrase(p) のみ、Paragraph(pa) のみに設定しようとしましたが、出力はまったく変わりませんでした。
注 2: Resource.loadFont("wingding"); メソッド try/catch はエラーを「キャッチ」しませんでした。