我有一个 PDF,其中有一个表是动态的,我想在我现有的 PDF 中动态地将另一个表添加到该表中。
有什么方法可以在现有表格(不在文档末尾)完成的特定位置添加现有 PDF 中的表格,然后我想添加我的表格。
我该如何添加?请给我一些好的方法。
如下图所示。

谢谢,
我有一个 PDF,其中有一个表是动态的,我想在我现有的 PDF 中动态地将另一个表添加到该表中。
有什么方法可以在现有表格(不在文档末尾)完成的特定位置添加现有 PDF 中的表格,然后我想添加我的表格。
我该如何添加?请给我一些好的方法。
如下图所示。

谢谢,
到目前为止,最简单的方法是在所需位置创建一个带有所需表格的新 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 的源文档,我保证您将获得更大的成功,减少挫败感。
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;
}