【发布时间】:2011-06-23 06:44:48
【问题描述】:
我使用 ITextSharp 库将 html 转换为 pdf。 我的用户在她/他的 html 文件中使用波斯语句子,所以这个库不能转换波斯语单词。
为了解决这个问题和从右到左的问题,我使用下面的代码:
Document document = new Document(PageSize.A4, 80, 50, 30, 65);
PdfWriter.GetInstance(document, new FileStream(strPDFpath, FileMode.Create));
document.Open();
ArrayList objects;
document.NewPage();
var stream = new StreamReader(strHTMLpath, Encoding.Default).ReadToEnd();
objects = iTextSharp.text.html.simpleparser.
HTMLWorker.ParseToList(new StreamReader(strHTMLpath, Encoding.UTF8), styles);
BaseFont bf = BaseFont.CreateFont("c:\\windows\\fonts\\Tahoma.ttf",
BaseFont.IDENTITY_H, true);
for (int k = 0; k < objects.Count; k++)
{
PdfPTable table = new PdfPTable(1);
table.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
var els = (IElement)objects[k];
foreach (Chunk el in els.Chunks)
{
#region set persian font
iTextSharp.text.Font f2 = new iTextSharp.text.Font(bf, el.Font.Size,
el.Font.Style, el.Font.Color);
el.Font = f2;
#endregion set persian font
#region Set right to left for persian words
PdfPCell cell = new PdfPCell(new Phrase(10, el.Content, el.Font));
cell.BorderWidth = 0;
table.AddCell(cell);
#endregion Set right to left for persian words
}
//document.Add((IElement)objects[k]);
document.Add(table);
}
document.Close();
Response.Write(strPDFpath);
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment; filename=" + strPDFpath);
Response.ContentType = "application/octet-stream";
Response.WriteFile(strPDFpath);
Response.Flush();
Response.Close();
if (File.Exists(strPDFpath))
{
File.Delete(strPDFpath);
}
我的从右到左和转换波斯语已经解决了,但它有另一个问题。
我的算法无法解析和转换html文件中使用的table标签的内容。
现在的问题是:如何用波斯语句子解析带有table标签、div和段落标签的html文件,并将其转换为pdf?
【问题讨论】:
-
"我的算法无法解析和转换 html 文件中使用的 table 标记的内容。" - 意思是对象不包含原始 html 文档的表格或什么?
-
Hello Kia Salam aziz ;) 看到这个链接hasheminezhad.com/itextsharp
标签: c# asp.net html localization