Mr CooL
Mr CooL

Reputation: 1579

iText-5.0.1 + Make the border of PdfPTable with dotted line

Is there any way to make the border of a cell with dotted line (e.g _ _ _ _ _ _ _ _ _ _ _ _) instead of solid line ( e.g ________________ ) in iText-5.0.1????

Upvotes: 2

Views: 10963

Answers (4)

Difi
Difi

Reputation: 11

Cell underlined with dashes:

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

Upvotes: 1

Vijith
Vijith

Reputation: 1

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;

Upvotes: 0

zmf
zmf

Reputation: 9303

As was suggested, use a PdfPCellEvent. The code below should get you most of the way there. Cell event example. By overriding the cell event, you basically tell iText how you think it should draw its cells. Whenever any cells are added to the table they'll follow your rules.

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

Upvotes: 1

David Fox
David Fox

Reputation: 10753

could you rig something like adding new paragraphs with small height and text="---------"

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

You can also draw the borders yourself using a PdfPCellEvent. There are different layers to add to. See the API here: http://api.itextpdf.com/com/itextpdf/text/pdf/PdfPCellEvent.html

Upvotes: 1

Related Questions