使用ItextSharp从html到Pdf创建Pdf时未知颜色格式异常

本文关键字:Pdf 未知 颜色 格式 异常 创建 html 使用 ItextSharp | 更新日期: 2023-09-27 18:12:36

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它给出的异常未知的颜色格式

使用ItextSharp从html到Pdf创建Pdf时未知颜色格式异常

您将需要张贴的HTML,让我们看到究竟是什么导致错误。但是最终错误信息应该是非常明显的。HTML的某些部分有一些无效的RGB值,就iTextSharp而言。根据规范(链接到5,但与以前的版本相同),HTML中的所有颜色都应该采用#RRGGBB的格式。然而,iText似乎也允许#RGB,尽管我认为它是用于CSS解析的。最后,如果你使用CSS,你可以使用rgb(R,G,B)

最新版本(5.1.1.2)的第260行显示了抛出实际异常的行。