2017-03-14 75 views
0

我正面臨使用Itextsharp導出PDF數據的問題。特別是它去除微米ā。如果你有任何想法請幫助。從上面的代碼iTextSharp pdf導出導出問題(特別是Macron「ā」)Unicode字符

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; 
} 

} 

結果:

森森 森森 Borell,P。和麥克法蘭,A。(2016)運動和教育的雙重話語:一個有效的共混物開發Mori 。兒童,年輕人和體育:關於經驗和意義的研究基督城:劍橋學院出版社。 Cooper,G。(2016)假體和TPPA。 Cooper,G。(2016)什麼是今日的知識自由:一種本土的反思。大陸思想& & 理論1(1):93-95。下面

+0

您是否嘗試過將編碼設置爲utf-8? – Guerrilla

+0

是的,我做了但沒有成功。 –

+1

您是否驗證過您使用的字體是否包含字形? – mkl

回答

0

終於得到了解決。

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); 
      } 

需要與iTextSharp的登記字體,並與通過樣式表添加編碼方法。

+0

啊,所以你實際使用的字體(沒有註冊的默認字體)不包含ā字形,就像問題的註釋中提到的一樣。 – mkl

+0

@mkl \t \t 更新我的答案。實際上,它需要註冊字體和編碼方法。 「styles.LoadTagStyle(」body「,」encoding「,」Identity-H「);」只有註冊字體不會使其工作。 –

0

的片段看起來很可疑:

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

dr["Citation"]已經是HTML。我認爲當你將它放在<p>元素中時,你會希望將它編碼爲HTML,這樣當你解析它時,它就能正確理解它。我懷疑這是破壞你的文字。

試試這個:

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

謝謝@Tim。結果是一樣的。 ā失蹤。 –

+0

更新我的答案。實際上,它需要註冊字體和編碼方法。 「styles.LoadTagStyle(」body「,」encoding「,」Identity-H「);」只有註冊字體不會使其工作。 –