首页 > 解决方案 > 从 html 转换为 pdf 时,iTextSharp LGPLv2.Core 无法在 rtl 中显示阿拉伯语

问题描述

我使用iTextSharp LGPLv2.Core将 html 转换为带有阿拉伯内容的 pdf。

转换 pdf 时,阿拉伯文本显示 LTR 而不是 RTL 和分隔字符,如下所示

在此处输入图像描述

我已经注册了三种字体,它们都不起作用:

这是我所做的代码片段:

try
        {
            var paymentReceipt = await _appointmentAppService.GetPaymentReceiptAsync(checkOutId);
            string invoiceHtml = RenderView("PaymentReceipt", paymentReceipt);

            using (MemoryStream stream = new System.IO.MemoryStream())
            {
                var styleSheet = new StyleSheet();

                styleSheet.LoadTagStyle(HtmlTags.BODY, "encoding", "Identity-H");
                styleSheet.LoadTagStyle(HtmlTags.BODY, HtmlTags.FONT, "arabic");

                string webRootPath = _webHostEnvironment.WebRootPath;
                string fontPath = Path.Combine(webRootPath, "fonts", "ARABTYPE.TTF");

                FontFactory.Register(fontPath, "arabic");
                //FontFactory.Register(TestUtils.GetTahomaFontPath());
                //FontFactory.Register(TestUtils.GetArialUnicodeMSFontPath());

                var unicodeFontProvider = FontFactoryImp.Instance;
                unicodeFontProvider.DefaultEmbedding = BaseFont.EMBEDDED;
                unicodeFontProvider.DefaultEncoding = BaseFont.IDENTITY_H;

                var props = new Hashtable
                {
                    { "font_factory", unicodeFontProvider }
                };

                var document = new Document();
                var pdfWriter = PdfWriter.GetInstance(document, stream);

                document.AddAuthor(TestUtils.Author);
                document.Open();

                var objects = HtmlWorker.ParseToList(
                    new StringReader(invoiceHtml),
                    styleSheet,
                    props
                );

                foreach (IElement element in objects)
                {
                    document.Add(element);
                }

                document.Close();
                return File(stream.ToArray(), "application/pdf");
            }
        }
        catch (Exception ex)
        {
            throw;
        }

标签: asp.net-core.net-coreitextc#-3.0

解决方案


推荐阅读