页眉

2017-08-01 59 views
0

下页继续PDFtable这是我的代码:页眉

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

不起作用。谢谢

+1

是否有原因,为什么你使用WriteSelectedRows()而不是'document.Add()'?如果你使用'document.Add()',你可以自动重复标题。现在,由于您将第二个参数定义为“-1”,因此您正在使用WriteSelectedRows()来添加*所有行。如果你只想显示50行,你为什么要这样做?为什么你的代码与你的要求相矛盾? –

+0

@BrunoLowagie谢谢你指出我正确的方向...... –

回答

2

以下是@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(); 
+1

我冒昧地添加一行:'table.HeaderRows = 1;'用这一行,标题行将在每一页上重复(这是一个OP提到的要求)。 –

+0

@BrunoLowagie Thx :) – Ben

+1

不客气。我也提出了答案。做得好! –