You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 6+下生成希伯来语与英语混合文本PDF的方案

解决方案:混合希伯来语与英语PDF生成(.NET 6+)

一、使用iText 7 Community(MIT协议)修正渲染问题

iText 7 Community是MIT协议授权的版本,对RTL语言(如希伯来语)的排版支持远优于旧版非开源iTextSharp,完全适配.NET 6+环境。

核心配置要点

  • 字体选择:必须嵌入支持希伯来语的Unicode字体(如Arial Unicode MS、Noto Sans Hebrew),确保特殊字符能正确渲染。
  • RTL方向设置:明确开启文本从右到左的书写方向,配合右对齐实现希伯来语的自然排版。
  • 双向文本处理:依赖库内置的双向文本逻辑,自动处理英语(LTR)与希伯来语(RTL)混合的文本顺序。

示例代码

using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using System.IO;

public class PdfGenerator
{
    public void GenerateMixedLanguagePdf()
    {
        string outputPath = "mixed_lang_invoice.pdf";
        using (PdfWriter writer = new PdfWriter(outputPath))
        using (PdfDocument pdfDoc = new PdfDocument(writer))
        using (Document document = new Document(pdfDoc))
        {
            // 设置页面边距
            document.SetMargins(36, 36, 36, 36);

            // 加载支持希伯来语的字体(替换为你的本地字体路径)
            string fontPath = @"C:\Windows\Fonts\arialuni.ttf";
            PdfFont font = PdfFontFactory.CreateFont(fontPath, PdfEncodings.IDENTITY_H, PdfFontFactory.EmbeddingStrategy.FORCE_EMBEDDED);

            // 英语段落(LTR左对齐)
            Paragraph englishPara = new Paragraph()
                .SetFont(font)
                .SetFontSize(12)
                .Add("Invoice Date: 2024-07-15\n")
                .Add("Customer Name: John Doe\n")
                .Add("Total Cost: $500\n")
                .Add("Address: Demo address\n\n")
                .SetTextAlignment(TextAlignment.LEFT);
            document.Add(englishPara);

            // 希伯来语段落(RTL右对齐)
            Paragraph hebrewPara = new Paragraph()
                .SetFont(font)
                .SetFontSize(12)
                .Add("תאריך חשבונית: 15-07-2024\n")
                .Add("שם לקוח: יוחנן דות\n")
                .Add("סכום כולל: 500$\n")
                .Add("העיר\n")
                .Add("2, מבחן\n")
                .Add("- ישראל\n")
                .Add("00000,\n")
                .Add("כתובת:")
                .SetTextAlignment(TextAlignment.RIGHT)
                .SetDirection(Direction.RTL);
            document.Add(hebrewPara);
        }
    }
}

public class Program
{
    public static void Main()
    {
        PdfGenerator generator = new PdfGenerator();
        generator.GenerateMixedLanguagePdf();
    }
}

解决的核心问题

  1. 希伯来语渲染:通过嵌入支持希伯来语的字体+IDENTITY_H编码,彻底解决字符显示异常问题。
  2. 双向文本排版:iText 7自动识别并调整LTR/RTL混合文本的顺序,避免文字错乱。
  3. RTL对齐:SetDirection(Direction.RTL)+TextAlignment.RIGHT实现希伯来语的自然从右到左排版。

二、替代库:PdfSharp(MIT协议)

PdfSharp是MIT协议的轻量级PDF生成库,支持.NET 6+,配合SkiaSharp后端可高效处理RTL文本。

示例代码(SkiaSharp后端)

using PdfSharpCore.Drawing;
using PdfSharpCore.Pdf;
using SkiaSharp;

public class PdfSharpGenerator
{
    public void GenerateRtlPdf()
    {
        string outputPath = "pdfsharp_mixed.pdf";
        using (PdfDocument document = new PdfDocument())
        {
            PdfPage page = document.AddPage();
            using (XGraphics gfx = XGraphics.FromPdfPage(page))
            {
                // 加载支持希伯来语的字体
                string fontPath = @"C:\Windows\Fonts\arialuni.ttf";
                using (SKTypeface skTypeface = SKTypeface.FromFile(fontPath))
                using (XFont font = new XFont(skTypeface, 12, XFontStyle.Regular))
                {
                    // 英语文本(左对齐)
                    gfx.DrawString("Invoice Date: 2024-07-15", font, XBrushes.Black,
                        new XRect(40, 40, page.Width - 80, page.Height - 80), XStringFormats.TopLeft);
                    gfx.DrawString("Customer Name: John Doe", font, XBrushes.Black,
                        new XRect(40, 60, page.Width - 80, page.Height - 80), XStringFormats.TopLeft);

                    // 希伯来语文本(右对齐)
                    string hebrewText = "תאריך חשבונית: 15-07-2024\nשם לקוח: יוחנן דות\nסכום כולל: 500$";
                    float yPos = 120;
                    foreach (string line in hebrewText.Split('\n'))
                    {
                        gfx.DrawString(line, font, XBrushes.Black,
                            new XRect(page.Width - 40, yPos, page.Width - 80, page.Height - 80), XStringFormats.TopRight);
                        yPos += 20;
                    }
                }
            }
            document.Save(outputPath);
        }
    }
}

三、关键注意事项

  • 字体合规性:确保使用的字体允许嵌入到PDF中,避免版权问题。
  • 编码一致性:始终使用Unicode编码(如IDENTITY_H)处理多语言文本。
  • 测试验证:生成PDF后需检查希伯来语的字符顺序、对齐方式是否符合阅读习惯。

内容的提问来源于stack exchange,提问作者Sandeep

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 01:41:07