从HTML到pdf创建Pdf期间的未知颜色格式异常使用ItextSharp

时间:2011-10-28 15:40:18

标签: c# html pdf itextsharp

Document document = new Document();
string str = Pagehtml;

//writer - have our own path!!!
PdfWriter.GetInstance(document, new FileStream(Server.MapPath(".") + "parsetest.pdf", FileMode.Create));
document.Open();
//here when it parse the html gives exception unknown color format should be #RGB
ArrayList htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(str), null);

//add the collection to the document
for (int k = 0; k < htmlarraylist.Count; k++)
{
    document.Add((IElement)htmlarraylist[k]);
}

这是我的代码,我在其中解析html代码但是当它解析html时它会提供未知颜色格式的异常

1 个答案:

答案 0 :(得分:0)

您需要为我们发布HTML,以确切了解导致错误的原因。但最终错误信息应该非常明显。就iTextSharp而言,HTML的某些部分具有一些无效的RGB值。 Per spec(链接到5但与之前版本相同)HTML中的所有颜色都应采用#RRGGBB格式。但是,iText似乎也允许#RGB,尽管我希望它可用于CSS解析。最后,如果您使用的是CSS,则可以使用rgb(R,G,B)

Line 260 of the most recent version (5.1.1.2) shows you the line throwing the actual exception.