Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.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
C# 将每页3行添加到IText文档_C#_Itext - Fatal编程技术网

C# 将每页3行添加到IText文档

C# 将每页3行添加到IText文档,c#,itext,C#,Itext,我希望从一个datatable中每页输出3行到一个itextsharp文档中,但它只是将所有行合并到一个表中,有人能告诉我我的错误吗 //Create new table and set columns and widths for Report Items PdfPTable itemTable = new PdfPTable(4); //Loop each row in

我希望从一个datatable中每页输出3行到一个itextsharp文档中,但它只是将所有行合并到一个表中,有人能告诉我我的错误吗

                    //Create new table and set columns and widths for Report Items
                    PdfPTable itemTable = new PdfPTable(4);

                    //Loop each row in the dataset and output
                    for (int i = 0; i < repDT.Rows.Count; i += 3)
                    {
                        float[] ITWidths = new float[] { 10f, 20f, 10f, 60f };  //4 columns by size (f) for list of multi-items on report
                        itemTable.SetWidths(ITWidths);

                        PdfPCell datesvalues = new PdfPCell(new Phrase("" + repDT.Rows[i]["OLDDATE"] + "\n" + Convert.ToDateTime(repDT.Rows[i]["INSDATE"]).ToString("dd/MM/yyyy") + "\n" + Convert.ToDateTime(repDT.Rows[i]["NEXT DATE"]).ToString("dd/MM/yyy") + "\n" + repDT.Rows[i]["CONT FREQ"].ToString() + " months", smalltext));
                        itemTable.AddCell(datesvalues);
                        PdfPCell itemdetails = new PdfPCell(new Phrase("1. " + repDT.Rows[i]["DESC"] + "\n" + "2. " + repDT.Rows[i]["PLANTNUMBER"] + "\n" + "3. " + repDT.Rows[i]["SERIALNUMBER"] + "\n" + "4. " + repDT.Rows[i]["SUBLOC"].ToString(), smalltext));
                        itemTable.AddCell(itemdetails);
                        PdfPCell swl = new PdfPCell(new Phrase("" + repDT.Rows[i]["SWL"], smalltext));
                        itemTable.AddCell(swl);
                        PdfPCell defects = new PdfPCell(new Phrase("A. " + repDT.Rows[i]["ADEFECT"] + "\n" + "B. " + repDT.Rows[i]["BDEFECT"] + "\n" + "C. " + repDT.Rows[i]["OBS"] + "\n" + "" + repDT.Rows[i]["SPARE1"].ToString(), smalltext));
                        itemTable.AddCell(defects);

                        document.NewPage();
                    }

                    document.Add(itemTable);
//创建新表并设置报表项的列和宽度
PdfPTable itemTable=新的PdfPTable(4);
//循环数据集中的每一行并输出
对于(int i=0;i
您的代码不会向循环中的文档添加任何内容,因此忽略其中的
document.NewPage()
调用

只需拉入实例化并记录将表添加到循环中:

//Loop each row in the dataset and output
for (int i = 0; i < repDT.Rows.Count; i += 3)
{
    //Create new table and set columns and widths for Report Items
    PdfPTable itemTable = new PdfPTable(4);
    float[] ITWidths = new float[] { 10f, 20f, 10f, 60f };  //4 columns by size (f) for list of multi-items on report
    itemTable.SetWidths(ITWidths);

    PdfPCell datesvalues = new PdfPCell(new Phrase("" + repDT.Rows[i]["OLDDATE"] + "\n" + Convert.ToDateTime(repDT.Rows[i]["INSDATE"]).ToString("dd/MM/yyyy") + "\n" + Convert.ToDateTime(repDT.Rows[i]["NEXT DATE"]).ToString("dd/MM/yyy") + "\n" + repDT.Rows[i]["CONT FREQ"].ToString() + " months", smalltext));
    itemTable.AddCell(datesvalues);
    PdfPCell itemdetails = new PdfPCell(new Phrase("1. " + repDT.Rows[i]["DESC"] + "\n" + "2. " + repDT.Rows[i]["PLANTNUMBER"] + "\n" + "3. " + repDT.Rows[i]["SERIALNUMBER"] + "\n" + "4. " + repDT.Rows[i]["SUBLOC"].ToString(), smalltext));
    itemTable.AddCell(itemdetails);
    PdfPCell swl = new PdfPCell(new Phrase("" + repDT.Rows[i]["SWL"], smalltext));
    itemTable.AddCell(swl);
    PdfPCell defects = new PdfPCell(new Phrase("A. " + repDT.Rows[i]["ADEFECT"] + "\n" + "B. " + repDT.Rows[i]["BDEFECT"] + "\n" + "C. " + repDT.Rows[i]["OBS"] + "\n" + "" + repDT.Rows[i]["SPARE1"].ToString(), smalltext));
    itemTable.AddCell(defects);

    document.Add(itemTable);

    document.NewPage();
}
//循环数据集中的每一行并输出
对于(int i=0;i
为什么要将
itemTable
添加到未知的
mTable
而不是
文档中?mkl,我有一个主表,用于在文档中添加其他项。这就是我出错的地方吗?好吧,如果你使用主表,那些
document.NewPage()
没有任何效果,因为它们都发生在添加主表之前,iText不会添加不必要的空白页。感谢你的帮助,mkl,我会试一试。mkl,我已经修改了上面的代码并删除了冗余的主表,将新表(itemtable)移出循环,但它仍然将itemtable显示为一个表,每3行之间没有新页面?我做错了什么?谢谢你的帮助。非常好的mkl,谢谢你的帮助。