ă ş ţ iTextSharp 不渲染字符

发布于 2024-10-19 12:01:31 字数 1379 浏览 0 评论 0原文

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

问题是像这样的字符: ă ţ ş 未呈现

I use iTextSharp in asp.net mvc to return pdf like this:

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

the problem is that characters like: ă ţ ş are not rendered

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吖咩 2024-10-26 12:01:31

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

iText + HTMLWorker - 如何更改默认字体?

yetanothercoder is correct. That would do the trick... but there's another very similar question which I answered in a bit more detail:

iText + HTMLWorker - How to change default font?

余厌 2024-10-26 12:01:31

请尝试以下操作:

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

它应该可以为您完成。
您必须将字段属性添加到您希望有变音符号的每个字段。

Baftă!

Please try the folowing:

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

And it should do it for you.
You have to add the field property to every field you wish to have diacritics.

Baftă!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文