我想在 iTextPDf 中显示特殊字符印度卢比符号,
我的代码:
Font fontRupee = FontFactory.GetFont("Arial", "₹", true, 12);
Chunk chunkRupee = new Chunk(" ₹ 5410", font3);
在源代码中存储 Unicode 字符(例如 $)绝不是一个好主意。如果你这样做,很多事情可能会出错:
从您的代码示例中,显然您不熟悉“编码”的概念。创建 Font
对象时,您传递卢比符号作为编码。
实现你想要的目标的正确方法如下所示:
BaseFont bf =
BaseFont.CreateFont("c:/windows/fonts/arial.ttf",
BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(bf, 12);
Chunk chunkRupee = new Chunk(" \u20B9 5410", font3);
请注意,卢比符号有两种可能的 Unicode 值(来源Wikipedia
): \u20B9 是您要查找的值;替代值是 \u20A8 (看起来像这样:₨)。 我已经用 arialuni.ttf 和 arial.ttf 对此进行了测试。令人惊讶的是 MS Arial Unicode 只能渲染 ₨;它无法呈现 $。普通 arial 能够渲染这两个符号。检查您使用的字体是否知道如何绘制符号非常重要。如果没有,您的页面上将不会显示任何内容。
BaseFont customfont = BaseFont.createFont(rootpath + "fonts/customfont.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED);
Font rupeeFont= new Font(customfont, 9,
Font.NORMAL, new Color(55, 55, 55));
Chunk chunkRupee = new Chunk("\u20B9", rupeeFont);
注意:使用自定义字体时,您可能需要使用一些其他字符或 unicode(U+20B9) 印度卢比
就像
Chunk chunkRupee = new Chunk("W", rupeeFont);
中的特定自定义字体 W 代表印度卢比。这取决于该字体。
//
已添加此行***********//
StyleSheet 样式 = new StyleSheet();
bool exists = System.IO.File.Exists(folder + "arial.ttf"); //**
if (!exists)
ttffilepath = await aPICall.DownloadFileToAWSByBucketName(bucketName, BucketFolder, "arial.ttf");
else
ttffilepath = folder + "arial.ttf";
FontFactory.Register(ttffilepath, "Garamond");
styles.LoadTagStyle("body", "encoding", "Identity-H");
HTMLWorker htmlparser = new HTMLWorker(pdfDoc, null, styles);
//
结束****//
using (MemoryStream memoryStream = new MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
htmlparser.SetStyleSheet(styles);
htmlparser.Parse(sr);
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
return bytes;
}
}
catch (Exception ex)
{
return null;
}
}