0

I am Very new to iText. I am trying to create Table and Paragraph in same page.

I want to create a table and I want add paragraph top and below by the table.

Here with I have attached my code and image. Please review and let me any idea to resolve the issue.

class DottedCell implements PdfPCellEvent {
    public void cellLayout(PdfPCell cell, Rectangle position,
            PdfContentByte[] canvases) {
        PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
        canvas.setLineDash(3f, 3f);
        canvas.moveTo(position.getLeft(), position.getTop());
        canvas.lineTo(position.getRight(), position.getTop());
        canvas.moveTo(position.getLeft(), position.getBottom());
        canvas.lineTo(position.getRight(), position.getBottom());
        canvas.stroke();
    }
}

private void createBill(Document document) throws DocumentException {

    paragraph = new Paragraph("Hotel VApps");
    paragraph.setAlignment(Element.ALIGN_CENTER);
    document.add(paragraph);

    paragraph = new Paragraph("Phone: 9943123999");
    paragraph.setAlignment(Element.ALIGN_CENTER);
    document.add(paragraph);

    paragraph = new Paragraph("Email : vijaydhasxx@gmail.com");
    paragraph.setAlignment(Element.ALIGN_CENTER);
    document.add(paragraph);

    DottedLineSeparator separator = new DottedLineSeparator();
    separator.setPercentage(59500f / 523f);
    Chunk linebreak = new Chunk(separator);
    document.add(linebreak);

    paragraph = new Paragraph("Bill No: 12345");
    paragraph.setAlignment(Element.ALIGN_LEFT);
    document.add(paragraph);

    paragraph = new Paragraph("Bill Date: 01/04/2015 10:30:55 PM");
    paragraph.setAlignment(Element.ALIGN_LEFT);
    document.add(paragraph);

    Report_Page app = new Report_Page();
    float[] columnWidths = { 1.5f, 5f, 2f, 1.5f, 2f };      
    table = new PdfPTable(columnWidths);
    table.setTotalWidth(300f);
    table.setHorizontalAlignment(Element.ALIGN_CENTER);

    cell = new PdfPCell(new Phrase("P.No"));
    cell.setHorizontalAlignment(Element.ALIGN_LEFT);
    cell.setCellEvent(app.new DottedCell());
    cell.setBorder(Rectangle.NO_BORDER);
    table.addCell(cell);

    cell = new PdfPCell(new Phrase("Item Name"));
    cell.setHorizontalAlignment(Element.ALIGN_LEFT);
    cell.setCellEvent(app.new DottedCell());
    cell.setBorder(Rectangle.NO_BORDER);
    table.addCell(cell);

    cell = new PdfPCell(new Phrase("Price"));
    cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
    cell.setCellEvent(app.new DottedCell());
    cell.setBorder(Rectangle.NO_BORDER);
    table.addCell(cell);

    cell = new PdfPCell(new Phrase("Qty"));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setCellEvent(app.new DottedCell());
    cell.setBorder(Rectangle.NO_BORDER);
    table.addCell(cell);

    cell = new PdfPCell(new Phrase("Ext Price"));
    cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
    cell.setCellEvent(app.new DottedCell());
    cell.setBorder(Rectangle.NO_BORDER);
    table.addCell(cell);
    table.setHeaderRows(1);

    sqlcon.open();
    Cursor c = sqlcon.readOrderedItems("");
    int rows = c.getCount();
    c.moveToFirst();

    for (int i = 0; i < rows; i++) {
        cell = new PdfPCell(new Phrase(String.valueOf(i + 1)));
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(c.getString(0)));
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(c.getString(2)));
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);

        cell = new PdfPCell(new Phrase(c.getString(1)));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);

        total += Double.parseDouble(c.getString(3));

        cell = new PdfPCell(new Phrase(c.getString(3)));
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
        cell.setBorder(Rectangle.NO_BORDER);
        table.addCell(cell);
        c.moveToNext();
    }

    sqlcon.close();


    cell = new PdfPCell(new Phrase(""));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setCellEvent(app.new DottedCell());
    cell.setBorder(Rectangle.NO_BORDER);
    table.addCell(cell);

    cell = new PdfPCell(new Phrase(""));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setCellEvent(app.new DottedCell());
    cell.setBorder(Rectangle.NO_BORDER);
    table.addCell(cell);

    cell = new PdfPCell(new Phrase(""));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setCellEvent(app.new DottedCell());
    cell.setBorder(Rectangle.NO_BORDER);
    table.addCell(cell);

    cell = new PdfPCell(new Phrase("Total"));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setCellEvent(app.new DottedCell());
    cell.setBorder(Rectangle.NO_BORDER);
    table.addCell(cell);

    cell = new PdfPCell(new Phrase(String.valueOf(total)));
    cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
    cell.setCellEvent(app.new DottedCell());
    cell.setBorder(Rectangle.NO_BORDER);
    table.addCell(cell);


    DottedLineSeparator separator1 = new DottedLineSeparator();
    separator1.setPercentage(59500f / 523f);
    Chunk linebreak1 = new Chunk(separator);
    document.add(linebreak1);

    paragraph = new Paragraph("Thank You");
    paragraph.setAlignment(Element.ALIGN_LEFT);
    document.add(paragraph);

    table.writeSelectedRows(0, -1, document.leftMargin(), 650,
            docWriter.getDirectContent());
}

Output is:

Out put Image

Desired output:

Expected OP

Please let me any idea to create pdf like this.

1 Answers1

1

You are mixing two different (usually exclusive) approaches.

On the one hand you are handing over the layout to iText. You do this by using lines such as:

document.add(paragraph);

In this case, iText keeps track of the y position on the page and it creates a new page when necessary.

On the other hand you take control over the layout. You do so by adding content at an absolute position:

table.writeSelectedRows(0, -1, document.leftMargin(), 650,
        docWriter.getDirectContent());

In that case, iText doesn't keep track of the y position. You are responsible for doing this.

You'll have to choose one of the two approaches.

Either you let iText do the layout and you replace the writeSelectedRows() method by a document.add():

document.add(table);
paragraph = new Paragraph("Thank You");
paragraph.setAlignment(Element.ALIGN_LEFT);
document.add(paragraph);

Now the paragraph will follow the table.

Or you'll have to add all the content at an absolute position:

float y = table.writeSelectedRows(0, -1, document.leftMargin(), 650,
        docWriter.getDirectContent());
// Add the *Thank You* paragraph at position y

I would advise you to let iText do the layout, because there are some flaws in our design. You hardcoded the position 650. How did you determine this position?

Furthermore writeSelectedRows() won't split a table if it doesn't fit the page. You are responsible for calculating the height of each row and to trigger a newPage() when the table is too big. (Just add some more rows to your table to test this, you'll see.)

I'm not saying that it's not possible to define the complete layout yourself (using ColumnText you can work in a really accurate way), I'm only saying that it's more work.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thank you.. Now I realize where I did mistake, Thanks again..!! – Vijayadhas Chandrasekaran Apr 03 '15 at 07:16
  • Thanks for your answers sir. Now I have created the bill..!! :-) But now one more doubt sir. I want to print this bill in POS printer. Paper size will be 58mm. This PDF is look like A4 size. How to re size this page for POS printer size? If products goes more than 100 mean so I have to use new page. For that how can I print the bill in one single paper. Sorry for trouble you sir. Now I don't have any other hand to help me sir. Please let me know sir.. – Vijayadhas Chandrasekaran Apr 03 '15 at 09:21
  • Post this as a separate question please. Do not use the comment section to create new questions. – Bruno Lowagie Apr 03 '15 at 09:28
  • [Question](http://stackoverflow.com/questions/29429684/how-to-create-bill-for-pos-printer-size-in-android).. Please see this sir.. – Vijayadhas Chandrasekaran Apr 03 '15 at 09:38