我對iText
很新。我正嘗試在同一頁中創建表和段落。使用iText在同一頁面中創建表格和段落
我想創建一個表,我想在表格的頂部和下面添加段落。
在這裏,我附上了我的代碼和圖像。請檢閱並讓我有任何想法來解決問題。
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 : [email protected]");
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());
}
輸出是:
所需的輸出:
請讓我的任何想法,這樣創建PDF。
謝謝..現在我知道我犯了什麼錯誤,再次感謝..! – 2015-04-03 07:16:21
謝謝你的回答,先生。現在我已經創建了賬單..! :-)但現在又多了一個疑問先生。我想用POS打印機打印這張賬單。紙張尺寸將爲58毫米。這PDF看起來像A4大小。如何重新設定POS打印機大小的頁面大小?如果產品超過100個,那麼我必須使用新的頁面。爲此,我怎樣才能在一張紙上打印賬單。對不起,您先生。現在我沒有任何其他的幫助我的先生了。請讓我知道先生.. – 2015-04-03 09:21:22
請將此作爲單獨問題發佈。不要使用評論部分來創建新問題。 – 2015-04-03 09:28:55