PDF泰文字符问题

时间:2019-01-17 09:39:01

标签: c# pdf itext pdf-generation unicode-string

例如,如果我尝试将以下字符打印为PDF

ต่

PDF输出

enter image description here

您可以看到PDF输出在字符和行之间有很多空格。我尝试了几种泰国字体,但输出是相同的。我需要在两个之间删除一个空格。

我的代码

using (TextReader reader = new StringReader(sb.ToString()))
{
    using (var document = new Document())
    {
        var hw = writer as HttpWriter;
        var sw = writer as StreamWriter;
        Stream outputStream = hw != null ? hw.OutputStream : sw != null ? sw.BaseStream : new MemoryStream();
        // associate output with response stream
        using (var pdfWriter = PdfWriter.GetInstance(document, outputStream))
        {
            pdfWriter.CloseStream = false;
            document.SetPageSize(iTextSharp.text.PageSize.A4);
            document.Open();
            document.NewPage();
            document.Add(new Paragraph(""));
            MemoryStream msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(sb.ToString()));
            XMLWorkerHelper.GetInstance().ParseXHtml(pdfWriter, document, msHtml, null, Encoding.UTF8, new UnicodeFont());
            document.Close();
            pdfWriter.Close();
        } 

        if (hw == null && sw == null)
        {
            outputStream.Position = 0;
            using (StreamReader sr = new StreamReader(outputStream, Encoding.Unicode))
            {
                writer.Write(sr.ReadToEnd());
            }
        }
    }
}

public class UnicodeFont : FontFactoryImp
{
    private static readonly string FontPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Content\\Loma.TTF");
    private readonly BaseFont _baseFont;

    public UnicodeFont()
    {
        _baseFont = BaseFont.CreateFont(FontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

    }

    public override Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color, bool cached)
    {
        return new Font(_baseFont, size, style, color);
    }
}

0 个答案:

没有答案