我对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