0

これは、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 はエラーを「キャッチ」しませんでした。

4

1 に答える 1

4

埋め込みフォントオブジェクトを作成し、このフォントを使用してテキストをレンダリングしてみてください。

//this code should run once at initialization/application startup
FontFactory.register("resources/wingding_font.ttf");
Font textFont = FontFactory.getFont("wingding", BaseFont.IDENTITY_H, 
    BaseFont.EMBEDDED, 10); //10 is the size
...
//reuse the reference to the font object when rendering your text
Paragraph p = new Paragraph("someText", textFont);

ちなみに、iTextにはフォントの読み込みを支援するクラスがあり、のメソッドはFontFactoryもう必要ありません。loadFontResources

それが役に立てば幸い。

于 2012-09-08T18:04:37.447 に答える