繁体   English   中英

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

[英]How to load custom font in FontFactory.register in iText

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

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

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

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

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

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

代码由以下人员完成:

 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。

它工作成功。

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 应用程序时,此代码运行良好。

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
     */

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM