我是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 : 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());
}
输出是:
期望的输出:
请让我知道如何创建这样的pdf。
答案 0 :(得分:1)
您正在混合两种不同的(通常是排他性的)方法。
一方面,您将布局移交给iText。您可以使用以下行来执行此操作:
document.add(paragraph);
在这种情况下,iText会跟踪页面上的y
位置,并在必要时创建新页面。
另一方面,您可以控制布局。您可以通过在绝对位置添加内容来实现此目的:
table.writeSelectedRows(0, -1, document.leftMargin(), 650,
docWriter.getDirectContent());
在这种情况下,iText不会跟踪y
位置。你有责任这样做。
您必须选择两种方法中的一种。
您可以让iText执行布局,并使用writeSelectedRows()
替换document.add()
方法:
document.add(table);
paragraph = new Paragraph("Thank You");
paragraph.setAlignment(Element.ALIGN_LEFT);
document.add(paragraph);
现在paragraph
将跟随表格。
或者您必须在绝对位置添加所有内容:
float y = table.writeSelectedRows(0, -1, document.leftMargin(), 650,
docWriter.getDirectContent());
// Add the *Thank You* paragraph at position y
我建议你让iText进行布局,因为我们的设计存在一些缺陷。你硬编码了650
的位置。你是如何确定这个职位的?
此外writeSelectedRows()
如果表格不适合页面,则不会拆分表格。您负责计算每行的高度,并在表太大时触发newPage()
。 (只需在表格中添加更多行来测试它,您就会看到。)
我并不是说你自己无法定义完整的布局(使用ColumnText
你可以以一种非常准确的方式工作),我只是说它和#39;更多的工作。