Java – Need to make PDF sample with boxes as table columns by android app

androiditextjavapdfpdf-generation

I need to make PDF sample file just like the below image but I didn't find any suitable guide to do exactly like this. I have followed some links

http://itextpdf.com/examples/iia.php?id=102

Is there a way to draw a rectangle into a PdfPCell in iText (the Java version)?

but from the second link I didn't understand how could I make more likely to my requirement.

Thanks in advance.enter image description here

Best Answer

It is unclear to me why you refer to this example: http://itextpdf.com/examples/iia.php?id=102 The PDF that is created with that example shows me in a Superman outfit. What is the link with creating a table with rounded borders?

Please take a look at the NestedTableRoundedBorder example. It creates a PDF that looks like this: nested_table_rounded_border.pdf

enter image description here

This construction consists of nested tables. The outer table only has one column, but we use it to create the rounded corners:

class RoundRectangle implements PdfPCellEvent {
    public void cellLayout(PdfPCell cell, Rectangle rect,
            PdfContentByte[] canvas) {
        PdfContentByte cb = canvas[PdfPTable.LINECANVAS];
        cb.roundRectangle(
            rect.getLeft() + 1.5f, rect.getBottom() + 1.5f, rect.getWidth() - 3,
            rect.getHeight() - 3, 4);
        cb.stroke();
    }
}

This cell event is used like this:

cell = new PdfPCell(innertable);
cell.setCellEvent(roundRectangle);
cell.setBorder(Rectangle.NO_BORDER);
cell.setPadding(8);
outertable.addCell(cell);

The inner tables are used to create cells with or without borders, for instance like this:

// inner table 1
PdfPTable innertable = new PdfPTable(5);
innertable.setWidths(new int[]{8, 12, 1, 4, 12});
// first row
// column 1
cell = new PdfPCell(new Phrase("Record Ref:"));
cell.setBorder(Rectangle.NO_BORDER);
innertable.addCell(cell);
// column 2
cell = new PdfPCell(new Phrase("GN Staff"));
cell.setPaddingLeft(2);
innertable.addCell(cell);
// column 3
cell = new PdfPCell();
cell.setBorder(Rectangle.NO_BORDER);
innertable.addCell(cell);
// column 4
cell = new PdfPCell(new Phrase("Date: "));
cell.setBorder(Rectangle.NO_BORDER);
innertable.addCell(cell);
// column 5
cell = new PdfPCell(new Phrase("30/4/2015"));
cell.setPaddingLeft(2);
innertable.addCell(cell);
// spacing
cell = new PdfPCell();
cell.setColspan(5);
cell.setFixedHeight(3);
cell.setBorder(Rectangle.NO_BORDER);
innertable.addCell(cell);

If some of the dimensions are quite like you want, it's sufficient to change parameters such as the widths array, the padding, the fixed height, etc.

Related Topic