2017-08-29 4 views
2

J'ai ici les codes et je continue à recevoir le message d'erreur: Le document n'a pas de pages. J'ai également placé les largeurs et la phrase de table, mais toujours le message d'erreur s'est produit. Je suis complètement sorti maintenant, j'ai essayé de chercher d'autres cas cependant, ils ont essayé de fixer leur largeur de table. Y a-t-il quelque chose qui me manque? Toute aide serait appréciée. Je vous remercie!C# - iTextSharp Le document n'a pas de pages

private void printPDF(object sender, EventArgs e) 
    {    

      Document docu = new Document(PageSize.LETTER); 
      PdfWriter writer = PdfWriter.GetInstance(docu, new FileStream("C:\\Report\\" + empno + ".pdf", FileMode.Create)); 
      Phrase phrase = null; 
      PdfPCell cell = null; 
      PdfPTable table = null; 
      BaseColor color = null; 

      docu.Open(); 

      //Header Table 
      table = new PdfPTable(2); 
      table.TotalWidth = 500f; 
      table.LockedWidth = true; 
      table.SetWidths(new float[] { 0.3f, 0.7f }); 

      //Company Name and Address 
      phrase = new Phrase(); 
      phrase.Add(new Chunk("Company Name\n\n", FontFactory.GetFont("Arial", 16, iTextSharp.text.Font.BOLD, BaseColor.ORANGE))); 
      phrase.Add(new Chunk("Company Address", FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK))); 
      cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT); 
      cell.VerticalAlignment = PdfPCell.ALIGN_TOP; 
      table.AddCell(cell); 

      docu.Add(table); 
      docu.Close(); 
    } 
private static PdfPCell PhraseCell(Phrase phrase, int align) 
    { 
     PdfPCell cell = new PdfPCell(phrase); 
     cell.BorderColor = BaseColor.WHITE; 
     cell.VerticalAlignment = PdfPCell.ALIGN_TOP; 
     cell.HorizontalAlignment = align; 
     cell.PaddingBottom = 2f; 
     cell.PaddingTop = 0f; 
     return cell; 
    } 
+0

Y at-il plus de code dans votre méthode de ImprimerPDF()? Il ne semble pas que vous fassiez quoi que ce soit avec le document 'docu' en plus d'ajouter la table et de la fermer. A-t-il besoin d'être retourné à l'appelant? – njenson

Répondre

2

J'ai eu un coup d'œil et je dirais que vous avez spécifié table = new PdfPTable(2); dit la table, il aura 2 cellules par ligne mais vous avez seulement fourni une cellule dans la première rangée. Comme vous avez seulement fourni une cellule, la définition de la table n'est pas complète, donc elle n'a pas de lignes à afficher.

Maintenant, si vous changez votre code et fournissez 2 cellules.

phrase = new Phrase(); 
phrase.Add(new Chunk("Company Name\n\n", FontFactory.GetFont("Arial", 16, iTextSharp.text.Font.BOLD, BaseColor.ORANGE))); 
phrase.Add(new Chunk("Company Address", FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK))); 
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT); 
cell.VerticalAlignment = PdfPCell.ALIGN_TOP; 
table.AddCell(cell); 

//yes this is just duplicating content but proves the point 
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT); 
cell.VerticalAlignment = PdfPCell.ALIGN_TOP; 
table.AddCell(cell); 


docu.Add(table); 
docu.Close(); 

Cela fonctionne très bien car nous avons maintenant deux cellules et pouvons compléter la définition de la table. Si vous essayez d'ajouter 3 cellules, vous verrez que la 3ème cellule n'est pas rendue car elle n'a pas complété la définition de la table. Lorsque vous ajoutez des cellules à une table, vous les ajoutez horizontalement à la table et la table les convertit en lignes en fonction des définitions de colonnes. -À-dire dans une table de colonne 2, les cellules deviennent

cell 1 | Row 1 Cell 1 
cell 2 | Row 1 Cell 2 
cell 3 | Row 2 Cell 1 
cell 4 | Row 2 Cell 2 

maintenant cette déclaration

table = new PdfPTable(2); 
table.TotalWidth = 500f; 
table.LockedWidth = true; 
table.SetWidths(new float[] { 0.3f, 0.7f }); 

est équivalent à:

table = new PdfPTable(new float[] { 0.3f, 0.7f }); 
table.TotalWidth = 500f; 
table.LockedWidth = true; 

Comme vous l'avez spécifié dans le constructeur 2 largeurs de colonnes qui donnera la table une spécification de 2 colonnes.

Comme un exemple complet voir le code ci-dessous:

private void printPDF(object sender, EventArgs e) 
{ 
    string path = "D:\\Test\\" + Guid.NewGuid().ToString() + ".pdf"; 
    using (var fileStream = new FileStream(path, FileMode.Create)) 
    { 
     Document docu = new Document(PageSize.LETTER); 
     PdfWriter writer = PdfWriter.GetInstance(docu, fileStream); 

     docu.Open(); 

     //Header Table 
     var table = new PdfPTable(new float[] { 0.3f, 0.7f }); 
     table.TotalWidth = 500f; 

     //company name 
     var phrase = new Phrase("Company Name", FontFactory.GetFont("Arial", 16, iTextSharp.text.Font.BOLD, BaseColor.ORANGE)); 
     var cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT); 
     table.AddCell(cell); 

     phrase = new Phrase("My test company", FontFactory.GetFont("Arial", 16, iTextSharp.text.Font.BOLD, BaseColor.ORANGE)); 
     cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT); 
     table.AddCell(cell); 

     //company address 
     phrase = new Phrase("Company Address", FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)); 
     cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT); 
     table.AddCell(cell); 

     phrase = new Phrase("123 Main Street, Your City.", FontFactory.GetFont("Arial", 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK)); 
     cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT); 
     table.AddCell(cell); 

     docu.Add(table); 
     docu.Close(); 
     writer.Close(); 
    } 
} 
private static PdfPCell PhraseCell(Phrase phrase, int align) 
{ 
    PdfPCell cell = new PdfPCell(phrase); 
    cell.BorderColor = BaseColor.WHITE; 
    cell.VerticalAlignment = PdfPCell.ALIGN_TOP; 
    cell.HorizontalAlignment = align; 
    cell.PaddingBottom = 2f; 
    cell.PaddingTop = 0f; 
    return cell; 
} 
+0

Merci pour ça! Je l'ai juste essayé et ça a marché comme prévu. Je n'ai pas compris à propos de ces cellules par rangée. –