iTextSharp不会呈现şcha字符

时间:2011-03-01 14:19:25

标签: pdf pdf-generation itextsharp

我在asp.net mvc中使用iTextSharp来返回这样的pdf:

public class Pdf : IPdf
    {
        public FileStreamResult Make(string s)
        {
            using (var ms = new MemoryStream())
            {
                using (var document = new Document())
                {
                    PdfWriter.GetInstance(document, ms);
                    document.Open();
                    using (var str = new StringReader(s))
                    {
                        var htmlWorker = new HTMLWorker(document);

                        htmlWorker.Parse(str);
                    }
                    document.Close();
                }

                HttpContext.Current.Response.ContentType = "application/pdf";
                HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=MyPdfName.pdf");
                HttpContext.Current.Response.Buffer = true;
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
                HttpContext.Current.Response.OutputStream.Flush();
                HttpContext.Current.Response.End();

                return new FileStreamResult(HttpContext.Current.Response.OutputStream, "application/pdf");
            }
        }
    }

问题是像ă ţ ş这样的字符没有呈现

2 个答案:

答案 0 :(得分:1)

yetanothercoder是正确的。这样就可以解决问题...但是还有一个非常类似的问题,我会更详细地回答一下:

iText + HTMLWorker - How to change default font?

答案 1 :(得分:1)

请尝试以下方法:

var unicodeFont = iTextSharp.text.pdf.BaseFont.CreateFont(iTextSharp.text.FontFactory.TIMES_ROMAN, iTextSharp.text.pdf.BaseFont.CP1250, iTextSharp.text.pdf.BaseFont.EMBEDDED);
acroFields.SetFieldProperty("txtContractorBirthPlace", "textfont", unicodeFont, null);

它应该为你做。 您必须将字段属性添加到您希望具有变音符号的每个字段。

<强> BAFTA!