使用asp.net中的iTextSharp将HTML表格的内容写入PDF文档

时间:2013-07-21 19:52:15

标签: asp.net c#-4.0 pdf-generation itextsharp

我有一个存储在字符串中的HTML表。我想使用iTextSharp库将该字符串写入PDF文档。请提出建议。下面是我想用PDF文件写的表

<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>

1 个答案:

答案 0 :(得分:4)

它是可能的。 试试这个。

    //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();
    }