簡體   English   中英

如何使用iTextSharp將表格插入PDF文檔?

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

我想在現有的PDF文檔中插入一個表格。

我正在關注一個關於將圖像插入PDF的帖子,但是在嘗試添加表時我得到一個空引用異常錯誤。

這是我目前的代碼

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

雖然代碼似乎沒問題,但我對你的行感到有些困惑

Table t = new Table(columns, rows);

你確定那是你想要的而不是PdfPTable 您的代碼中的其他所有內容似乎都在使用PdfPTable ,而我無法在iTextSharp找到普通的Table

而且巧合的是,我現在正在做類似的事情。

編輯修改后的代碼

我已經清理了不再使用的字段了:

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

希望這能解決你的問題。 我將在接下來的六個小時在線,並會盡力關注這一點,直到我回到家:)

好的,我更新到5.0.6然后我插入了表格。 現在一切都在第一頁上呈現。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM