2010-02-23 4 views
5

J'ai besoin d'une table avec environ 12 cellules pour afficher en tant qu'en-tête. Le code suivant ne parvient pas à le faire. Je suis conscient que table2 n'a pas 12 cellules. Sur la deuxième page, seul "testing" est affiché. Qu'est-ce que je rate?PdfPTable en tant qu'en-tête dans iTextSharp

Merci d'avance!

Document document = new Document(); 

     try 
     { 
      PdfWriter.GetInstance(document, new FileStream("TableTest.pdf", FileMode.Create)); 
      document.Open(); 

      PdfPTable table = new PdfPTable(1); 
      table.WidthPercentage = 100; 
      PdfPTable table2 = new PdfPTable(2); 

      //logo 
      PdfPCell cell2 = new PdfPCell(Image.GetInstance(@"C:\path\to\file.gif")); 
      cell2.Colspan = 2; 
      table2.AddCell(cell2); 

      //title 
      cell2 = new PdfPCell(new Phrase("\nTITLE TEXT", new Font(Font.HELVETICA, 16, Font.BOLD | Font.UNDERLINE))); 
      cell2.HorizontalAlignment = Element.ALIGN_CENTER; 
      cell2.Colspan = 2; 
      table2.AddCell(cell2); 

      PdfPCell cell = new PdfPCell(table2); 
      table.HeaderRows = 1; 
      table.AddCell(cell); 
      table.AddCell(new PdfPCell(new Phrase(""))); 

      document.Add(table); 

      document.Add(new Phrase("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\ntesting")); 
     } 
     catch (DocumentException de) 
     { 
      Console.Error.WriteLine(de.Message); 
     } 
     catch (IOException ioe) 
     { 
      Console.Error.WriteLine(ioe.Message); 
     } 

     document.Close(); 

Répondre

11

Bingo! La classe PdfPageEventHandler a fonctionné pour moi.

J'ai utilisé la PdfPageEventHelper redéfinissant la méthode OnEndPage:

class _events : PdfPageEventHelper 
{ 
    public override void OnEndPage(PdfWriter writer, Document document) 
    { 
     PdfPTable table = new PdfPTable(1); 
     //table.WidthPercentage = 100; //PdfPTable.writeselectedrows below didn't like this 
     table.TotalWidth = document.PageSize.Width - document.LeftMargin - document.RightMargin; //this centers [table] 
     PdfPTable table2 = new PdfPTable(2); 

     //logo 
     PdfPCell cell2 = new PdfPCell(Image.GetInstance(@"C:\path\to\file.gif")); 
     cell2.Colspan = 2; 
     table2.AddCell(cell2); 

     //title 
     cell2 = new PdfPCell(new Phrase("\nTITLE", new Font(Font.HELVETICA, 16, Font.BOLD | Font.UNDERLINE))); 
     cell2.HorizontalAlignment = Element.ALIGN_CENTER; 
     cell2.Colspan = 2; 
     table2.AddCell(cell2); 

     PdfPCell cell = new PdfPCell(table2); 
     table.AddCell(cell); 

     table.WriteSelectedRows(0, -1, document.LeftMargin, document.PageSize.Height - 36, writer.DirectContent); 
    } 
} 

alors, construire pdf

Document document = new Document(PageSize.A4, 36, 36, 36 + <height of table>, 36); // note height should be set here 
_events e = new _events(); 
PdfWriter pw = PdfWriter.GetInstance(document, new FileStream("TableTest.pdf", FileMode.Create)); 
pw.PageEvent = e; 
document.Open(); 

for(int i=0;i<100;i++) 
{ 
    document.Add(new Phrase("TESTING\n")); 
} 

document.Close(); 
0

Cela ne fonctionnera pas si vous avez plus d'une page. Si vous avez plus d'une page, vous devez utiliser l'événement OnStartPage au lieu de l'événement onEndPage.

1

J'ai utilisé une approche très simple pour ajouter une ligne d'en-tête sur chaque page, lors de la création d'un PdfPTable avec 'n' lignes, où 'n' pourrait être un nombre entier. J'utilise iTextSharp 4.0.4.0. Par conséquent, vous pouvez créer une table avec 1000 lignes ou 2 lignes ou 5000 lignes, mais iTextSharp affichera automatiquement la sortie de page pour vous. Ce que iTextSharp ne fera pas, c'est ajouter automatiquement un en-tête au début de chaque page.

Pour ajouter une ligne d'en-tête pour chaque page, tout ce que je ne faisais ajouter la ligne de code après la création du PdfPTable avec toutes ses lignes et cellules, puis iTextSharp paginée automatiquement la sortie d'une ligne d'en-tête. 'table' est l'instance de PdfPTable que j'ai créée dans mon code avant de définir sa propriété HeaderRows.

table.HeaderRows = 1; //this will automatically insert a header row 
         //at start of every page. The first row you created 
         //in your code will be used as the header row in this case. 
Questions connexes