Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ItextSharp在其他页面上添加数据_Itextsharp - Fatal编程技术网

使用ItextSharp在其他页面上添加数据

使用ItextSharp在其他页面上添加数据,itextsharp,Itextsharp,这个问题是关于使用itextSharp创建pdf的 我在文档中的第一行添加了表,我手动添加了名称、Id等,表名和Id来自数据库,现在我的问题是,当页面再次结束时,我想手动添加相同的行,例如名称、Id和数据将继续来自数据库,所以我该怎么办??请帮我解决这些问题。这完全取决于您如何创建文档。您是附加到现有文档,还是从头开始生成新文档?无论哪种方式,您都可能最终使用PageEvents来完成此任务。您可以编写自定义的OnPageEnd事件,以便在编写器到达新页面的开头时“重写”表格标题。如果您是从“从

这个问题是关于使用itextSharp创建pdf的


我在文档中的第一行添加了表,我手动添加了名称、Id等,表名和Id来自数据库,现在我的问题是,当页面再次结束时,我想手动添加相同的行,例如名称、Id和数据将继续来自数据库,所以我该怎么办??请帮我解决这些问题。

这完全取决于您如何创建文档。您是附加到现有文档,还是从头开始生成新文档?无论哪种方式,您都可能最终使用
PageEvents
来完成此任务。您可以编写自定义的
OnPageEnd
事件,以便在编写器到达新页面的开头时“重写”表格标题。如果您是从“从头开始”的角度进行此操作,可能会变得很棘手,但只要您在创建表的过程中触发事件,就应该不会有问题。如果要附加到现有文档,则最好创建一个包含表的文件,并在创建完表后将它们合并在一起。不管怎样,这都不是一个特别复杂的过程。如果在for或while循环中创建表,如:

for(int i = 0; a < 100; i++)
{
    //OnPageEnd Event that adds the Table Header
    table.addCell(cell)
} 

非常感谢Mike,但是如果这是最后一页,没有找到表的记录,只感谢文档中剩余的单词,然后还会在我的PDF的最后一页创建页眉,我不想要最后一页的页眉,你能告诉我该怎么办吗?很抱歉,回复时间太长,我一直在忙于一个新项目。然而,今天我不得不重温这一部分,所以我认为传播我所学到的东西是合适的。我将编辑我的答案,以包含您向我介绍的新方案。此方法实际上是一次创建整个表。完成后,如果合适,它将尝试将其写入一个页面,但如果onpageend事件触发,它知道需要一个新页面,因此它将设置该新页面,并在停止写入表的位置继续。需要指出的一件有趣的事情是,在编写初始文档和本附录时声明了页边距。您需要根据页眉和页脚的大小等进行试验,以使其正确。干杯!
private void appendTable(string inputFile, string outputFile, string id)
{
    Document outputDoc = new Document(PageSize.LETTER, 15, 15, 35, 50);
    PdfReader reader = new PdfReader(inputFile);
    string sql = string.Format("SELECT * FROM table WHERE id='{0}'", id);
    try
    {
        DataTable dt = db.GetDataTable(sql);
        PdfPTable table = new PdfPTable(1);
        iTextSharp.text.Font arial = FontFactory.GetFont("Arial", 8);
        iTextSharp.text.Font arialheader = FontFactory.GetFont("Arial", 12);
        table.WidthPercentage = 95;
        PdfPCell headerCell = new PdfPCell(new Phrase("Cell Header", arialheader));
        headerCell.HorizontalAlignment = 1;
        table.AddCell(headerCell);

        PdfWriter wr = PdfWriter.GetInstance(outputDoc, new FileStream(outputFile, FileMode.Create, FileAccess.ReadWrite));
        foreach(DataRow row in dt.Rows)
        {
            PdfPCell cell = new PdfPCell(new Phrase("Cell Text", arial));
            meterCell.HorizontalAlignment = 1; //Center Aligned
            table.AddCell(meterCell);
        }

        outputDoc.Open();
        wr.PageEvent = new PageEvents.Header();
        wr.PageEvent = new PageEvents.Footer();
        outputDoc.Add(table);
        outputDoc.NewPage();
        outputDoc.Close();
    }

    catch (PdfException ex)
    {
        MessageBox.Show(ex.Message.ToString() + " **** " + ex.StackTrace);
    }
    catch (NpgsqlException ex)
    {
    MessageBox.Show(ex.Message.ToString() + " **** " + ex.StackTrace);
    }
 }