0

I am facing a problem with export PDF data using Itextsharp. Especially, it removing micron ā. Please help if you have any idea.

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.Text;

public partial class PDF_generate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

            DataTable dt = new DataTable();
            dt.Columns.AddRange(new DataColumn[] {
        new DataColumn("College", typeof(int)),
        new DataColumn("Department", typeof(string)),
        new DataColumn("PublicationType", typeof(string)),
         new DataColumn("Citation", typeof(string))
    });
            dt.Rows.Add(1, "Aotahi School of Māori  Māori and  Indigenous Studies", "Chapters", "Māori  Māori");
            dt.Rows.Add(1, "Aotahi School of Māori  Māori and  Indigenous Studies", "Chapters", "Māori  Māori");
            dt.Rows.Add(1, "Aotahi School of Māori  M&#257;ori and  Indigenous Studies", "Chapters", "Borell, P. and Macfarlane, A. (2016) Dual discourses of sport and education: An effectual blend for M&#257;ori development. <i>Children, young people and sport: Studies on experience and meaning</i> Christchurch: Cambridge Scholars Press.");
            dt.Rows.Add(2, "Mudassar Khan", "India Māori", "Cooper, G. (2016) <i>A Prosthesis and the TPPA.</i>");
            dt.Rows.Add(3, "Māori Mathews", "France", "Cooper, G. (2016) What is Intellectual Freedom Today: An Indigenous Reflection. <i>Continental Thought &amp;&amp; Theory </i>1(1): 93-95.");


    generatePDF(dt);

   }
  public void generatePDF(DataTable dt)
{
        Document document = new Document(PageSize.A4, 40f, 88f, 30f, 10f);

        using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
        {
            PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
            Phrase phrase = null;
            PdfPCell cell = null;
            PdfPTable table = 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;


            foreach (DataRow dr in dt.Rows)
            {

                //Table Cell css style 
                var tableCell = new PdfPCell();
                tableCell.BorderColor = Color.WHITE;
                tableCell.VerticalAlignment = PdfCell.ALIGN_TOP;
                tableCell.HorizontalAlignment = PdfCell.PARAGRAPH;
                tableCell.PaddingBottom = 3f;
                tableCell.PaddingTop = 0f;
                tableCell.PaddingLeft = 1f;

                //Css style for citation
                StyleSheet styles = new StyleSheet();
                styles.LoadTagStyle("p", "face", "Georgia");
                styles.LoadTagStyle("p", "size", "10px");
                styles.LoadTagStyle("p", "line-height", "2px");
                styles.LoadTagStyle("a", "text-decoration", "underline");
                styles.LoadTagStyle("a", "color", "blue");


                //Convert citation into html format.
               foreach (IElement element in HTMLWorker.ParseToList(new StringReader("<p>" + HttpUtility.HtmlDecode(dr["Citation"].ToString())+ "</p>"),styles))
                {
                    tableCell.AddElement(element);
                }
               table.AddCell(tableCell);
            }

            document.Add(table);
            document.Close();

            byte[] bytes = memoryStream.ToArray();

            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 = System.Text.Encoding.Unicode;
            Response.End();
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.SuppressContent = true;
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
}

private static PdfPCell PhraseCell(Phrase phrase, int align)
{
    PdfPCell cell = new PdfPCell(phrase);
    cell.BorderColor = Color.WHITE;
    cell.VerticalAlignment = PdfCell.ALIGN_TOP;
    cell.HorizontalAlignment = align;
    cell.PaddingBottom = 2f;
    cell.PaddingTop = 0f;
    return cell;
}

}

Result from the above Code:

Mori Mori Mori Mori Borell, P. and Macfarlane, A. (2016) Dual discourses of sport and education: An effectual blend for Mori development. Children, young people and sport: Studies on experience and meaning Christchurch: Cambridge Scholars Press. Cooper, G. (2016) A Prosthesis and the TPPA. Cooper, G. (2016) What is Intellectual Freedom Today: An Indigenous Reflection. Continental Thought && Theory 1(1): 93-95.

Mack Patel
  • 25
  • 3
  • 13

2 Answers2

0

The snippet below looks suspicious:

"<p>" + HttpUtility.HtmlDecode(dr["Citation"].ToString())+ "</p>"

dr["Citation"] is already HTML. I think you will want to leave it encoded as HTML when you place it inside the <p> element, so that when you parse that it understands it correctly. I suspect that is mangling your text.

Try this instead:

"<p>" + dr["Citation"].ToString() + "</p>"
Tim
  • 5,940
  • 1
  • 12
  • 18
  • Thanks @Tim. Result is same. ā is missing. – Mack Patel Mar 14 '17 at 03:01
  • Update my answer. Actually, it's required registered font and encoding method. "styles.LoadTagStyle("body", "encoding", "Identity-H");" Only registered font will not make it working. – Mack Patel Mar 16 '17 at 21:45
0

Finally got the solution.

   foreach (DataRow dr in dt.Rows)
            {

                //Table Cell css style 
                var tableCell = new PdfPCell();
                tableCell.BorderColor = Color.WHITE;
                tableCell.VerticalAlignment = PdfCell.ALIGN_TOP;
                tableCell.HorizontalAlignment = PdfCell.PARAGRAPH;
                tableCell.PaddingBottom = 3f;
                tableCell.PaddingTop = 0f;
                tableCell.PaddingLeft = 1f;

                string fontpath = Environment.GetEnvironmentVariable("SystemRoot") + "\\fonts\\Calibri.TTF";
                ////Path to our font
                //string arialuniTff = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), fontpath);
                ////Register the font with iTextSharp
                //iTextSharp.text.FontFactory.Register(arialuniTff);

                //Register font with iTextSharp
                FontFactory.Register(fontpath, "Calibri");
                StyleSheet styles = new StyleSheet();
                styles.LoadTagStyle("body", "face", "Calibri"); ;
                styles.LoadTagStyle("body", "encoding", "Identity-H");
                styles.LoadTagStyle("p", "size", "10px");
                styles.LoadTagStyle("p", "line-height", "2px");
                styles.LoadTagStyle("a", "text-decoration", "underline");
                styles.LoadTagStyle("a", "color", "blue");


                //Convert citation into html format.
               foreach (IElement element in HTMLWorker.ParseToList(new StringReader("<p>" +dr["Citation"].ToString()+ "</p>"),styles))
                {
                    tableCell.AddElement(element);
                }
               table.AddCell(tableCell);
            }

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

Mack Patel
  • 25
  • 3
  • 13
  • Ah, so the font you actually used (the default one without registration) did not contain the ā glyph, just as mentioned in the comments to the question. – mkl Mar 16 '17 at 07:34
  • @mkl Update my answer. Actually, it's required registered font and encoding method. "styles.LoadTagStyle("body", "encoding", "Identity-H");" Only registered font will not make it working. – Mack Patel Mar 19 '17 at 19:36