我正在使用 iText(java 库)生成 PDF 文件。在这里我有一段我必须打勾
PdfPCell cell95 = new PdfPCell(new Paragraph((new Chunk('\u2713', FontFactory.getFont(FontFactory.HELVETICA, 11, Font.BOLD, new BaseColor(0,0,0))))));
我正在使用它,但它不起作用。
您不能将 unicode 直接用于 PDFPCell。
相反,创建复选标记的图像并将其插入 PDFPCell。
此 stackoverflow 帖子和itext-questions 中的此项目表明您需要使用 unicode 字符集而不是默认的 Windows CP1252 字符集创建字体 - 尝试使用getFont
编码说明符的重载:
FontFactory.getFont(FontFactory.HELVETICA, BaseFont.IDENTITY_H, 11, Font.BOLD, new BaseColor(0,0,0))
使用此代码:
String FONT = "C:/dev/dejavu-fonts-ttf-2.33/ttf/DejaVuSans.ttf";
FontSelector selector = new FontSelector();
BaseFont base = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
selector.addFont(FontFactory.getFont(FontFactory.HELVETICA, 12));
selector.addFont(new Font(base, 12));
Phrase ph = selector.process(text);
document.add(new Paragraph(ph));
这里的重点是 1) 设置 BaseFont.IDENTITY_H; 2)只有一些字体提供“复选标记”
try this solved. Download the dejavu-sans from https://www.fontsquirrel.com/fonts/dejavu-sans. and keep it in local.
use the below code to write tick symbol into PDF doc.
private static void writeUsingIText() {
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream(new File(FILE_NAME)));
//open
document.open();
Paragraph p = new Paragraph();
p.add("This is my paragraph 1");
p.setAlignment(Element.ALIGN_CENTER);
document.add(p);
Paragraph p2 = new Paragraph();
p2.add("This is my paragraph 2"); //no alignment
document.add(p2);
Font f = new Font();
f.setStyle(Font.BOLD);
f.setSize(8);
document.add(new Paragraph("This is my paragraph 3", f));
document.add(new Paragraph("This is my tick mark ", f));
String FONT = "C:\\Users\\<username>\\Documents\\DejaVuSans.ttf";
FontSelector selector = new FontSelector();
BaseFont base = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
selector.addFont(FontFactory.getFont(FontFactory.HELVETICA, 12));
selector.addFont(new Font(base, 12));
char tickSymbol=10003;
String text = String.valueOf(tickSymbol);
Phrase ph = selector.process(text);
document.add(new Paragraph(ph));
//close
document.close();
System.out.println("Done");
} catch (FileNotFoundException | DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}