使用itextsharp导出到pdf,用于来自数据库的多语言数据 [英] Export to pdf using itextsharp for multilanguage data coming from database

查看:62
本文介绍了使用itextsharp导出到pdf,用于来自数据库的多语言数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不支持除英语之外的任何语言。 show的空白文件



我尝试过:



public void ExportToPDF (DataTable dt,string SheetName,string From,string To)

{

HttpContext.Current.Response.ContentType =application / pdf;

HttpContext.Current.Response.AddHeader(content-disposition,attachment; filename =+ SheetName +。pdf);

HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability) .NoCache);

To = Convert.ToDateTime(To).AddDays(-1).ToString(yyyy-MM-dd);

string html = getHTMLtableFromDataTable (dt,SheetName,From,To);

System.IO.StringReader sr = new System.IO.StringReader(html);

// BaseFont bf = BaseFont。 CreateFont(Environment.GetEnvironmentVariable(windir)+ @\ fonts\Mangal.TTF,BaseFont.IDENTITY_H,BaseFont.EMBEDDED);

//iTextSharp.text.Font字体= new iTextSharp.text.Font(bf,10,iTextSharp.text.Font.NORMAL);



文件pdfDoc =新文件(PageSize.A4,10f,10f ,10f,0f);

HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

PdfWriter writer = PdfWriter.GetInstance(pdfDoc,HttpContext.Current.Response.OutputStream); < br $> b $ b

writer.ViewerPreferences = PdfWriter.PageModeUseOutlines;

PdfFooter eventHandler = new PdfFooter();

writer.PageEvent = eventHandler;



pdfDoc.Open();

// Chunk chunk = new Chunk();

//chunk.Font = font;

//pdfDoc.Add(new Paragraph(段落 - 这是一个测试! ÇçĞğİıÖöŞşÜü,font));

//pdfDoc.Add(chunk);

// Chunk c = new Chunk();

//c.Font = font;

//c.Append(\"Turkish characters:ĞÜŞİÖÇğüşıöç);

//pdfDoc.Add(c);

htmlparser.Parse(sr);



pdfDoc.Close();

HttpContext.Current.Response.Write (pdfDoc);

HttpContext.Current.Response.End();

}



也试过以下代码



public void ExportToPDF(DataTable dt,string SheetName,string From,string To)

{

HttpContext.Current.Response.ContentType =application / pdf;

HttpContext.Current.Response.AddHeader(content-disposition,attachment; filename =+ SheetName +。pdf) ;

HttpContext.Current.Response.Cache.SetCac heability(HttpCacheability.NoCache);

To = Convert.ToDateTime(To).AddDays(-1).ToString(yyyy-MM-dd);

string html = getHTMLtableFromDataTable(dt,SheetName,From,To);

System.IO.StringReader sr = new System.IO.StringReader(textConvert(html));

/ / BaseFont bf = BaseFont.CreateFont(Environment.GetEnvironmentVariable(windir)+ @\ fonts\Mangal.TTF,BaseFont.IDENTITY_H,BaseFont.EMBEDDED);

// iTextSharp。 text.Font font = new iTextSharp.text.Font(bf,10,iTextSharp.text.Font.NORMAL);



文件pdfDoc =新文件(PageSize.A4 ,10f,10f,10f,0f);

HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

PdfWriter writer = PdfWriter.GetInstance(pdfDoc,HttpContext.Current.Response。 OutputStream);



writer.ViewerPreferences = PdfWriter.PageModeUseOutlines;

PdfFooter eventHandler = new PdfFooter();

writer.PageEvent = eventHandler;



pdfDoc.Open();

// Chunk chunk = new Chunk();

//chunk.Font = font;

//pdfDoc.Add(new Paragraph( 段落 - 这是一个测试! ÇçĞğİıÖöŞşÜü,font));

//pdfDoc.Add(chunk);

// Chunk c = new Chunk();

//c.Font = font;

//c.Append(\"Turkish characters:ĞÜŞİÖÇğüşıöç);

//pdfDoc.Add(c);

htmlparser.Parse(sr);



pdfDoc.Close();

HttpContext.Current.Response.Write (pdfDoc);

HttpContext.Current.Response.End();

}



公共静态字符串textConvert(string S)

{

if(S == null){return null;}

try

{

编码encFrom = Encoding.UTF8;

编码encTo = Encoding.UTF8;

string str = S;

Byte [] b = encFrom.GetBytes(str);

返回e ncTo.GetString(b);

}

catch {return null; }

}

does not support any language other than English. show's empty text

What I have tried:

public void ExportToPDF(DataTable dt, string SheetName, string From, string To)
{
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + SheetName + ".pdf");
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
To = Convert.ToDateTime(To).AddDays(-1).ToString("yyyy-MM-dd");
string html = getHTMLtableFromDataTable(dt, SheetName, From, To);
System.IO.StringReader sr = new System.IO.StringReader(html);
//BaseFont bf = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + @"\fonts\Mangal.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
//iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);

Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);

writer.ViewerPreferences = PdfWriter.PageModeUseOutlines;
PdfFooter eventHandler = new PdfFooter();
writer.PageEvent = eventHandler;

pdfDoc.Open();
//Chunk chunk = new Chunk();
//chunk.Font = font;
//pdfDoc.Add(new Paragraph("Paragraph - This is a test! ÇçĞğİıÖöŞşÜü", font));
//pdfDoc.Add(chunk);
//Chunk c = new Chunk();
//c.Font = font;
//c.Append("Turkish characters: ĞÜŞİÖÇ ğüşıöç");
//pdfDoc.Add(c);
htmlparser.Parse(sr);

pdfDoc.Close();
HttpContext.Current.Response.Write(pdfDoc);
HttpContext.Current.Response.End();
}

also tried following code

public void ExportToPDF(DataTable dt, string SheetName, string From, string To)
{
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + SheetName + ".pdf");
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
To = Convert.ToDateTime(To).AddDays(-1).ToString("yyyy-MM-dd");
string html = getHTMLtableFromDataTable(dt, SheetName, From, To);
System.IO.StringReader sr = new System.IO.StringReader(textConvert(html));
//BaseFont bf = BaseFont.CreateFont(Environment.GetEnvironmentVariable("windir") + @"\fonts\Mangal.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
//iTextSharp.text.Font font = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);

Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);

writer.ViewerPreferences = PdfWriter.PageModeUseOutlines;
PdfFooter eventHandler = new PdfFooter();
writer.PageEvent = eventHandler;

pdfDoc.Open();
//Chunk chunk = new Chunk();
//chunk.Font = font;
//pdfDoc.Add(new Paragraph("Paragraph - This is a test! ÇçĞğİıÖöŞşÜü", font));
//pdfDoc.Add(chunk);
//Chunk c = new Chunk();
//c.Font = font;
//c.Append("Turkish characters: ĞÜŞİÖÇ ğüşıöç");
//pdfDoc.Add(c);
htmlparser.Parse(sr);

pdfDoc.Close();
HttpContext.Current.Response.Write(pdfDoc);
HttpContext.Current.Response.End();
}

public static string textConvert(string S)
{
if (S == null) { return null; }
try
{
Encoding encFrom = Encoding.UTF8;
Encoding encTo = Encoding.UTF8;
string str = S;
Byte[] b = encFrom.GetBytes(str);
return encTo.GetString(b);
}
catch { return null; }
}

推荐答案

你需要引用一个外部unicode真实字体,例如



you need to reference an external unicode true-type font eg

//Reference a Unicode font to be sure that the symbols are present. 
    BaseFont bfArialUniCode = BaseFont.CreateFont(@"drive:\path\ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 
    //Create a font from the base font 
    Font font = new Font(bfArialUniCode, 12);





显然'drive:\ path \'需要正确 - 我刚刚表明它是一个占位符 - 它可能是C:\ Windows \Fonts,无论是什么都是正确的对于你的机器 - 这可以工作





obviously the 'drive:\path\' needs to be correct - I've just shown that as a placeholder - it could be "C:\Windows\Fonts", whatever is correct for your machine - this could work

const string arialunicodepath          = Environment.GetEnvironmentVariable( "SystemRoot" ) + "\\fonts\\ARIALUNI.TTF";





....另一种始终确保字体是你想要的方法是将字体文件的副本部署到例如direc你在哪里安装你的程序和一个字体的子目录 - gettit?



把它们放在一起





.... Another way to always ensure the font is where you want it is to deploy a copy of the font file to for example, the directory where you install your program and a subdirectory for fonts - gettit ?

Putting it all together

String uniText1 = @"Any Text You Want With Unicode Characters\n";
String uniText2 = @"Line 2 Of Text";
// Get Font File ? Windows Font Directory
const string arialunicodepath = Environment.GetEnvironmentVariable( "SystemRoot" ) + "\\fonts\\ARIALUNI.TTF";
// Reference Unicode font
BaseFont bfArialUniCode = BaseFont.CreateFont(arialunicodepath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
// Create Instance of font from the base 
Font font = new Font(bfArialUniCode, 12);
// Basic Elements = Chunks

Chunk c1 = new Chunk(uniText1, font);

Chunk c2 = new Chunk(uniText2, font);

// Chunks make Phrases

Phrase p1 = new Phrase(c1);

p1.Add(c2);

// Phrases Make Paragraphs

Paragraph p = new Paragraph();

p.Add(p1);

// Add Paragraph to doc (assuming you’ve done a doc.Open();)

doc.Add([p);

doc.Close();





[/ Edit]



[/Edit]


这篇关于使用itextsharp导出到pdf,用于来自数据库的多语言数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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