我正在使用 iTextSharp (5.5.9) 创建一个包含阿拉伯文和英文字符的 pdf 页面,我正在使用 PdfPTable 来组织页面中的文本。我能够显示英文字符,但阿拉伯文字符没有运气。
我正在使用 MVC6 web api 将文档加载到浏览器,他是代码:
[HttpGet]
[Route("PrintReport")]
public FileStreamResult GenerateReport(int id)
{
MemoryStream workStream = new MemoryStream();
Document document = new Document(PageSize.A4, 25, 25, 30, 30);
PdfWriter.GetInstance(document, workStream).CloseStream = false;
document.Open();
document.NewPage();
string fontLoc = @"c:\windows\fonts\tahoma.ttf"; // make sure to have the correct path to the font file
BaseFont bf = BaseFont.CreateFont(fontLoc, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font f = new Font(bf, 12);
PdfPTable table = new PdfPTable(3);
table.DefaultCell.NoWrap = false;
table.WidthPercentage = 100;
table.AddCell(getCell("Testing", PdfPCell.ALIGN_LEFT,f));
table.AddCell(getCell("Text in the middle", PdfPCell.ALIGN_CENTER,f));
PdfPCell cell = new PdfPCell(new Phrase("مرحبا", f));
cell.NoWrap = false;
cell.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
table.AddCell(cell);
document.Add(table);
document.Close();
byte[] byteInfo = workStream.ToArray();
workStream.Write(byteInfo, 0, byteInfo.Length);
workStream.Position = 0;
return new FileStreamResult(workStream, "application/pdf");
}
private PdfPCell getCell(string text, int alignment,Font f)
{
PdfPCell cell = new PdfPCell(new Phrase(text,f));
cell.Padding = 0;
cell.HorizontalAlignment = alignment;
cell.Border = PdfPCell.NO_BORDER;
return cell;
}
但我得到问号