How to insert a table into a PDF using iTextSharp?

I want to insert a table into my existing PDF document.

I read a message about inserting an image into a PDF, but when I try to add a table, I get an error with a null link error.

Here is my current code

public static byte[] InsertTable(byte[] pdf, DataTable dt, int pageNum, int x, int y, int columns, int rows, int[] columnWidths, float rowHeight)
{
    using (var inputPDF = new MemoryStream(pdf))
    using (var outputPDF = new MemoryStream())
    {
        var reader = new PdfReader(inputPDF);
        var stamper = new PdfStamper(reader, outputPDF);
        var pdfContentByte = stamper.GetOverContent(pageNum);


        Table t = new Table(columns, rows);
        t.SetWidths(columnWidths);

        foreach (DataRow dr in dt.Rows)
            foreach (object o in dr.ItemArray)
            {
                Cell c = new Cell();
                c.Add(new Chunk(o.ToString()));
                t.AddCell(c);
            }
        pdfContentByte.PdfDocument.Add(t);
        pdfContentByte.PdfDocument.Close();
        return outputPDF.ToArray();
    }
}
+3
source share
2 answers

Ok, I upgraded to 5.0.6 And I inserted a table. Now everything turns out on the first page.

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

Although the code looks fine, I'm a little confused by your line

Table t = new Table(columns, rows);

Are you sure you want, not PdfPTable. Everything else in your code seems to be using PdfPTable, and I couldn't find a simple one Tablein iTextSharp.

And, strangely enough, now I'm working on pretty similar things.

EDIT for modified code

, :

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

, . , :)

+2

Source: https://habr.com/ru/post/1793952/


All Articles