Android : Create Table and Paragraph in Same page using iText

on Thursday, April 2, 2015


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: 9943756999");
paragraph.setAlignment(Element.ALIGN_CENTER);
document.add(paragraph);

paragraph = new Paragraph("Email : vijaydhascr@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());
}


Out put is:


Out put Image


But I want like this:


Expected OP


Please let me any idea to create pdf like this.


Thanks in advance


0 comments:

Post a Comment