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

How to load custom font in FontFactory.register in iText

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

在Windows资源管理器中的项目中,字体路径如下:

public_html\
esources\\fonts\\arial.ttf

用于引用字体的代码是:

1
2
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

但是结果与找不到文件相同。那么如何引用该文件呢?


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

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

我已经测试过此代码,并且可以正常工作。


代码由以下人员完成:

1
2
 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");

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

成功运行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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();

我正在使用带有iText 5(openpdf)的Spring Boot 2.2,并且当我执行Spring Boot应用程序时,此代码运行良好。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
     */


}