user1242717
user1242717

Reputation: 11

how to insert table for existing form fill pdf using itextsharp

I have a form fill pdf where we dynamically add text field values to pdf. after adding this i need to add the table in the same page of the pdf. if i add table it creates new pdf with only table. all other existing data are cleared. I am using below code :

    private void AddTableToPDF()
    {

        Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 100, 100);
        try
        {
            string pdfFilePath = @"D:\Temp\PDF\Inspection Form - Steel Girder.pdf";

            PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(pdfFilePath, FileMode.Append));

            doc.Open();//Open Document to write           

            System.IO.MemoryStream mStream = new System.IO.MemoryStream();
            PdfWriter writer = PdfWriter.GetInstance(doc, mStream);               

            DataTable dt = GetDataTable();
            DataTable dtHeader = new DataTable();
            dtHeader = GetHeaderDataTable();             


            if (dtHeader != null)
            {
                PdfPTable PdfTable = new PdfPTable(dtHeader.Columns.Count);
                PdfPCell PdfPCell = null;

                for (int rows = 0; rows < dtHeader.Rows.Count; rows++)
                {
                    for (int column = 0; column < dtHeader.Columns.Count; column++)
                    {
                        PdfPCell = new PdfPCell(new Phrase(new Chunk(dtHeader.Rows[rows][column].ToString(), font8)));
                        PdfTable.AddCell(PdfPCell);
                    }
                }               

                doc.Add(PdfTable); // add pdf table to the document

            }

            if (dt != null)
            {                    
                PdfPTable PdfTable = new PdfPTable(dt.Columns.Count);
                PdfPCell PdfPCell = null;


                PdfPCell = new PdfPCell(new Phrase(new Chunk("Reference", font8)));
                PdfTable.AddCell(PdfPCell);

                PdfPCell = new PdfPCell(new Phrase(new Chunk("Remark", font8)));
                PdfTable.AddCell(PdfPCell);

                PdfPCell = new PdfPCell(new Phrase(new Chunk("Description", font8)));
                PdfTable.AddCell(PdfPCell);


                for (int rows = 0; rows < dt.Rows.Count; rows++)
                {
                    for (int column = 0; column < dt.Columns.Count; column++)
                    {
                        PdfPCell = new PdfPCell(new Phrase(new Chunk(dt.Rows[rows][column].ToString(), font8)));
                        PdfTable.AddCell(PdfPCell);
                    }
                }

                doc.Add(PdfTable); // add pdf table to the document                 

            }


        }
        catch (DocumentException docEx)
        {
            Response.Write(docEx.Message);

        }
        catch (IOException ioEx)
        {
            Response.Write(ioEx.Message);

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);

        }
        finally
        {

            doc.Close();

        }
    }

Upvotes: 1

Views: 3194

Answers (1)

SHEKHAR SHETE
SHEKHAR SHETE

Reputation: 6066

Suppose your main PDF is named with m1.pdf. you just create a new PDF with name m2.pdf which will contain the new Table that you need to add. now just merge these Two PDF (m1.pdf & m2.pdf) in to new one 'MergedNerw.pdf')

To Merge Two PDF please use following code this Works fine i have Tested it:

protected void BtnMerge_Click(object sender, EventArgs e)
    {
        String[] files = @"d:\m1.pdf,d:\m2.pdf".Split(',');
        MergeFiles(@"d:\MergedNew.pdf", files);
    }

    public void MergeFiles(string destinationFile, string[] sourceFiles)
    {

        if (System.IO.File.Exists(destinationFile))
            System.IO.File.Delete(destinationFile);

        string[] sSrcFile;
        sSrcFile = new string[2];
        string[] arr = new string[2];
        for (int i = 0; i <= sourceFiles.Length - 1; i++)
        {
            if (sourceFiles[i] != null)
            {
                if (sourceFiles[i].Trim() != "")
                   arr[i] = sourceFiles[i].ToString();
            }
        }
        if (arr != null)
        {
            sSrcFile = new string[2];

            for (int ic = 0; ic <= arr.Length - 1; ic++)
            {
                sSrcFile[ic] = arr[ic].ToString();
            }
        }
        try
        {
            int f = 0;
            PdfReader reader = new PdfReader(sSrcFile[f]);
            int n = reader.NumberOfPages;
            //Response.Write("There are " + n + " pages in the original file.");
            Document document = new Document(PageSize.A4);
            PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create));
            document.Open();
            PdfContentByte cb = writer.DirectContent;
            PdfImportedPage page;
            int rotation;
            while (f < sSrcFile.Length)
            {
                int i = 0;
                while (i < n)
                {
                    i++;
                    document.SetPageSize(PageSize.A4);
                    document.NewPage();
                    page = writer.GetImportedPage(reader, i);
                    rotation = reader.GetPageRotation(i);
                    if (rotation == 90 || rotation == 270)
                    {
                        cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                    }
                    else
                    {
                        cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                    }
                    //Response.Write("\n Processed page " + i);
                }

                f++;
                if (f < sSrcFile.Length)
                {
                    reader = new iTextSharp.text.pdf.PdfReader(sSrcFile[f]);
                    //get the numnber of pages
                    n = reader.NumberOfPages;
                    //Response.Write("There are " + n + " pages in the original file.");
                }
            }
            //Response.Write("Success");
            document.Close();
        }
        catch (Exception e)
        {
            //Response.Write(e.Message);
        }
    }

Hope this helps you!

Upvotes: 1

Related Questions