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

5

我需要您帮忙在iText的FontFactory.register方法中添加一个名为“arial.ttf”的自定义字体,该字体存储在项目资源文件夹下。

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

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未被找到作为文件或资源。

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

/public_html/resources/fonts/arial.ttf

../resources/fonts/arial.ttf

/fonts/arial.ttf

/arial.ttf

但结果都相同,无法找到该文件。那么如何引用该文件呢?

@Satya 是的,我正在尝试在服务器上实现这个。 - 99maas
@Satya 我将会把这个项目部署到 WAR 包中,并在 Linux 服务器上进行部署。不过我也想在本地的 Weblogic 上测试一下。 - 99maas
@Satya:File.separator未被识别,并且它显示不是一个有效的表达式。 - 99maas
不要尝试 System.getProperty("file.separator") - SatyaTNV
请将以下与程序编写相关的内容从英语翻译成中文。仅返回翻译后的文本:请勿复制粘贴。有时会添加特殊字符,但两者都是正确的。 - SatyaTNV
显示剩余10条评论
4个回答

7

您可以使用contextClassLoader获取资源文件夹中存在的“字体”路径,并将其用于FontFactory文件路径。

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

我已经测试了这段代码,它可以正常运行。

1

代码是由以下人完成的:

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

0
我尝试过将存储在文件夹“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();

-1

我正在使用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
     */

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接