首页 > 解决方案 > 如何在 itext 7 中使用 opentype 功能?

问题描述

我使用 fontlab studio,我使用 OpenType 在其中创建连字,是否可以在 itext 7中使用连字?

标签: javapdfitext

解决方案


可以在使用 iText7 编写的文本(包括连字)上应用 OpenType 功能,但它需要pdfCalligraph - iText7 插件。

另请参阅此答案iText 变音符号(例如 D̂、M̂ 等)在 PDF 上未正确显示,特别是下一段:

有关pdfCalligraph的更多信息,请参阅“iText 7:构建块”教程的第 2 章(请滚动至本章末尾)以了解其工作原理。您可以在此处获得 pdfCalligraph 的免费试用版。

当您拥有 pdfCalligraph 时,您必须明确启用连字,因为它们被视为可选功能。您可以使用下一段代码作为示例:

PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
Document document = new Document(pdfDocument);
PdfFont font = PdfFontFactory.createFont(FONT_PATH, PdfEncodings.IDENTITY_H);
document.setProperty(Property.FONT, font);

String text1 = "Testing ligatures feature in layout (off): Fff akt ikto!";
Paragraph p = new Paragraph(text1);
document.add(p);

String text2 = "Testing ligatures feature in layout (on): Fff akt ikto!\nAnd also kerning: AWAWAWA";
Paragraph pLiga = new Paragraph(text2);
pLiga.setProperty(Property.TYPOGRAPHY_CONFIG, new TypographyConfigurator()
        .addFeatureConfig(
                new LatinScriptConfig()
                        .setLigaturesApplying(true)
                        .setKerningFeature(true)
        ));
document.add(pLiga);

document.close(); 

示例输出: 图像显示通过 pdfCalligraph 应用 opentype 功能的效果


推荐阅读