0

I tried HttpUtility.htmlDecode but that will remove that character and instead māori it will become mori.

I am using iTextsharp to generate a pdf file from the data table.

I have removed HTML decoding code so now it's showing like

School of Māori

A History of Māori of Nelson

While HTML decode is working for Müller this HTML tag.

Can anyone suggest me what decoding will help in this case?

My Code:

  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);
    }
}
Mack Patel
  • 25
  • 3
  • 13

3 Answers3

0

I bet that your test case had an invalid HTML entity code.

This works

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

The above code will output

Māori

See the fiddle

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
0

HttpUtility needs System.Web but System.Net.Http comes by default in Console Apps

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

Output:-

Māori

arun thatham
  • 500
  • 1
  • 4
  • 13
-1

Need to register font with iTextSharp and add encoding method with the passing style sheet.

An answer is here. https://stackoverflow.com/a/42824669/5047078

Community
  • 1
  • 1
Mack Patel
  • 25
  • 3
  • 13