itext PDF 7 在 Paragraph() 中使用 Text() 时,希腊字母显示为空方块
itext PDF 7 Greek Letters appear as empty squares when using Text() inside Paragraph()
我在使用 itextpdf 7 时遇到一些奇怪的问题,我用它来动态生成 pdf 并使用 java restful api 将其发送给客户端。
当我使用以下代码生成 pdf 时,一切正常:
ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
PdfWriter pdfWriter = new PdfWriter(baosPDF);
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
Document document = new Document(pdfDocument);
document.setMargins(100, 20, 20, 20);
// LOAD BOOKMAN OLD STYLE FONTS
String REGULAR = "Fonts/BOOKOS.TTF";
String BOLD = "Fonts/BOOKOSB.TTF";
String ITALIC = "Fonts/BOOKOSI.TTF";
String ITALIC_BOLD = "Fonts/BOOKOSBI.TTF";
PdfFont regularFont = PdfFontFactory.createFont(REGULAR, "Identity-H", true);
PdfFont boldFont = PdfFontFactory.createFont(BOLD, "Identity-H", true);
PdfFont italicFont = PdfFontFactory.createFont(ITALIC, "Identity-H", true);
PdfFont italicBoldFont = PdfFontFactory.createFont(ITALIC_BOLD, "Identity-H", true);
Table table = new Table(2);
table.setHorizontalAlignment(HorizontalAlignment.CENTER);
Paragraph paragraph = new Paragraph();
paragraph.setFont(regularFont);
// When i use String directly inside paragraph it works
// The Following Works and Greek language is printed OK.
paragraph.add("Αρ. Πρωτ.: " + Integer.toString(incident.getProtocolNo()));
Cell cell = new Cell();
cell.setPadding(7);
cell.add(paragraph);
table.addCell(cell);
document.add(table);
document.close();
但是,因为我想在段落中同时使用 粗体 和 普通 文本,所以我尝试使用 Text() inside paragraph 但它不起作用:它将希腊字母打印为空方块。我的代码是:
// THE FOLLOWING IS NOT WORKING. IT PRINTS GREEK LETTERS AS EMTPY SQUARES...
text1 = new Text("Ημερομηνία: ");
text1.setFont(boldFont);
text1.setFontSize(11);
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String date = formatter.format(incident.getDate());
text2 = new Text(date);
text2.setFont(regularFont);
text2.setFontSize(11);
paragraph = new Paragraph();
paragraph.add(text1);
paragraph.add(text2);
cell.add(paragraph);
table.addCell(cell);
document.add(table);
为什么希腊字母在这种情况下显示不正确?我究竟做错了什么?请帮忙...
问题是我用于粗体的字体。它不包含希腊语,而它应该包含。我从另一个站点再次下载了它,大小是上一个的三倍。现在一切正常。
我在使用 itextpdf 7 时遇到一些奇怪的问题,我用它来动态生成 pdf 并使用 java restful api 将其发送给客户端。 当我使用以下代码生成 pdf 时,一切正常:
ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
PdfWriter pdfWriter = new PdfWriter(baosPDF);
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
Document document = new Document(pdfDocument);
document.setMargins(100, 20, 20, 20);
// LOAD BOOKMAN OLD STYLE FONTS
String REGULAR = "Fonts/BOOKOS.TTF";
String BOLD = "Fonts/BOOKOSB.TTF";
String ITALIC = "Fonts/BOOKOSI.TTF";
String ITALIC_BOLD = "Fonts/BOOKOSBI.TTF";
PdfFont regularFont = PdfFontFactory.createFont(REGULAR, "Identity-H", true);
PdfFont boldFont = PdfFontFactory.createFont(BOLD, "Identity-H", true);
PdfFont italicFont = PdfFontFactory.createFont(ITALIC, "Identity-H", true);
PdfFont italicBoldFont = PdfFontFactory.createFont(ITALIC_BOLD, "Identity-H", true);
Table table = new Table(2);
table.setHorizontalAlignment(HorizontalAlignment.CENTER);
Paragraph paragraph = new Paragraph();
paragraph.setFont(regularFont);
// When i use String directly inside paragraph it works
// The Following Works and Greek language is printed OK.
paragraph.add("Αρ. Πρωτ.: " + Integer.toString(incident.getProtocolNo()));
Cell cell = new Cell();
cell.setPadding(7);
cell.add(paragraph);
table.addCell(cell);
document.add(table);
document.close();
但是,因为我想在段落中同时使用 粗体 和 普通 文本,所以我尝试使用 Text() inside paragraph 但它不起作用:它将希腊字母打印为空方块。我的代码是:
// THE FOLLOWING IS NOT WORKING. IT PRINTS GREEK LETTERS AS EMTPY SQUARES...
text1 = new Text("Ημερομηνία: ");
text1.setFont(boldFont);
text1.setFontSize(11);
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String date = formatter.format(incident.getDate());
text2 = new Text(date);
text2.setFont(regularFont);
text2.setFontSize(11);
paragraph = new Paragraph();
paragraph.add(text1);
paragraph.add(text2);
cell.add(paragraph);
table.addCell(cell);
document.add(table);
为什么希腊字母在这种情况下显示不正确?我究竟做错了什么?请帮忙...
问题是我用于粗体的字体。它不包含希腊语,而它应该包含。我从另一个站点再次下载了它,大小是上一个的三倍。现在一切正常。