我在html编辑器中创建了具有输出的表。
<table style="width: 100%;">
<tbody>
<tr>
<td style="width: 50.0000%;"><a href="http://www.google.com">sdfsdf</a><br></td>
<td style="width: 50.0000%;"><br></td>
</tr>
<tr>
<td style="width: 50.0000%;"><br></td>
<td style="width: 50.0000%;"><br></td>
</tr>
</tbody>
</table>并将Itextsharp html转换为PDF格式,如下所示
private List<IElement> GetXHtmlElementList(string html)
{
if (!string.IsNullOrEmpty(html) && html.Contains("<img"))
{
html = html.Replace("<img", "<br/><img");
}
List<IElement> list = new List<IElement>();
Dictionary<string, object> interfaceProps = new Dictionary<string, object>() {
{"img_provider", new CustomItextImageProvider()}
};
if (html != null)
{
var encodedHTML = HttpUtility.HtmlEncode(html);
if(encodedHTML != null)
using (StringReader sr = new StringReader(HttpUtility.HtmlDecode(encodedHTML)))
{
list = ParseToList(sr, null, interfaceProps);
}
}
return list;
}但是在ParseToList中会出现以下错误
{“无法将'iTextSharp.text.html.simpleparser.CellWrapper‘类型的对象强制转换为’iTextSharp.text.Paragploy‘”}
我可能需要考虑切换其他选项,如果这是行不通的,我还没有找到任何解决方案。
发布于 2017-09-07 14:33:51
如果您习惯于使用iText,请考虑使用pdfHTML。它是一个iText7加载项,它以一种远优于XMLWorker的方式提供了从HTML到PDF的转换。
小代码示例:
// IO
String htmlSource = "<p>Lorem Ipsum Dolor Sit Amet</p>";
File pdfDest = getOutputFile();
// pdfHTML specific code
ConverterProperties converterProperties = new ConverterProperties();
HtmlConverter.convertToPdf(
new FileInputStream(htmlSource),
new FileOutputStream(pdfDest),
converterProperties);欲了解更多信息,请访问https://itextpdf.com/itext7/pdfHTML
https://stackoverflow.com/questions/46075957
复制相似问题