如何将ā转换为真实的角色ā

时间:2017-02-28 03:30:14

标签: c# html asp.net itext

我尝试了HttpUtility.htmlDecode,但这将删除该字符,而māori它将变为 mori

我正在使用iTextsharp从数据表生成pdf文件。

我删除了HTML解码代码,现在它显示为

  

毛利学院

     

纳尔逊毛利人的历史

虽然HTML解码适用于Müller此HTML标记。

有人能建议我在这种情况下解码会有什么帮助吗?

我的代码:

  protected void GeneratePdfReport(DataTable RRDT)
    {
   // DataRow dr = GetData("SELECT * FROM Employees where EmployeeId = " + ddlEmployees.SelectedItem.Value).Rows[0]; ;
   // DataRow dr = RRDT.Rows[0];

    try
    {
        Document document = new Document(PageSize.A4, 40f, 88f, 30f, 10f);
        Font NormalFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, Color.BLACK);
        using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
        {
            PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
            Phrase phrase = null;
            PdfPCell cell = null;
            PdfPTable table = null;
            Color color = null;

            document.Open();

            //Header Table
            table = new PdfPTable(1);
            table.TotalWidth = 500f;
            table.LockedWidth = true;
            //    table.SetWidths(new float[] { 1f });
            table.SpacingBefore = 20f;
            table.HorizontalAlignment = Element.ALIGN_LEFT;

            string TempCollege = "";
            string TempDepartment = "";
            string TempPublication = "";



            foreach (DataRow dr in RRDT.Rows)
            {

                if (dr["College"].ToString() != TempCollege || TempCollege == "")
                {
                    //College
                    phrase = new Phrase();
                    phrase.Add(new Chunk(dr["College"] + "\n", FontFactory.GetFont("Georgia", 20, Font.BOLD, Color.RED)));
                    cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
                    cell.PaddingBottom = 12f;
                    cell.PaddingTop = 12f;
                    table.AddCell(cell);
                    TempCollege = dr["College"].ToString();
                    TempDepartment = "";
                    TempPublication = "";
                }


                if (dr["Department"].ToString() != TempDepartment || TempDepartment == "")
                {
                    //Department
                    phrase = new Phrase();
                    phrase.Add(new Chunk(dr["Department"].ToString() + "\n", FontFactory.GetFont("Arial", 14, Font.BOLD, Color.RED)));
                    cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
                    cell.PaddingBottom = 12f;
                    cell.PaddingTop = 12f;
                    table.AddCell(cell);
                    TempDepartment = dr["Department"].ToString();
                    TempPublication = "";
                }

                if (dr["PublicationType"].ToString() != TempPublication || TempPublication == "")
                {
                    //Publication Type
                    phrase = new Phrase();
                    phrase.Add(new Chunk(dr["PublicationType"] + "\n", FontFactory.GetFont("Calibri", 12, Font.BOLD, Color.BLACK)));
                    cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
                    cell.PaddingBottom = 6f;
                    cell.PaddingTop = 6f;
                    table.AddCell(cell);
                    TempPublication = dr["PublicationType"].ToString();
                }

                //Citation
                phrase = new Phrase();
                phrase.Add(new Chunk(HttpUtility.HtmlDecode(dr["Citation"].ToString()) + "\n", FontFactory.GetFont("Arial", 10, Font.NORMAL, Color.BLACK)));
                cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
                cell.PaddingBottom = 3f;
                cell.PaddingTop = 3f;

                table.AddCell(cell);
            }

            document.Add(table);

            document.Close();


                byte[] bytes = memoryStream.ToArray();
                memoryStream.Close();
                Response.Clear();
                Response.ContentType = "application/pdf";
                Response.AddHeader("Content-Disposition", "attachment; filename=ResearchReport.pdf");
                Response.Buffer = true;
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.BinaryWrite(bytes);
                Response.ContentEncoding = Encoding.UTF8;
                HttpContext.Current.Response.Flush(); 
                HttpContext.Current.Response.SuppressContent = true;  
                HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
    }
    catch (ThreadAbortException ex)
    {
        String errorString = "Something went wrong:";
        UCSparkCommon.SendExceptionEmail(errorString, ex);
    }
    catch (Exception ex)
    {
        String errorString = "Something went wrong:";
        UCSparkCommon.SendExceptionEmail(errorString, ex);
    }
}

3 个答案:

答案 0 :(得分:0)

我敢打赌你的测试用例的HTML实体代码无效。

这有效

        var s = "Māori";
        Console.WriteLine(HttpUtility.HtmlDecode(s));

上面的代码将输出

  

毛利

请参阅fiddle

答案 1 :(得分:0)

HttpUtility需要System.Web,但System.Net.Http默认出现在Console Apps中

Console.WriteLine(System.Net.WebUtility.HtmlDecode("Māori"));
  

输出: -

     

毛利

答案 2 :(得分:-1)

需要使用iTextSharp注册字体,并使用传递样式表添加编码方法。

答案就在这里。 https://stackoverflow.com/a/42824669/5047078