我正在尝试将新字体添加到我的pdf文件中,我需要使其加粗并加下划线。

我可以添加新字体,但不能同时使其加粗和加下划线。

我尝试了以下方法。

public class PdfGenerator {

    private static final String BASE_FONT = "Trebuchet MS";
    private static final String BASE_FONT_BOLDITALIC = "Trebuchet MS BI";
    private static final String BASE_FONT_BOLD = "Trebuchet MS B";

    private static final Font titlefontSmall = FontFactory.getFont(
            BASE_FONT_BOLD, 10, Font.UNDERLINE);

    static {
        String filePath = "..my font directory";
        String fontPath = filePath + "\\" + "trebuc.ttf";
        String fontPathB = filePath + "\\" + "TREBUCBD.TTF";
        String fontPathBI = filePath + "\\" + "TREBUCBI.TTF";

        FontFactory.register(fontPath, BASE_FONT);
        FontFactory.register(fontPathB, BASE_FONT_BOLD);
        FontFactory.register(fontPathBI, BASE_FONT_BOLDITALIC);
    }

    public void genMyPdf(String filePath) {
        try {
            Document document = new Document();
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream(filePath));
            document.open();

            Paragraph p = new Paragraph("This should be bold & underline",
                    titlefontSmall);
            document.add(p);
            document.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }

    }

}

我究竟做错了什么 ?

最佳答案

因为您正在使用IText库,所以请阅读 HERE

基本上使用块,然后将这些块添加到文档中。

Chunk underline = new Chunk("Underline. ");
underline.setUnderline(0.1f, -2f); //0.1 thick, -2 y-location
document.add(underline);

我自己还没有尝试过,所以不知道结果如何。进一步阅读iText文档,您似乎必须先定义一个粗体字体,然后再实现它。 THIS TUTORIAL 显示了iText粗体字体用法和使用粗体文本pdf的示例。从那里,我确定您可以将上面的代码实现为粗体文本和walah !,加粗下划线的文本:D

关于java - 带粗体和下划线的Itext新字体,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15527774/

10-13 04:39
查看更多