如何使用iTextSharp创建包含图像和表格的pdf文件

时间:2012-06-22 08:28:17

标签: itextsharp

public void genreatePdf()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<table>");
        sb.Append("<tr><td><img src='images/my.jpg'/></td></tr>");
        sb.Append("<tr><td>some text</td></tr>");
        sb.Append("</table>");

        string path = Server.MapPath("~/invoice/invoice.pdf");
        Document document = new Document(PageSize.A4);
        PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));
        document.Open();
        document.Add(new Paragraph(strMailMsg.ToString()));
        document.Close();
    }

pdf文件的输出是

<table><tr><td><img src='images/my.jpg'/></td></tr><tr><td>some text</td></tr></table>

qus:预期的格式就像一张包含图像和文字的表格。为什么不在该格式上创建pdf文件。

1 个答案:

答案 0 :(得分:0)

您需要创建一个表并将其附加到pdf文件中。它不是通过附加XHTML来完成的,而是使用C#的内部表。

Table aTable = new Table(2, 2);    // 2 rows, 2 columns
theTable.AddCell("0.0");
theTable.AddCell("0.1");
theTable.AddCell("1.0");
theTable.AddCell("1.1");

//add the table to the document
document.Add(theTable);

here是一个使用itextsharp的简单教程。

相关问题