如何在 iText 中加载 FontFactory.register 中的自定义字体

How to load custom font in FontFactory.register in iText

我需要你的帮助来添加自定义字体 "arial.ttf",它存储在我的项目的资源文件夹下,在 iText 的 FontFactory.register 方法中。

项目中的字体路径如下Windows Explorer:

public_html\resources\fonts\arial.ttf

字体引用代码为:

FontFactory.register("/resources/fonts/arial.ttf", "my_bold_font");
Font myBoldFont = FontFactory.getFont("my_bold_font");

然而,当我 运行 Java 方法时,它总是给我错误:

java.io.IOException: /resources/fonts/arial.ttf not found as file or resource.

我尝试了不同的路径,例如:

/public_html/resources/fonts/arial.ttf

../resources/fonts/arial.ttf

/fonts/arial.ttf

/arial.ttf

但是结果一样,找不到文件。那么如何引用文件呢?

代码完成者:

 FontFactory.register(System.getProperty("file.separator")+"resources"+System.getProperty("file.separator")+"fonts"+System.getProperty("file.separator")+"arial.‌​ttf", "my_bold_font");
 Font myBoldFont = FontFactory.getFont("my_bold_font");

您可以使用 contextClassLoader 获取资源文件夹中存在的 'font' 的路径,并且可以在 FontFactory 文件路径中使用。

URL font_path = Thread.currentThread().getContextClassLoader().getResource("fontname");
FontFactory.register(font_path.toString(), "test_font");

我已经测试了这段代码,它工作正常。

我已经尝试将文件夹 "src/main/webapp/resources/fonts" 中存储的所有字体添加到 XMLWorkerFontProvider。

成功了。

XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);

URL font_path = Thread.currentThread().getContextClassLoader().getResource("resources/fonts");

File directory = new File(font_path.getPath());

//get all the files from a directory

File[] fList = directory.listFiles();

for (File file : fList){

    System.out.println("Font File Path******************************   " +file.getPath());
    fontImp.register(file.getPath());
} 
FontFactory.setFontImp(fontImp);

Document document = new Document();
Rectangle one = new Rectangle(width,height);
document.setPageSize(one);
document.setMargins(1, 1, 1, 1);
ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
PdfWriter pdfWriter=PdfWriter.getInstance(document, new FileOutputStream(folderPath+"/"+fileName));
document.open();

InputStream is = new ByteArrayInputStream(html.getBytes());
//XMLWorkerHelper.getInstance().parseXHtml(pdfWriter,document, is);
XMLWorkerHelper.getInstance().parseXHtml(pdfWriter,document, is, null, null,fontImp);
document.close();

我正在使用 Spring Boot 2.2 和 iText 5 (openpdf),当我执行 Spring Boot 应用程序时,这段代码运行良好。

import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.pdf.BaseFont;
import java.awt.*;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

public class CustomFont {

    public static Font myFont;

    /**
     * This method should be loaded when you start the app for the first time.
     * @param resourceLoader The resource you can get using Spring @Autowired annotation and passing it to here.
     */
    public static void registerFont(ResourceLoader resourceLoader) {

        try {

            // Note the "classpath: " syntax.
            // I am storing my font in: src/main/resources/fonts/my-custom-font.ttf
            Resource resource = resourceLoader.getResource("classpath:/fonts/my-custom-font.ttf");
            FontFactory.register(resource.getURL().getPath(), "aliasCustomFontName");

        } catch (Exception e) {
            // When executing the unit tests, the font is not found so I am catching and ignoring it.
            // The fonts are not important for the unit tests in my case.
            e.printStackTrace();
        }

        myFont = FontFactory.getFont("aliasCustomFontName", BaseFont.WINANSI, true, 1, Font.NORMAL, Color.WHITE);
    }

    /**
     * You can use the custom font in your code with: CustomFont.myFont
     */

}