如何将阿拉伯字体添加到Itext7 Xamarin表单

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

我使用下面的代码将阿拉伯字体添加到itext7并且它有效,但是当创建我的pdf文件时,阿拉伯单词出现在单独的字母中并且RIGHT_TO_LEFT不受影响,任何帮助将不胜感激..

 public PdfFont GetFont(string filename, string fontName)
        {
            var assembly = typeof(ResultVehicleCheckList).GetTypeInfo().Assembly;
            var fontStream = assembly.GetManifestResourceStream($"{assembly.GetName().Name}.Assets.Fonts.{filename}");
     
            byte[] fontBytes;

            using (var memoryStream = new MemoryStream())
            {
                fontStream.CopyTo(memoryStream);
                fontBytes = memoryStream.ToArray();
            }

            return PdfFontFactory.CreateFont(fontBytes, PdfEncodings.IDENTITY_H, PdfFontFactory.EmbeddingStrategy.FORCE_EMBEDDED, true);
        }


         // Initialize font (Use a font that supports Arabic characters)
         PdfFont   font = GetFont("arial.ttf", "arial");

          // Initialize table
 Table table1 = new Table(5, true);
            table1.SetBaseDirection(BaseDirection.RIGHT_TO_LEFT);
            table1.SetWidth(MillimetersToPoints(185));
            table1.SetWidth(new UnitValue(UnitValue.PERCENT, 100));
android ios xamarin.forms itext7
1个回答
0
投票

要在 Xamarin Forms 中向 iText7 添加阿拉伯字体并确保阿拉伯文字在生成的 PDF 中正确显示,您可以按照以下步骤操作:

  1. 请确认您是否有特定字体。 使用支持阿拉伯字符的字体。您可以从各种来源获取阿拉伯字体,例如 NotoNaskhArabic-Regular.ttf。

  2. 使用 PdfFontFactory.CreateFont 方法为阿拉伯字体创建 PdfFont 对象。确保编码设置为 PdfEncodings.IDENTITY_H 以支持阿拉伯字符并且嵌入字体。

    PdfFont 字体 = GetFont("NotoNaskhArabic-Regular.ttf", "阿拉伯语");

  3. 将表格的基本方向设置为 BaseDirection.RIGHT_TO_LEFT 以确保阿拉伯文本的正确渲染。

    表 table1 = new Table(5, true); table1.SetBaseDirection(BaseDirection.RIGHT_TO_LEFT);

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