2016-09-21 2 views
0

C J'utilise iText PDF pour générer un fichier PDF qui contient une table qui devrait être établi comme suit:Problèmes avec la version 7 de iText PDF pour #

------------------------------------------------ 
|     HEADER      | 
------------------------------------------------ 
| some data goes here | more data here   | 
------------------------------------------------ 
| Col 1 | Col 2 | Col 3 | Col 4 | Col 5 | Col 6 | 
------------------------------------------------ 
| 1 | SDF  wer qwerwq | weqr | WERQW | 
------------------------------------------------ 
|  |      |  |  | 
|  |      |  |  | 
|  |      |  |  | 
|  |      |  |  | 
------------------------------------------------ 
| footer information       | 
------------------------------------------------ 

mais la table est en cours d'élaboration comme suit:

J'ai essayé suivant les exemples, mais ils sont écrits en Java, et le modèle objet C# semble être légèrement différent. Les lignes ci-dessous la ligne qui a une valeur « Col 1 » de 1 sont répartis sur plusieurs colonnes 2, 3 et 4.

Points à noter:

  1. Pour la cellule d'en-tête, je suis en train de l'alignement horizontal en appelant cell.SetHorizontalAlignment (HorizontalAlignment.CENTER)
  2. je dois définir la couleur d'une partie du texte à être rouge
  3. J'utilise la méthode table.AddCell ajouter la cellule
  4. Je suis en train de la frontière de la table (selon la documentation c'est la cellule par défaut) à Bord er.NO_BORDER.
  5. Ceci est pour une application Web écrite en C#
  6. J'ai téléchargé la dernière version de iText (version 7.0.1)
  7. J'ai créé un CellRender personnalisé, mais qui semble avoir aucun effet.
  8. À l'origine j'utilisais iText 5 mais j'avais besoin d'un plus grand contrôle sur le rendu de la table car j'avais besoin de savoir jusqu'où la page que nous avons atteinte.
  9. Voici le code que j'utilise pour créer la cellule:

    PdfFont cellFont = font; 
    
        if ((fontStyle & FONT_STYLE_BOLD) == FONT_STYLE_BOLD && (fontStyle & FONT_STYLE_ITALIC) == FONT_STYLE_ITALIC) 
        { 
         cellFont = fontBoldItalic; 
        } 
        else if ((fontStyle & FONT_STYLE_BOLD) == FONT_STYLE_BOLD) 
        { 
         cellFont = fontBold; 
        } 
        else if ((fontStyle & FONT_STYLE_ITALIC) == FONT_STYLE_ITALIC) 
        { 
         cellFont = fontItalic; 
        } 
    
        Color fontColor = Color.BLACK; 
        if ((fontStyle & FONT_STYLE_RED) == FONT_STYLE_RED) 
        { 
         fontColor = Color.RED; 
        } 
    
        Text text = new Text(content); 
        text.SetFont(cellFont); 
        text.SetFontColor(fontColor); 
        text.SetFontSize(fontSize); 
    
        if ((fontStyle & FONT_STYLE_UNDERLINE) == FONT_STYLE_UNDERLINE) 
        { 
         text.SetUnderline(); 
        } 
    
        Cell cell = new Cell(rowspan, colspan); 
        cell.Add(new Paragraph(text)); 
        //cell.SetNextRenderer(new CellBorders(cell, borders)); 
    
        return cell; 
    

Voici comment la table est créée et la table est ajoutée au document à la fin de la méthode Web:

 Table table = new Table(6); 
     table.SetWidthPercent(100); 
     table.SetPadding(3); 
     table.SetSpacingRatio(1); 
     table.SetBorder(Border.NO_BORDER); 
+1

Pour la cellule d'en-tête, avez-vous essayé de placer l'alignement horizontal du texte ou le paragraphe à 'TextAlignment. CENTER' (méthode 'SetHorizontalAlignment')? –

+1

En ce qui concerne les bordures, vous pouvez spécifier des propriétés de bordure sur les cellules elles-mêmes. ('cell # SetBorder/SetBorderLeft/...'). –

+1

Erreur mineure dans le premier commentaire, la méthode à utiliser est 'SetTextAlignment()' –

Répondre

2

Vous avez deux problèmes:

  1. Le texte ne s'aligne pas correctement (En-tête) L'alignement du texte dans les cellules est défini à l'aide de la méthode SetTextAlignment(). SetHorizontalAlignment définit l'alignement du conteneur entourant le texte.

  2. Les bordures ne s'affichent pas comme prévu. Premièrement, la définition d'une bordure sur une table ne définit pas le comportement de la cellule par défaut dans iText 7, comme dans iText 5. Puisque la valeur par défaut tableRenderer n'utilise pas la propriété border, table#SetBorder(Border.NO_BORDER) n'aura d'effet que si vous définissez un rendu personnalisé. faire usage de la propriété vous-même.

La façon correcte de définir des bordures personnalisées est de le faire au niveau des cellules individuelles. Et là, vous devez vous rappeler que depuis les frontières se chevauchent, vous devez définir toutes les frontières qui se chevauchent à NO_BORDER si vous ne voulez pas la frontière pour montrer:

for(int j = 0; j<3; j++){ 
     Cell dCell = new Cell(); 
     //When setting borders to NO_BORDER, remember to set that border to NO_BORDER in all adjoining cells 
     dCell.SetBorderLeft(Border.NO_BORDER); 
     dCell.SetBorderRight(Border.NO_BORDER); 
     dCell.Add(new Paragraph(String.Format("Entry, {0}-{1}",i,j))); 
     //Dashed, striped, grooved and more can be specified 
     if(i != 4) dCell.SetBorderBottom(new DashedBorder(1f)); 
     tab.AddCell(dCell); 
    } 

Le fragment ci-dessus se traduira dans les cellules sans frontières entre la Entrées

for(int k = 4; k<6;k++){ 
     Cell d2Cell = new Cell(); 
     //Only the right-most border will not show, since the left-most borders of the neighbouring cells still get drawn 
     d2Cell.SetBorderRight(Border.NO_BORDER); 
     d2Cell.Add(new Paragraph(String.Format("Entry, {0}-{1}",i,k)).SetFontColor(Color.RED)); 
     //Specific borders apart from NO_BORDER do override the default 
     if(i!=4) d2Cell.SetBorderBottom(new DashedBorder(1f)); 
     tab.AddCell(d2Cell); 
    } 

Le fragment ci-dessus n'aura aucune bordure à droite de la cellule la plus à droite.

est Ci-dessous un fragment de code autonome qui construit un tableau comme celui de la question:

public void CreateTable(string dest) 
    { 
     PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); 
     Document doc = new Document(pdfDoc); 

     Table tab = new Table(6); 
     //Table should take up the entire width 
     tab.SetWidthPercent(100); 
     tab.SetPadding(3); 
     //Because table takes up the entire width, this has no visual effect, but otherwise it will center the table 
     tab.SetHorizontalAlignment(HorizontalAlignment.CENTER); 


     //Header cell 
     Cell hCell = new Cell(1, 6); 
     hCell.Add(new Paragraph("Centered Header")); 
     //Text is aligned by calling SetTextAlignment 
     hCell.SetTextAlignment(TextAlignment.CENTER); 

     tab.AddCell(hCell); 
     //Subheaders 
     Cell shCellL = new Cell(1, 3); 
     shCellL.Add(new Paragraph("Left aligned data")); 
     shCellL.SetTextAlignment(TextAlignment.LEFT); 

     Cell shCellR = new Cell(1, 3); 
     shCellR.Add(new Paragraph("Right aligned data")); 
     shCellR.SetTextAlignment(TextAlignment.RIGHT); 

     tab.AddCell(shCellL); 
     tab.AddCell(shCellR); 
     //col names 
     for (int i = 0; i < 6; i++) 
     { 
      Cell colName = new Cell(); 
      colName.Add(new Paragraph(String.Format("Col {0}", i))); 
      colName.SetTextAlignment(TextAlignment.CENTER); 
      tab.AddCell(colName); 
     } 
     //data cols 
     for (int i = 1; i < 5; i++) 
     { 
      Cell nC = new Cell(); 
      nC.Add(new Paragraph("" + i)); 
      tab.AddCell(nC); 
      for (int j = 0; j < 3; j++) 
      { 
       Cell dCell = new Cell(); 
       //When Setting borders to NO_BORDER, remember to Set that border to NO_BORDER in all adjoining cells 
       dCell.SetBorderLeft(Border.NO_BORDER); 
       dCell.SetBorderRight(Border.NO_BORDER); 
       dCell.Add(new Paragraph(String.Format("Entry, {0}-{1}", i, j))); 
       //Dashed, striped, grooved and more can be specified 
       if (i != 4) dCell.SetBorderBottom(new DashedBorder(1f)); 
       tab.AddCell(dCell); 
      } 
      for (int k = 4; k < 6; k++) 
      { 
       Cell d2Cell = new Cell(); 
       //Only the rightmost border will not show, since the left-most borders of the neighbouring cells still get drawn 
       d2Cell.SetBorderRight(Border.NO_BORDER); 
       d2Cell.Add(new Paragraph(String.Format("Entry, {0}-{1}", i, k)).SetFontColor(Color.RED)); 
       //Specific borders apart from NO_BORDER do override the default 
       if (i != 4) d2Cell.SetBorderBottom(new DashedBorder(1f)); 
       tab.AddCell(d2Cell); 
      } 
     } 

     //footer cell 
     Cell fCell = new Cell(1, 6); 
     fCell.Add(new Paragraph("footer")); 
     tab.AddCell(fCell); 

     //Add table to document 
     doc.Add(new Paragraph("Complex Table example")); 
     doc.Add(tab); 
     doc.Close(); 
    }