iTextSharp pdf导出导出问题(特别是Macron“ā”)Unicode字符 [英] iTextSharp pdf export exporting issue (Specially Macron “ ā ”) Unicode Character

查看:160
本文介绍了iTextSharp pdf导出导出问题(特别是Macron“ā”)Unicode字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Itextsharp导出PDF数据时遇到问题。特别是,它去除微米ā。如果您有任何想法,请提供帮助。

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

}

以上结果代码:


Mori Mori
Mori Mori
Borell,P。和Macfarlane,A。( 2016)体育和教育的双重话语:Mori
开发的有效融合。儿童,青少年和体育:经验和意义研究基督城:剑桥
学者出版社。
Cooper,G。(2016)A Prosthesis和TPPA。
Cooper,G。(2016)什么是今日的知识自由:土着反思。大陆思想&&
理论1(1):93-95。

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.


推荐答案

终于得到了解决方案。

   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注册字体并添加编码传递样式表的方法。

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

这篇关于iTextSharp pdf导出导出问题(特别是Macron“ā”)Unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆