使用iTextSharp在PDF中显示html表格中的中文字符
本文关键字:表格 中文 字符 html 显示 iTextSharp PDF 使用 | 更新日期: 2023-09-27 17:54:10
我已经能够遵循这里的示例,并且已经能够让中文字符在表外显示,但在表中的单元格内它们不会显示。
这是示例html代码-
@{
<head>
<title>Daily Production Report 国务院公布了房</title>
</head>
}
@{
<table style="width: 61%; font-size:x-small; font-family:'Arial Unicode MS'" border="1">
<tr>
<td style="width: 78px">Date:</td>
<td style="width: 200px"> </td>
<td style="width: 80px">Order ID</td>
<td style="width: 200px">国务院公布了房</td>
<td style="width: 112px">Total Pieces</td>
<td style="width: 200px"> </td>
</tr>
</table>
}
这是-
后面的代码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.GetInstance(document, new FileStream(fileName, FileMode.Create));
document.Open();
foreach (IElement element in HTMLWorker.ParseToList(
new StringReader(HTMLData.ToString()), style))
{
document.Add(element);
}
document.Close();
}
虽然你的HTML是有效的,HTMLWorker
's是打破的事实,你把你的字体名称在HTML内的单引号。将font-family:'Arial Unicode MS'
更改为font-family:Arial Unicode MS
对我有效。更好的是,如果你控制HTML,那么就完全摆脱font-family
声明,因为<body>
标签将继承到其他所有内容。
如果你不能改变生成的HTML,你也可以在声明它的时候重命名/重新别名Arial。
//Register one alias
FontFactory.Register("C:''Windows''Fonts''ARIALUNI.TTF", "Arial Unicode MS");
//Register another alias
FontFactory.Register("C:''Windows''Fonts''ARIALUNI.TTF", "'Arial Unicode MS'");
另外,请记住HTMLWorker
早就被弃用了,取而代之的是XMLWorker
。