Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# iTextSharp pdf导出问题(特别是Macron“;ā;”;)Unicode字符_C#_Asp.net_Stream_Itext - Fatal编程技术网

C# iTextSharp pdf导出问题(特别是Macron“;ā;”;)Unicode字符

C# iTextSharp pdf导出问题(特别是Macron“;ā;”;)Unicode字符,c#,asp.net,stream,itext,C#,Asp.net,Stream,Itext,我在使用Itextsharp导出PDF数据时遇到问题。特别是,它可以去除微米ā。如果你有任何想法,请帮忙 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.t

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

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

}
使用系统;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.IO;
使用系统数据;
使用System.Data.SqlClient;
使用系统配置;
使用iTextSharp.text;
使用iTextSharp.text.html.simpleparser;
使用iTextSharp.text.pdf;
使用系统文本;
公共部分类PDF_生成:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
DataTable dt=新的DataTable();
dt.Columns.AddRange(新数据列[]){
新数据列(“学院”,类型(int)),
新数据列(“部门”,类型(字符串)),
新数据列(“PublicationType”,typeof(string)),
新数据列(“引用”,类型(字符串))
});
增加(1,“奥塔希毛利人和土著研究学校”,“章节”,“毛利人和土著人”);
增加(1,“奥塔希毛利人和土著研究学校”,“章节”,“毛利人和土著人”);
dt.Rows.Add(1,“奥塔希毛利人和土著研究学校”,“章节”,“Borell,P.和Macfarlane,A.(2016)体育和教育的双重论述:毛利人发展的有效融合。儿童、青年和体育:经验和意义研究基督城:剑桥学者出版社”);
dt.Rows.添加(2,“穆达萨尔·汗”、“印度毛利人”、“库珀·G.(2016)A假肢和TPPA”);
dt.Rows.Add(3,“毛利人马修”,“法国”,“库珀,G.(2016)什么是今天的知识自由:本土反思。大陆思想与理论1(1):93-95”);
生成epdf(dt);
}
公共void generatePDF(数据表dt)
{
文件=新文件(PageSize.A4、40f、88f、30f、10f);
使用(System.IO.MemoryStream MemoryStream=new System.IO.MemoryStream())
{
PdfWriter writer=PdfWriter.GetInstance(文档,内存流);
短语=空;
PdfPCell=null;
PdfPTable table=null;
document.Open();
//标题表
表=新的PDFP表(1);
表1.2总宽度=500f;
table.LockedWidth=true;
//表.设置宽度(新浮点[]{1f});
表1.1:间距前=20f;
table.HorizontalAlignment=Element.ALIGN_LEFT;
foreach(数据行dr在dt.行中)
{
//表格单元格css样式
var tableCell=新的PdfPCell();
tableCell.BorderColor=Color.WHITE;
tableCell.VerticalAlignment=PdfCell.ALIGN_TOP;
tableCell.HorizontalAlignment=PdfCell.paragration;
tableCell.PaddingBottom=3f;
tableCell.PaddingTop=0f;
tableCell.PaddingLeft=1f;
//引用的Css样式
样式表样式=新样式表();
样式。LoadTagStyle(“p”、“face”、“Georgia”);
样式。LoadTagStyle(“p”、“大小”、“10px”);
样式。LoadTagStyle(“p”、“线高”、“2px”);
LoadTagStyle(“a”、“文本装饰”、“下划线”);
样式。LoadTagStyle(“a”、“颜色”、“蓝色”);
//将引文转换为html格式。
foreach(HTMLWorker.ParseToList中的IEElement元素(新的StringReader(“”+HttpUtility.HtmlCode(dr[“引文”].ToString())+“

”),样式)) { tableCell.AddElement(元素); } table.AddCell(tableCell); } 文件。添加(表); document.Close(); byte[]bytes=memoryStream.ToArray(); Response.Clear(); Response.ContentType=“application/pdf”; AddHeader(“内容处置”、“附件;文件名=ResearchReport.pdf”); Response.Buffer=true; Response.Cache.SetCacheability(HttpCacheability.NoCache); 响应。二进制写入(字节); Response.ContentEncoding=System.Text.Encoding.Unicode; Response.End(); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.SuppressContent=true; HttpContext.Current.ApplicationInstance.CompleteRequest(); } } 专用静态PdfPCell短语单元格(短语短语,int-align) { PdfPCell=新的PdfPCell(短语); cell.BorderColor=Color.WHITE; cell.VerticalAlignment=PdfCell.ALIGN_TOP; cell.HorizontalAlignment=对齐; cell.PaddingBottom=2f; cell.PaddingTop=0f; 返回单元; } }
上述代码的结果:

森森 森森 Borell,P.和Macfarlane,A.(2016)体育和教育的双重论述:莫里的有效融合 发展。儿童、青年与体育:经验与意义研究克赖斯特彻奇:剑桥 学者出版社。 Cooper,G.(2016)假体和TPPA。 Cooper,G.(2016)什么是今天的知识自由:本土反思。大陆思想&& 理论1(1):93-95


下面的代码片段看起来可疑:

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

终于找到了解决方案。

   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);
            }
foreach(数据行中的数据行dr)
{
//表格单元格css样式
var tableCell=新的PdfPCell();
tableCell.BorderColor=Color.WHITE;
tableCell.VerticalAlignment=PdfCell.ALIGN_TOP;
tableCell.HorizontalAlignment=PdfCell.paragration;
tableCell.PaddingBottom=3f;
tableCell.PaddingTop=0f;
tableCell.PaddingLeft=1f;
string fontpath=Environment.GetEnvironmentVariable(“SystemRoot”)+“\\f
   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);
            }