4

我使用http://www.codeproject.com/Articles/260470/PDF-reporting-using-ASP-NET-MVC3从我的剃刀视图中生成 pdf 文件,效果很好,但我无法显示像 č 这样的西里尔字母, C 。我尝试了一切,但无法正常工作。

我必须以某种方式告诉 HtmlWorker 使用不同的字体:

 using (var htmlViewReader = new StringReader(htmlText))
                    {                       
                        using (var htmlWorker = new HTMLWorker(pdfDocument))
                        {                            

                            htmlWorker.Parse(htmlViewReader);
                        }
                    }

你能帮我吗?

编辑:

我少了一行

styleSheet.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H);

其余的与答案相同。

4

1 回答 1

4

如果您将StandardPdfRenderer的Render方法更改为以下代码段,它应该可以工作:

public byte[] Render(string htmlText, string pageTitle)
{
    byte[] renderedBuffer;

    using (var outputMemoryStream = new MemoryStream())
    {
        using (var pdfDocument = new Document(PageSize.A4, HorizontalMargin, HorizontalMargin, VerticalMargin, VerticalMargin))
        {
            string arialuniTff = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF");
            iTextSharp.text.FontFactory.Register(arialuniTff);

            PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDocument, outputMemoryStream);

            pdfWriter.CloseStream = false;
            pdfWriter.PageEvent = new PrintHeaderFooter { Title = pageTitle };
            pdfDocument.Open();

            using (var htmlViewReader = new StringReader(htmlText))
            {
                using (var htmlWorker = new HTMLWorker(pdfDocument))
                {
                    var styleSheet = new iTextSharp.text.html.simpleparser.StyleSheet();
                    styleSheet.LoadTagStyle(HtmlTags.BODY, HtmlTags.FACE, "Arial Unicode MS");
                    styleSheet.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H);
                    htmlWorker.SetStyleSheet(styleSheet);

                    htmlWorker.Parse(htmlViewReader);
                }
            }
        }

        renderedBuffer = new byte[outputMemoryStream.Position];
        outputMemoryStream.Position = 0;
        outputMemoryStream.Read(renderedBuffer, 0, renderedBuffer.Length);
    }

    return renderedBuffer;
}
于 2012-05-23T07:33:37.210 回答