我在使用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āori and Indigenous Studies", "Chapters", "Borell, P. and Macfarlane, A. (2016) Dual discourses of sport and education: An effectual blend for Mā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 && 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)什么是今日的知识自由:土着反思。大陆思想&amp;&amp; 理论1(1):93-95。
答案 0 :(得分:0)
下面的代码段看起来很可疑:
"<p>" + HttpUtility.HtmlDecode(dr["Citation"].ToString())+ "</p>"
dr["Citation"]
已经是HTML了。我认为当你将它放在<p>
元素中时,你会希望将它编码为HTML,这样当你解析它正确理解它时。我怀疑这会破坏你的文字。
请改为尝试:
"<p>" + dr["Citation"].ToString() + "</p>"
答案 1 :(得分: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注册字体,并使用传递样式表添加编码方法。