How do I insert a table into a PDF document with iTextSharp?

旧街凉风 提交于 2019-12-06 02:37:00

Although the code seems ok, I'm a bit confused by your line

Table t = new Table(columns, rows);

Are you sure thats what you want and not PdfPTable. Everything else in your code seems to be using PdfPTable, and I haven't been able to find a plain Table in iTextSharp.

And coincidently enough, I'm working on pretty similar thing right now.

EDIT For Modified Code

I've cleaned up the fields that aren't being used anymore as well:

public static byte[] InsertTable(byte[] buffer, DataTable dt, int columns, float[] columnWidths)
    {
        using (MemoryStream inputPDF = new MemoryStream(buffer))
        using (MemoryStream outputPDF = new MemoryStream())
        {
            PdfReader reader = new PdfReader(inputPDF);
            iTextSharp.text.Document doc = new iTextSharp.text.Document();
            PdfWriter write = PdfWriter.GetInstance(doc, outputPDF);
            doc.Open();

            for (int i = 1; i <= reader.NumberOfPages; i++)
            {
                doc.NewPage();
                write.DirectContent.AddTemplate(write.GetImportedPage(reader, i), 1f, 0, 0, 1, 0, 0);
            }

            //adding my table
            PdfPTable t = new PdfPTable(columns);
            t.SetTotalWidth(columnWidths);

            foreach (DataRow dr in dt.Rows)
                foreach (object o in dr.ItemArray)
                {
                    PdfPCell c = new PdfPCell();
                    c.AddElement(new Chunk(o.ToString()));
                    t.AddCell(c);
                }

            doc.NewPage();

            doc.Add(t);
            doc.Close();
            write.Close();
            reader.Close();
            return outputPDF.ToArray();
        }
    }

Hope this solves your problem. I'm online for next six hours and will try to keep an eye on this until I head back home :)

Ok, I updated to 5.0.6 And I got the table inserted. Right now everything is getting rendered on the first page though.

public static byte[] InsertTable(byte[] pdf, DataTable dt, int pageNum, float x, float y, int columns, int rows, float[] columnWidths, float rowHeight)
{
    using (var inputPDF = new MemoryStream(pdf))
    using (var outputPDF = new MemoryStream())
    {
        //loading existing
        var reader = new PdfReader(inputPDF);
        Document doc = new Document();
        PdfWriter write = PdfWriter.GetInstance(doc, outputPDF);
        doc.Open();
        PdfContentByte canvas = write.DirectContent;
        PdfImportedPage page;
        for (int i = 1; i <= reader.NumberOfPages; i++) {
            page = write.GetImportedPage(reader, i);
            canvas.AddTemplate(page, 1f, 0, 0, 1, 0, 0);
        }

        //adding my table
        PdfPTable t = new PdfPTable(columns);
        t.SetTotalWidth(columnWidths);

        foreach (DataRow dr in dt.Rows)
            foreach (object o in dr.ItemArray)
            {
                PdfPCell c = new PdfPCell();
                c.AddElement(new Chunk(o.ToString()));
                t.AddCell(c);
            }
        doc.Add(t);
        doc.Close();
        return outputPDF.ToArray();
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!