C# 在下一页上继续使用标题的PDFtable

C# 在下一页上继续使用标题的PDFtable,c#,itext,pdf-generation,C#,Itext,Pdf Generation,这是我的代码: PdfPTable tableSumme = new PdfPTable(dtUebersicht.Columns.Count-1); widthsSumme = new float[] { 4.2f, 5f, 5f, 5f, 5f }; tableSumme.SetWidths(widthsSumme); tableSumme.WidthPercentage = 100; tableSumme.TotalWidth = 500f; foreach (DataColumn c

这是我的代码:

PdfPTable tableSumme = new PdfPTable(dtUebersicht.Columns.Count-1);
widthsSumme = new float[] { 4.2f, 5f, 5f, 5f, 5f };
tableSumme.SetWidths(widthsSumme);
tableSumme.WidthPercentage = 100;
tableSumme.TotalWidth = 500f;


foreach (DataColumn c in dtUebersicht.Columns)
    {
PdfPCell Spalte = new PdfPCell(new Phrase(c.ColumnName, VerdanaFont));
                            Spalte.HorizontalAlignment = Element.ALIGN_CENTER;
                            Spalte.VerticalAlignment = Element.ALIGN_MIDDLE;
                            tableSumme.AddCell(Spalte);
    }

PdfContentByte cbSumme = writerSumme.DirectContent;


foreach (DataRow dr in dtUebersicht.Rows)
{
PdfPCell Spalte0 = new PdfPCell(new Phrase(dr[0].ToString(), VerdanaFont));
                                    Spalte0.HorizontalAlignment = Element.ALIGN_CENTER;
                                    Spalte0.VerticalAlignment = Element.ALIGN_MIDDLE;

PdfPCell Spalte1 = new PdfPCell(new Phrase(dr[1].ToString(), VerdanaFont));
                                    Spalte1.HorizontalAlignment = Element.ALIGN_CENTER;
                                    Spalte1.VerticalAlignment = Element.ALIGN_MIDDLE;

tableSumme.AddCell(Spalte0);
tableSumme.AddCell(Spalte1);
}

tableSumme.WriteSelectedRows(0, -1, 35, 757, cbSumme);
这给了我一个带有一页和数据的PDF。数据比页面长,所以我想每50行插入一个新页面。我怎样才能解决这个问题

简单的

 if (Rowindex % 50 == 0)
   { documentSumme.NewPage(); }

不起作用。谢谢

下面是@Bruno Lowagie评论后的一个小例子。 当在添加所有行之前到达页面末尾时,它们将添加到下一页

Document doc = new Document();
FileStream fs = new FileStream(@"your path", FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
doc.Open();

List<string> columns = new List<string> {"col1", "col2", "col3", "col4", "col5"};

PdfPTable table = new PdfPTable(columns.Count);
table.SetWidths(new[] { 5f, 5f, 5f, 5f, 5f });
table.WidthPercentage = 100;
table.TotalWidth = 500f;
table.HeaderRows = 1;

foreach (string col in columns)
{
    PdfPCell cell = new PdfPCell(new Phrase(col));
    table.AddCell(cell);
}

for (int i = 0; i < 100; i++)
{
    for (int j = 0; j < columns.Count; j++)
    {
        PdfPCell cell = new PdfPCell(new Phrase($"{i},{j}"));
        table.AddCell(cell);
    }
}

doc.Add(table);
doc.Close();
Document doc=新文档();
FileStream fs=newfilestream(@“您的路径”,FileMode.Create,FileAccess.Write);
PdfWriter writer=PdfWriter.GetInstance(doc,fs);
doc.Open();
列表列=新列表{“col1”、“col2”、“col3”、“col4”、“col5”};
PdfPTable table=新的PdfPTable(columns.Count);
表.设置宽度(新[{5f,5f,5f,5f,5f});
表1.1:百分比=100;
表1.2总宽度=500f;
表1.1-1;
foreach(列中的字符串列)
{
PdfPCell cell=新的PdfPCell(新短语(col));
表2.AddCell(cell);
}
对于(int i=0;i<100;i++)
{
对于(int j=0;j
下面是@Bruno Lowagie评论之后的一个小例子。 当在添加所有行之前到达页面末尾时,它们将添加到下一页

Document doc = new Document();
FileStream fs = new FileStream(@"your path", FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
doc.Open();

List<string> columns = new List<string> {"col1", "col2", "col3", "col4", "col5"};

PdfPTable table = new PdfPTable(columns.Count);
table.SetWidths(new[] { 5f, 5f, 5f, 5f, 5f });
table.WidthPercentage = 100;
table.TotalWidth = 500f;
table.HeaderRows = 1;

foreach (string col in columns)
{
    PdfPCell cell = new PdfPCell(new Phrase(col));
    table.AddCell(cell);
}

for (int i = 0; i < 100; i++)
{
    for (int j = 0; j < columns.Count; j++)
    {
        PdfPCell cell = new PdfPCell(new Phrase($"{i},{j}"));
        table.AddCell(cell);
    }
}

doc.Add(table);
doc.Close();
Document doc=新文档();
FileStream fs=newfilestream(@“您的路径”,FileMode.Create,FileAccess.Write);
PdfWriter writer=PdfWriter.GetInstance(doc,fs);
doc.Open();
列表列=新列表{“col1”、“col2”、“col3”、“col4”、“col5”};
PdfPTable table=新的PdfPTable(columns.Count);
表.设置宽度(新[{5f,5f,5f,5f,5f});
表1.1:百分比=100;
表1.2总宽度=500f;
表1.1-1;
foreach(列中的字符串列)
{
PdfPCell cell=新的PdfPCell(新短语(col));
表2.AddCell(cell);
}
对于(int i=0;i<100;i++)
{
对于(int j=0;j
您使用
WriteSelectedRows()
而不是
document.Add()
有什么原因吗?如果使用
document.Add()
,则可以自动重复标题。现在,您正在使用
WriteSelectedRows()
添加所有行,因为您将第二个参数定义为
-1
。如果只想显示50行,为什么要这样做?为什么您的代码与您的要求相矛盾?@BrunoLowagie感谢您为我指明了正确的方向……您使用
WriteSelectedRows()
而不是
document.Add()
有什么原因吗?如果使用
document.Add()
,则可以自动重复标题。现在,您正在使用
WriteSelectedRows()
添加所有行,因为您将第二个参数定义为
-1
。如果只想显示50行,为什么要这样做?为什么您的代码与您的要求相矛盾?@BrunoLowagie感谢您为我指明了正确的方向……我冒昧地添加了一行:
table.HeaderRows=1有了这一行,标题行将在每一页上重复(这是OP提到的要求)。@BrunoLowagie Thx:)不客气。我还对答案投了赞成票。做得好!我随意添加了一行:
table.HeaderRows=1有了这一行,标题行将在每一页上重复(这是OP提到的要求)。@BrunoLowagie Thx:)不客气。我还对答案投了赞成票。做得好!