我需要您的帮助,添加自定义字体“arial.ttf”,该字体存储在我的项目的资源文件夹下,位于iText的FontFactory.register
方法中。
Windows资源管理器中的项目中的字体路径如下:
的public_html \资源\字体\ 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
../资源/字体/ ARIAL.TTF
/fonts/arial.ttf
/arial.ttf
但结果与找不到文件的结果相同。那么如何引用该文件?
答案 0 :(得分:3)
您可以获得'字体'的路径。它们使用contextClassLoader
存在于资源文件夹中,可以在FontFactory
文件路径中使用。
URL font_path = Thread.currentThread().getContextClassLoader().getResource("fontname");
FontFactory.register(font_path.toString(), "test_font");
我已经测试了这段代码,但效果很好。
答案 1 :(得分: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");
答案 2 :(得分: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();
答案 3 :(得分:-1)
我正在使用带有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
*/
}