【问题标题】:ă ş ţ chars aren't rendered by iTextSharpă ş ţ 字符不是由 iTextSharp 呈现的
【发布时间】:2011-07-06 13:24:06
【问题描述】:

我在 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");
            }
        }
    }

问题是没有渲染像:ă ţ ş 这样的字符

【问题讨论】:

  • 您是否尝试过使用十六进制值?您是否尝试过编码以支持此类字符?我使用Æ ØÅ 非常适合iTextSharp。
  • 这不是答案,而是对上述代码的观察(与您的问题无关,但将来可能会对您有所帮助): ms.GetBuffer() 可能会在某些情况下生成损坏的 PDF 文件情况,您可以在此处阅读更多详细信息:stackoverflow.com/questions/5109384/…
  • @balexandre 你的字符也对我有用,我将如何使用十六进制值?
  • 您是否尝试过按照我说的更改编码? HttpContext.Current.Response.ContentEncoding = "ISO-8859-16"; --> en.wikipedia.org/wiki/ISO/IEC_8859-16

标签: pdf pdf-generation itextsharp


【解决方案1】:

yetanothercoder 是正确的。这样就可以了...但是还有另一个非常相似的问题,我更详细地回答了:

iText + HTMLWorker - How to change default font?

【讨论】:

    【解决方案2】:

    请尝试以下方法:

    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);
    

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

    Baftă!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-20
      • 1970-01-01
      • 2018-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多