在asp.net中使用iTextSharp的HTML表格的内容写入到PDF文档 [英] Writing content of HTML table into PDF doc using iTextSharp in asp.net

查看:101
本文介绍了在asp.net中使用iTextSharp的HTML表格的内容写入到PDF文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有存储在一个字符串的HTML表格。我想写一个字符串转换成使用iTextSharp的库中的PDF文档。请建议的方法。下面是我想在PDF文件中写入表

 <表>
&所述; TR>
        <th>test</th><td>&nbsp;&nbsp;</td><td>ABCD</td><td>&nbsp;&nbsp;</td><td>&nbsp;&nbsp;</td><td>&nbsp;&nbsp;</td>
    &所述; / TR&GT;&下; TR&GT;
        &LT;第i个测试2</th><td>&nbsp;&nbsp;</td><td>XYZ</td><td>&nbsp;&nbsp;</td><td>&nbsp;&nbsp;</td><td>&nbsp;&nbsp;</td>
    &LT; / TR&GT;
&LT; /表&gt;


解决方案

它是可能的。
试试这个。

  // HTMLString =传递你的HTML,fileLocation =文件存储位置
    公共无效converttopdf(字符串HTMLString,串fileLocation)
    {
        文档的文档=新的文件();        PdfWriter.GetInstance(文件,新的FileStream(fileLocation,FileMode.Create));
        document.Open();        清单&LT; IElement&GT; htmlarraylist = HTMLWorker.ParseToList(新StringReader(HTMLString),NULL);
        对于(INT K = 0; K&LT; htmlarraylist.Count; k ++)
        {
            document.Add((IElement)htmlarraylist [K]);
        }        document.Close();
    }

I have an HTML table stored in a string. I want to write that string into a PDF document using iTextSharp library. Please suggest the approach. Below is the table I want to write in a PDF file

<table>
<tr>
        <th>test</th><td>&nbsp;&nbsp;</td><td>ABCD</td><td>&nbsp;&nbsp;</td><td>&nbsp;&nbsp;</td><td>&nbsp;&nbsp;</td>
    </tr><tr>
        <th>test 2</th><td>&nbsp;&nbsp;</td><td>XYZ</td><td>&nbsp;&nbsp;</td><td>&nbsp;&nbsp;</td><td>&nbsp;&nbsp;</td>
    </tr>
</table>

解决方案

Its is possible. try this.

    //HTMLString = Pass your Html , fileLocation = File Store Location
    public void converttopdf(string HTMLString, string fileLocation)
    {
        Document document = new Document();

        PdfWriter.GetInstance(document, new FileStream(fileLocation, FileMode.Create));
        document.Open();

        List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(HTMLString), null);
        for (int k = 0; k < htmlarraylist.Count; k++)
        {
            document.Add((IElement)htmlarraylist[k]);
        }

        document.Close();
    }

这篇关于在asp.net中使用iTextSharp的HTML表格的内容写入到PDF文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆