如何在MVC3中的iText PDF中显示印度卢比符号

问题描述 投票:0回答:3

我想在 iTextPDf 中显示特殊字符印度卢比符号,

我的代码:

 Font fontRupee = FontFactory.GetFont("Arial", "₹", true, 12);
 Chunk chunkRupee = new Chunk(" ₹ 5410", font3);
asp.net-mvc-3 itext nopcommerce itextpdf
3个回答
7
投票

在源代码中存储 Unicode 字符(例如 $)绝不是一个好主意。如果你这样做,很多事情可能会出错:

  • 有人可以使用与 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 能够渲染这两个符号。检查您使用的字体是否知道如何绘制符号非常重要。如果没有,您的页面上将不会显示任何内容。


1
投票

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 代表印度卢比。这取决于该字体。

    


0
投票
//

已添加此行***********// 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; } }

© www.soinside.com 2019 - 2024. All rights reserved.