Commit 11eff3d6 authored by 徐俊's avatar 徐俊

xujun

parent 6d4e9fbd
......@@ -36,11 +36,11 @@ public class ProjectInfoToPDF {
}
// 宋体
BaseFont bfChinese = loadChineseFont("/fonts/simsun.ttc");
BaseFont bfChinese = loadChineseFont("simsun.ttc");
// 仿宋
BaseFont fsChinese = loadChineseFont("/fonts/simfang.ttf");
BaseFont fsChinese = loadChineseFont("simfang.ttf");
// WINGDNG2
BaseFont WINGDNG2 = loadChineseFont("/fonts/WINGDNG2.ttf");
BaseFont WINGDNG2 = loadChineseFont("WINGDNG2.ttf");
// 添加水印
writer.setPageEvent(new WatermarkPageEvent(watermark, fsChinese));
......@@ -116,23 +116,34 @@ public class ProjectInfoToPDF {
/**
* 加载中文字体
* @param fontPath 字体文件路径(相对于resources目录)
* @param fontName 字体文件名
* @return BaseFont 中文字体
* @throws RuntimeException 加载字体失败时抛出异常
*/
private static BaseFont loadChineseFont(String fontPath) {
private static BaseFont loadChineseFont(String fontName) {
try {
// 从资源文件夹加载字体
String path = ProjectInfoToPDF.class.getResource(fontPath).getPath();
// 根据字体文件类型使用不同的加载方式
if (fontPath.toLowerCase().endsWith(".ttc")) {
return BaseFont.createFont(path + ",0", BaseFont.WINANSI, BaseFont.EMBEDDED);
if ("linux".equals(getCurrentOperatingSystem())) {
if (fontName.toLowerCase().endsWith(".ttc"))
return BaseFont.createFont("/usr/share/fonts/" + fontName + ",0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
else
return BaseFont.createFont("/usr/share/fonts/" + fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
} else {
return BaseFont.createFont(path, BaseFont.WINANSI, BaseFont.EMBEDDED);
if (fontName.toLowerCase().endsWith(".ttc"))
return BaseFont.createFont("c:/Windows/Fonts/" + fontName + ",0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
else
return BaseFont.createFont("c:/Windows/Fonts/" + fontName, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
}
// // 从资源文件夹加载字体
// String path = ProjectInfoToPDF.class.getResource(fontPath).getPath();
//
// // 根据字体文件类型使用不同的加载方式
// if (fontPath.toLowerCase().endsWith(".ttc")) {
// return BaseFont.createFont(path + ",0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
// } else {
// return BaseFont.createFont(path, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
// }
} catch (Exception e) {
throw new RuntimeException("加载字体文件失败,请确保resources目录下存在字体文件 " + fontPath + ":" + e.getMessage(), e);
throw new RuntimeException("加载字体文件失败,请确保系统中存在字体文件 " + fontName + ":" + e.getMessage(), e);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment