我正在尝试从包含阿拉伯文和英文字符的html富文本格式生成pdf字母,我使用itext创建文档,然后向文档中添加页脚和页眉,现在当我使用HTMLWorker(阿拉伯语)添加富文本时字母未呈现。
我尝试使用一种自定义字体(arial,arialuni),但是阿拉伯字符的书写错误(从左到右),所以我创建了一个PdfPTable并带有一个PdfPCell并使用HTMLWorker.parseToList将元素添加到单元格中,但这是行不通的,然后我从HTMLWorker更改为XMLWorkerHelper,但这也行不通,用户可能会插入包含表和段落的长html代码。
这是我的代码:
// create the document
final Document document = new Document(new RectangleReadOnly(695, 842), 80, 80, 120, 140);
final PdfWriter pd = PdfWriter.getInstance(document, baos);
final GeneratedPdfService event = new GeneratedPdfService();
pd.setPageEvent(event);
// open the document
document.open();
// add paragraphs
final Paragraph reference = new Paragraph(corrReference, FontFactory.getFont("arial", 12, Font.NORMAL, BaseColor.BLACK));
final Paragraph date = new Paragraph("Date: " + currDate, FontFactory.getFont("arial", 12, Font.NORMAL, BaseColor.BLACK));
document.add(new Paragraph(date));
document.add(new Paragraph(reference));
document.add(new Paragraph("\n\n"));
// define the font
BaseFont bf = BaseFont.createFont(arial.getAbsolutePath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
// create the elements list
List<Element> elements = XMLWorkerHelper.parseToElementList(emailTemplate.replace("<br>", "<br />"), null);
PdfPTable table = new PdfPTable(1);
table.setTotalWidth(535);
PdfPCell cell = new PdfPCell();
cell.setBorder(Rectangle.NO_BORDER);
cell.setRunDirection(PdfWriter.RUN_DIRECTION_LTR);
for (Element e : elements) {
for (Chunk c : e.getChunks()) {
c.setFont(new Font(bf));
}
cell.addElement(e);
}
table.addCell(cell);
table.getRows().size();
table.writeSelectedRows(0, -1, document.right() - 535, document.top() - 50, pd.getDirectContent());
// close the document
document.close();