首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ItextSharp PDF转换

ItextSharp PDF转换
EN

Stack Overflow用户
提问于 2017-09-06 13:00:44
回答 1查看 203关注 0票数 0

我在html编辑器中创建了具有输出的表。

代码语言:javascript
复制
<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格式,如下所示

代码语言:javascript
复制
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‘”}

我可能需要考虑切换其他选项,如果这是行不通的,我还没有找到任何解决方案。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-07 14:33:51

如果您习惯于使用iText,请考虑使用pdfHTML。它是一个iText7加载项,它以一种远优于XMLWorker的方式提供了从HTML到PDF的转换。

小代码示例:

代码语言:javascript
复制
    // 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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46075957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档