StringReader中UTF-8的问题 [英] Problems with UTF-8 in StringReader
本文介绍了StringReader中UTF-8的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用HTML到PDF进行invoce,但是当我有unicode字符时会遇到麻烦:ПРОБАСДАСасда或者代码中的这个> КОСТАДИНСТОЈЧЕВ并且此字符未显示在pdf文档中。这是代码,所以请告诉我如何解决这个问题。
Hi,
I am trying to make a invoce with HTML to PDF but have troubles when i have unicode characters like: ПРОБАСДАСасда or in the code this one> КОСТАДИН СТОЈЧЕВ and this characters arent shown in the pdf doc. here is the code so pls gude me how to fix this.
try
{
var document = new Document(PageSize.A4, 50, 50, 25, 25);
PdfWriter pdfWriter = PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\Invoice_Statement.pdf", FileMode.Create));
document.Open();
document.AddAuthor("");
document.AddCreator("Creator of the Doc");
document.AddSubject("Subject of the Doc");
document.AddCreationDate();
document.AddTitle("This is the title");
//SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
//SAXmyHtmlHandler shh = new SAXmyHtmlHandler(document);
HTMLWorker htmlWorker = new HTMLWorker(document);
String str = "<html><head><meta http-equiv='Content-Type' content='text/html;charset=utf-8'><title><img src='C:/Users/Kosta/Desktop/Vmedia Own Projects/Prodavnicata/eMall/Dodatoci za Master/images/logo.png' alt='Smiley face' height='42' width='42'></title></head><body><table><tr><td><p style='font-size: 10pt; font-family: Times'>" +
"Cher Monsieur,</p><br><p align='justify' style='text-indent: 2em; font-size: 10pt; font-family: Times'>" +
"КОСТАДИН СТОЈЧЕВ<br></p><p align='justify' style='text-indent: 2em; font-size: 10pt; font-family: Times'>" +
"En vous remerciant à nouveau de la confiance que vous nous témoignez,</p>" +
"<br><p style='font-size: 10pt; font-family: Times'>Bien Cordialement,<br>" +
"<br>ADMINISTRATEUR ADMINISTRATEUR<br>Ligne directe : 04 42 91 52 10<br>Acadomia® – " +
"37 BD Aristide Briand – 13100 Aix en Provence </p></td></tr></table></body></html>";
htmlWorker.Parse(new StringReader(str));
document.Close();
}
catch (DocumentException err)
{
}
catch (FileNotFoundException ex)
{
}
catch (IOException exx)
{
}
推荐答案
以下是几个样本。示例A是一个简单的类
,它将存储西里尔文输出并在中返回
使用字符串
ToString()覆盖
。如果你想使用StringReader类
你总是从
StringReader $ c $继承
c>如例B所示。
例A:
Here are a couple samples. Example A is a simpleclass
that will store the cyrillic output and return thestring
in theToString()
usingoverride
. If you want to useStringReader class
you could alwaysinherit
fromStringReader
as shown in Example B.
Example A:
public class MyStringReader
{
private String str;
private Encoding encoding = Encoding.UTF8;
public Encoding Encoding
{
get
{
return this.encoding;
}
set
{
if (value != null)
{
this.encoding = value;
}
}
}
public MyStringReader(string str)
{
this.str = this.encoding.GetString(this.encoding.GetBytes(str));
}
public override string ToString()
{
return this.str;
}
}
例B:
Example B:
public sealed class CyrillicTextReader : StringReader
{
public CyrillicTextReader(string text)
: base(LoadCyrillicText(text))
{
}
private static string LoadCyrillicText(string text)
{
return Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(text));
}
}
我修复它:)))))最后下面的代码完美无缺。主要问题是它需要一个字体
I fix it :))))) finally the code below works perfectly. The main problem was that it needed a font
string html = @"<html><body><img src='C:/Users/Kosta/Desktop/Vmedia Own Projects/Prodavnicata/eMall/Dodatoci za Master/images/logo.png' alt='Smiley face' height='42' width='42'><table><tr><td><p style='font-size: 10pt; font-family: Times'>" +
"Cher Monsieur,</p><br><p>" +
"КОСТА<br></p><p align='justify' style='text-indent: 2em; font-size: 10pt; font-family: Times'>" +
"En vous remerciant à nouveau de la confiance que vous nous témoignez,</p>" +
"<br><p style='font-size: 10pt; font-family: Times'>Bien Cordialement,<br>" +
"<br>ADMINISTRATEUR ADMINISTRATEUR<br>Ligne directe : 04 42 91 52 10<br>Acadomia® – " +
"37 BD Aristide Briand – 13100 Aix en Provence </p></td></tr></table></body></html>";
FontFactory.Register("c:/windows/fonts/ARIALUNI.TTF");
StyleSheet style = new StyleSheet();
style.LoadTagStyle("body", "face", "Arial Unicode MS");
style.LoadTagStyle("body", "encoding", BaseFont.IDENTITY_H);
using (Document document = new Document())
{
PdfWriter pdfWriter = PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\Invoice_Statement.pdf", FileMode.Create));
document.Open();
foreach (IElement element in HTMLWorker.ParseToList(
new StringReader(html.ToString()), style))
{
document.Add(element);
}
}
这篇关于StringReader中UTF-8的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文