添加到表使用iTextSharp的现有PDF [英] Add table into existing PDF using iTExtsharp

查看:238
本文介绍了添加到表使用iTextSharp的现有PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PDF有一个表的表是动态的,我要动态添加下面的表中的另一个表中现有的PDF文件。

有没有办法在现有的表(​​不是在文件末尾)完成,然后我想补充我的表中的特定位置添加了表中现有的PDF。

我如何添加?请建议我一些很好的方式。

正如你可以看到下面的图片。

谢谢,


解决方案

 使用iTextSharp.text;
使用iTextSharp.text.pdf;///功能,这将创建PDF文档,并保存在服务器文件夹私人无效ExportDataToPDFTable()
    {
      文档的文档=新文件(iTextSharp.text.PageSize.LETTER,10,10,42,35);
         尝试
          {
           串pdfFilePath =使用Server.Mappath(。)+/pdf/myPdf​​.pdf;
           //创建文档类对象和它的大小设置为信,并给予空间左,右,上,下​​边距
           PdfWriter WRI = PdfWriter.GetInstance(文件,新的FileStream(pdfFilePath,FileMode.Create));           doc.Open(); //打开文档编写           字体font8 = FontFactory.GetFont(ARIAL,7);           //写一些内容
            逐段=新段(使用iTextSharp的我将展示如何创建PDF文档简单的表);            DataTable的DT = GetDataTable();            如果(DT!= NULL)
                {
                 在PDF表格的//实例,了解创建并设置该表的列数
                 PdfPTable PdfTable =新PdfPTable(dt.Columns.Count);
                 PdfPCell PdfPCell = NULL;
                 //添加PDF表格的标题
                 PdfPCell =新PdfPCell(新乐句(新组块(ID,font8)));
                 PdfTable.AddCell(PdfPCell);                 PdfPCell =新PdfPCell(新乐句(新组块(名,font8)));
                 PdfTable.AddCell(PdfPCell);
                 //如何从数据表中的数据添加到PDF表
                 对于(INT行= 0;行和LT; dt.Rows.Count;行++)
                    {
                     对于(INT列= 0;&列LT; dt.Columns.Count;柱++)
                         {
                           PdfPCell =新PdfPCell(新乐句(新组块(dt.Rows [行] [列]的ToString(),font8)));
                            PdfTable.AddCell(PdfPCell);
                            }
                        }                        PdfTable.SpacingBefore = 15F; //给一些空间后的文本也可能重叠表                        doc.Add(段落); //添加段落到文档
                        doc.Add(PdfTable); // PDF表添加到文档                    }                }
                赶上(DocumentException docEx)
                {
                    //处理PDF文档异常,如果任何
                }
                赶上(IOException异常ioEx)
                {
                    //处理IO异常
                }
                赶上(异常前)
                {
                    //如果发生ahndle其他异常
                }
                最后
                {
                    //关闭文件和作家
                    doc.Close();       }
 }

样品数据表:

 私人数据表GetDataTable()
    {
        //创建DataTable类的对象
        DataTable中的dataTable =新的DataTable(MyDataTable); //创建ID的DataColumn
        DataColumn的dataColumn_ID =新的DataColumn(ID,typeof运算(的Int32));
        dataTable.Columns.Add(dataColumn_ID); //创建另一个DataColumn的名称
        DataColumn的dataColumn_Name =新的DataColumn(名,typeof运算(字符串));
        dataTable.Columns.Add(dataColumn_Name);
        //现在添加一些行新创建的dataTable
        DataRow的数据行;对于(INT I = 0;我小于5;我++)
        {
            数据行= dataTable.NewRow();
            //重要,你必须创建新的行
            数据行[ID] =我;数据行[名称] =一些文本+ i.ToString();
            dataTable.Rows.Add(数据行);
        }
        dataTable.AcceptChanges();
        返回的dataTable;
    }

I have one PDF in that there is one table that table is dynamic and I want to add another table below to that table dynamically in my existing PDF.

Is there any way to add the table in existing PDF at specific place that existing table(that is not at the end of document) is completed then I want to add my table.

How can I add? Please suggest me some good way.

As you can see below image.

Thanks,

解决方案

using iTextSharp.text;
using iTextSharp.text.pdf;

/// Function which will create pdf document and save in the server folder

private void ExportDataToPDFTable()
    {
      Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
         try
          {
           string pdfFilePath = Server.MapPath(".") + "/pdf/myPdf.pdf";
           //Create Document class object and set its size to letter and give space left, right, Top, Bottom Margin
           PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(pdfFilePath, FileMode.Create));

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

           Font font8 = FontFactory.GetFont("ARIAL", 7);

           //Write some content
            Paragraph paragraph = new Paragraph("Using ITextsharp I am going to show how to create simple table in PDF document ");

            DataTable dt = GetDataTable();

            if (dt != null)
                {
                 //Craete instance of the pdf table and set the number of column in that table
                 PdfPTable PdfTable = new PdfPTable(dt.Columns.Count);
                 PdfPCell PdfPCell = null;


                 //Add Header of the pdf table
                 PdfPCell = new PdfPCell(new Phrase(new Chunk("ID", font8)));
                 PdfTable.AddCell(PdfPCell);

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


                 //How add the data from datatable to pdf table
                 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);
                            }
                        }

                        PdfTable.SpacingBefore = 15f; // Give some space after the text or it may overlap the table

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

                    }

                }
                catch (DocumentException docEx)
                {
                    //handle pdf document exception if any
                }
                catch (IOException ioEx)
                {
                    // handle IO exception
                }
                catch (Exception ex)
                {
                    // ahndle other exception if occurs
                }
                finally
                {
                    //Close document and writer
                    doc.Close();

       }
 }

Sample Datatable:

private DataTable GetDataTable()
    {
        // Create an object of DataTable class
        DataTable dataTable = new DataTable("MyDataTable");//Create ID DataColumn
        DataColumn dataColumn_ID = new DataColumn("ID", typeof(Int32));
        dataTable.Columns.Add(dataColumn_ID);//Create another DataColumn Name
        DataColumn dataColumn_Name = new DataColumn("Name", typeof(string));
        dataTable.Columns.Add(dataColumn_Name);
        //Now Add some row to newly created dataTable
        DataRow dataRow;for (int i = 0; i < 5; i++)
        {
            dataRow = dataTable.NewRow();
            // Important you have create New row
            dataRow["ID"] = i;dataRow["Name"] = "Some Text " + i.ToString();
            dataTable.Rows.Add(dataRow);
        }
        dataTable.AcceptChanges();
        return dataTable;
    }

这篇关于添加到表使用iTextSharp的现有PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆