(itext5) itext-pdf中没找到能直接将Paragraph设置中文、西文2种不同字体的方法,所以暂时通过Chunk来实现这一功能。
思路:遍历字符串,依次得到连续的中文/西文串,然后根据它的类型得到对应字体,根据串和字体建一个Chunk加入paragraph。(当然也可以每个字符都构建一个Chunk,写法更简单,但感觉会比较占内存)
中文是双字节字符,西文是单字节字符,匹配的时候就根据是否为双字节字符来判断。
代码如下:
是否为双字节字符:
public class RegexUtils {
/**
* 是否为双字节字符
* @param input
* @return
*/
public static boolean isDoubleByteCharacter(char input) {
return Pattern.matches(Regexs.DOUBLE_BYTE_CHARACTERS.getCode(), String.valueOf(input));
}
}
正则表达式的枚举类:
public enum Regexs {
CHINESE_CHARACTERS("[\\u4e00-\\u9fa5]", "中文字符"),
DOUBLE_BYTE_CHARACTERS("[^\\x00-\\xff]", "双字节字符");
private String code;
private String des;
Regexs(String code, String des) {
this.code = code;
this.des = des;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getDes() {
return des;
}
public void setDes(String des) {
this.des = des;
}
}
根据内容s和提供的中文字体、非中文字体构造Paragraph:
public class PdfUtils {
/**
* s中的双字节字符设置为中文字体,单字节字符设置为其他字体,返回Phrase
* 思路:遍历字符串的每个字符,依次得到相同类型(同为双字节或非双字节)的连续字符串,用对应字体创建Chunk加入Paragraph
* @param s
* @param chineseFont
* @param otherFont
* @return
*/
public static Paragraph getParagraph(String s, Font chineseFont, Font otherFont) {
if (s == null || s.equals("")) {
return new Paragraph();
}
Paragraph paragraph = new Paragraph();
//临时串,用于存放相同类型(中文或者非中文)字符
StringBuilder tempBuilder = new StringBuilder();
boolean lastIsChinese = true;
for (char c : s.toCharArray()) {
boolean currentIsChinese = RegexUtils.isDoubleByteCharacter(c);
//如果当前字符和上个字符是同一种字符(都是中文字符,或者都不是中文字符),则当前字符加入临时串
if (currentIsChinese == lastIsChinese) {
tempBuilder.append(c);
}
//如果当前字符和上个字符不是一种字符,则把临时串建成chunk加入phrase,清空临时串,然后把当前字符加入临时串,更新上个字符状态
else {
paragraph.add(new Chunk(tempBuilder.toString(), lastIsChinese ? chineseFont : otherFont));
//清空临时串
tempBuilder.setLength(0);
tempBuilder.append(c);
lastIsChinese = currentIsChinese;
}
}
//把最后的临时串建成chunk加入phrase
paragraph.add(new Chunk(tempBuilder.toString(), lastIsChinese ? chineseFont : otherFont));
return paragraph;
}
}
测试:
public class FontTest0 {
public static final String DEST = "results/others/font_test0.pdf";
public static void main(String[] args) throws DocumentException, IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new FontTest0().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//中文字体
final Font fontChinese = new Font(baseFontChinese, 16, Font.NORMAL);
//非中文字体
final Font fontNotChinese = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.NORMAL);
String s = "中文xx中文中x文中文a中文中文0123456789," +
"中文中文中文。test 000 times填充填充填充填充12212填充填充填充填充填充填充填充填充填" +
"充填充填充填aaa充填充填充填充EFG填充填充填充填充填充填充3123填充填充填充" +
"填充填充填充填充bbb填充填充填充填2313充填充填充填充填充填充填充填充填" +
"充填充填充";
Paragraph paragraph = PdfUtils.getParagraph(s, fontChinese, fontNotChinese);
paragraph.setFirstLineIndent(20);
paragraph.setLeading(0, 2);
PdfPTable iTable = new PdfPTable(1);
PdfPCell iCell = new PdfPCell();
iCell.addElement(paragraph);
iCell.setBorder(Rectangle.NO_BORDER);
iTable.addCell(iCell);
document.add(iTable);
document.close();
}
}
项目是用maven构建的,对应依赖:
<dependencies>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-pdfa</artifactId>
<version>5.5.10</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-xtra</artifactId>
<version>5.5.10</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.10</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
</dependencies>
运行后,会生成文件:results/others/font_test0.pdf。效果:
以上就是所有内容。如果有其他解决方法或者有任何问题欢迎评论。