2010-03-24 7 views

ответ

1

мог бы вы вышка что-то, как добавить новые пункты с небольшой высотой и текстом = «---------»

PdfPCell Cell = new PdfPCell(new Paragraph("------")); 
Cell.Height = 0.2f; 

Вы можете также сделать границы самостоятельно, используя PdfPCellEvent. Есть разные слои для добавления. См. Здесь API: http://api.itextpdf.com/com/itextpdf/text/pdf/PdfPCellEvent.html

+0

Я предполагаю, что мы не можем установить высоту для ячейки? –

1

Как и было предложено, используйте PdfPCellEvent. Код, приведенный ниже, должен получить от вас большую часть пути. Cell event example. Переопределяя событие ячейки, вы в основном говорите iText, как вы думаете, что он должен рисовать свои ячейки. Всякий раз, когда какие-либо ячейки добавляются в таблицу, они будут следовать вашим правилам.

class CustomCell implements PdfPCellEvent { 
public void cellLayout(PdfPCell cell, Rectangle rect, 
        PdfContentByte[] canvas) { 
        PdfContentByte cb = canvas[PdfPTable.LINECANVAS]; 
        cb.setLineDash(new float[] {3.0f, 3.0f}, 0);   
        cb.stroke(); 
      } 
} 

public class Main { 

     public static void main(String[] args) throws Exception { 
      Document document = new Document(); 
      PdfWriter.getInstance(document, new FileOutputStream("output.pdf")); 
      document.open(); 
      CustomCell border = new CustomCell(); 

      PdfPTable table = new PdfPTable(6); 
      PdfPCell cell; 

      for (int i = 1; i <= 6; i++) { 
       cell = new PdfPCell(new Phrase("test"));    
       cell.setCellEvent(border); 
       table.addCell(cell); 
      } 

      document.add(table); 
      document.close(); 
    } 
} 
+0

Я пропустил сообщение об ошибке с помощью E-Clipse, когда я попробовал свой код ... «Нет доступного экземпляра типа pdf ...» Любая идея, что произошло? –

0
PdfPCell Border1 = new PdfPCell(new Paragraph("-----------------------------------------------------------------------------------------------------------------------")); 
      Border1.Border = 0; 
      Border1.VerticalAlignment = 3; 
      Border1.FixedHeight = 5F; 
      Border1.PaddingLeft = -5; 
      Border1.PaddingRight = -5; 
      Border1.PaddingBottom = -5; 
      Border1.PaddingTop = -5; 
1

Cell подчеркнуты черточек:

public class UnderlinedCell implements PdfPCellEvent { 

    public void cellLayout(PdfPCell cell, Rectangle position, 
     PdfContentByte[] canvases) { 
     PdfContentByte canvas = canvases[PdfPTable.LINECANVAS]; 
     canvas.setLineWidth(0.5f); 
     canvas.setLineDash(3f, 3f); 
     canvas.moveTo(position.getLeft(), position.getBottom()); 
     canvas.lineTo(position.getRight(), position.getBottom()); 

     canvas.stroke(); 
    } 
} 
Смежные вопросы