我尝试使用 itext7 将带有html标签的字符串转换为pdf,如下面的代码所示。 doc.Add(...)
行引发了异常:
string text = "<p><span style='text-decoration: underline;'>UNDERLINE</span>;</p>";
IList<IElement> lst = HtmlConverter.ConvertToElements( text );
foreach( IElement e in lst )
{
doc.Add( (IBlockElement)e );
}
它会产生以下异常:
source: itext.layout
message: Unable to cast object of type 'System.Collections.Generic.List`1[iText.Layout.Properties.Underline]' to type 'System.Collections.Generic.IList`1[System.Object]'.
stack trace:
at iText.Layout.Renderer.AccessibleAttributesApplier.ApplyInlineLevelLayoutAttributes(AbstractRenderer renderer, PdfDictionary attributes)
at iText.Layout.Renderer.AccessibleAttributesApplier.ApplyLayoutAttributes(PdfName role, AbstractRenderer renderer, PdfDocument doc)
at iText.Layout.Renderer.TextRenderer.Draw(DrawContext drawContext)
at iText.Layout.Renderer.AbstractRenderer.DrawChildren(DrawContext drawContext)
at iText.Layout.Renderer.AbstractRenderer.Draw(DrawContext drawContext)
at iText.Layout.Renderer.ParagraphRenderer.DrawChildren(DrawContext drawContext)
at iText.Layout.Renderer.BlockRenderer.Draw(DrawContext drawContext)
at iText.Layout.Renderer.DocumentRenderer.FlushSingleRenderer(IRenderer resultRenderer)
at iText.Layout.Renderer.RootRenderer.ProcessRenderer(IRenderer renderer, IList`1 resultRenderers)
at iText.Layout.Renderer.RootRenderer.UpdateCurrentAreaAndProcessRenderer(IRenderer renderer, IList`1 resultRenderers, LayoutResult result)
at iText.Layout.Renderer.RootRenderer.AddChild(IRenderer renderer)
at iText.Layout.RootElement`1.Add(IBlockElement element)
at iText.Layout.Document.Add(IBlockElement element)
当字符串包含html链接时,我得到同样的异常:
<p><a href="http://www.google.com">google</a></p>