【问题标题】:Add table into existing PDF using iTExtsharp使用 iTExtsharp 将表格添加到现有 PDF
【发布时间】:2013-02-09 17:23:58
【问题描述】:

我有一个 PDF,其中有一个表格是动态的,我想在我现有的 PDF 中动态地将另一个表格添加到该表格中。

有什么方法可以在现有表格(不在文档末尾)完成的特定位置添加现有 PDF 中的表格,然后我想添加我的表格。

如何添加?请给我一些好的方法。

如下图所示。

谢谢,

【问题讨论】:

  • 你提到了 iTextSharp。该库允许使用 PdfStamper 类操作现有的 pdf。更有趣的是如何识别现有 PDF 中添加新数据的位置。
  • 嗨 Mkl 感谢您的 cmets。如何识别现有 pdf 中的位置,以便添加表格。你能举个例子吗?
  • 你能举个例子吗?不,因为你在问一些在很多情况下(不是全部)不可能的事情。这就是为什么 mkl 写了关于如何识别现有 PDF 中的位置的问题是一个更有趣的问题。如果您无法回答该问题(因为您可能会问一些不可能的问题),那么您的实际问题就无关紧要了。没有其他人可以为您回答这个问题,因为没有其他人知道现有的 PDF 是什么样的。
  • 嗨,布鲁诺,我已经添加了相同的示例 pdf 图像。正如您在上图中看到的那样,“订单中的项目”表是动态的,现在您可以在那里看到四个项目,但它可能在不同的 pdf 中非常不同。我想将下表添加到现有 pdf 中的该表中。希望你这是帮助你理解。谢谢,
  • 在您的情况下,这似乎是可能的。如果 Thank you for your business... 行始终是表格后的第一个文本,则可以使用该行和 Items in Your Order 来查找位置。我只是想知道什么样的桌子适合这么小的距离。我希望您不要也期望 Thank you... 行和下面的任何内容都会被移下,对吗?虽然并非不可能,但这将使其成为一个非常大的项目。

标签: c# asp.net pdf pdf-generation itextsharp


【解决方案1】:

到目前为止,最简单的方法是在所需位置创建一个带有所需表格的新 PDF,然后将其印在现有 PDF 上。这可以使用(例如)PdfStamper 类在代码中完成,但也有独立的工具,例如 pdftk 和许多其他工具。不要将其视为“编辑” PDF,将其视为在原始文件之上添加新内容。

pdftk original.pdf stamp newtable.pdf output combined.pdf

@mkl 的原始问题解决了真正有趣且可能困难的部分 - 你如何确定新桌子的正确位置。如果你能想出一个几何规则,那么你的状态就很好。如果这涉及对原始文件的任何解析,您应该知道,像确定现有表中的行数这样(看似)简单的事情有时可能非常困难。很有可能,在内容流中埋藏着一个 html <table> 标签。拥有一个实际 PDF 的示例将非常有帮助。 PDF 的图像不是一回事。

为您提供一些背景信息,解析 PDF 的 布局 很容易,这就是 PDF 阅读器所做的。解析 PDF 的 内容 完全不同,而且难度更大。例如,您发布的 PDF 图像可以从上到下绘制,或者可以先绘制页眉和页脚,然后是所有粗体项目,然后是纯文本。仅仅因为两个事物在物理布局中彼此相邻并不意味着它们在文件结构、对象树或内容流中彼此相邻。它是矢量图形而不是文本文件或位图。除非创建 PDF 的软件专门提供有关如何编辑内容的线索,否则 PDF 并非设计为可编辑的。有很多事情看起来应该很容易,但是一旦您了解了 PDF 的构建方式,就会发现这很困难。我这样说并不是要劝阻你,只是为了让你体会到这项任务的艰巨性。如果您可以追溯创建此 PDF 的源文档,我保证您会取得更大的成功,减少挫败感。

【讨论】:

    【解决方案2】:
    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();
    
           }
     }
    

    示例数据表:

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

    【讨论】:

    • 您的代码创建了一个新的 PDF,而原始问题要求将代码添加到现有的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多