2017-03-14 58 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“);”只有注册字体不会使其工作。 –