如何强制itextsharp ITextHandler.Parse使用UTF-8?

时间:2015-08-16 20:45:34

标签: asp.net-mvc pdf utf-8 itextsharp

我在ASP.NET MVC服务器上使用代码呈现PDF,但是某些字符(例如e with accent (ě))未正确显示。我使用的库是来自nuget的itextsharp-LGPL。

PDF呈现代码为:

protected ActionResult ViewPdf(object model)
{
    // http://www.codeproject.com/Articles/66948/Rendering-PDF-views-in-ASP-MVC-using-iTextSharp
    // Create the iTextSharp document.
    Document doc = new Document();
    // Set the document to write to memory.
    MemoryStream memStream = new MemoryStream();
    PdfWriter writer = PdfWriter.GetInstance(doc, memStream);
    writer.CloseStream = false;
    doc.Open();

    // Render the view xml to a string, then parse that string into an XML dom.
    string xmltext = this.RenderActionResultToString(this.View(model));
    XmlDocument xmldoc = new XmlDocument();
    xmldoc.InnerXml = xmltext.Trim();

    // Parse the XML into the iTextSharp document.
    ITextHandler textHandler = new ITextHandler(doc);
    textHandler.Parse(xmldoc);

    // Close and get the resulted binary data.
    doc.Close();
    byte[] buf = new byte[memStream.Position];
    memStream.Position = 0;
    memStream.Read(buf, 0, buf.Length);

    // Send the binary data to the browser.
    return new BinaryContentResult(buf, "application/pdf");
}

控制器的Helper方法如下所示:

public class PDFControllerBase : Controller
{
    protected string RenderActionResultToString(ActionResult result)
    {
        // Create memory writer.
        var sb = new StringBuilder();
        var memWriter = new StringWriter(sb);            

        // Create fake http context to render the view.
        var fakeResponse = new HttpResponse(memWriter);
        var fakeContext = new HttpContext(System.Web.HttpContext.Current.Request, fakeResponse);
        var fakeControllerContext = new ControllerContext(
            new HttpContextWrapper(fakeContext),
            this.ControllerContext.RouteData,
            this.ControllerContext.Controller);
        var oldContext = System.Web.HttpContext.Current;
        System.Web.HttpContext.Current = fakeContext;

        // Render the view.
        result.ExecuteResult(fakeControllerContext);

        // Restore data.
        System.Web.HttpContext.Current = oldContext;

        // Flush memory and return output.
        memWriter.Flush();
        string content = sb.ToString();
        return content;
    }
}

视图如下:

@model MyNamespace.Model.MyModel

@{
    Layout = null;
}
<?xml version="1.0" encoding="UTF-8" ?>
<itext creationdate="2/4/2015 5:49:07 pm" producer="iTextSharpXML">
    <paragraph leading="18.0" font="arial" size="16.0" align="center">
        <chunk>@Model.Title</chunk><newline /><newline />
    </paragraph>
    <paragraph leading="18.0" font="arial" size="10.0" align="justify">
        <chunk>@Model.TextDetail</chunk><newline />        
    </paragraph>   
</itext>

我尝试将以下代码添加到PDF渲染中,但它没有帮助:

string arial = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "arial.ttf");
FontFactory.Register(arial, "arial");

0 个答案:

没有答案