我正在使用显示许多表格的pdf。当表格不适合页面的其余部分时,iText 会剪切表格并将其余部分放在下一页上。
我希望如果表格不适合页面的其余部分,则将其放在新页面上。
我怎样才能做到这一点?
有不同的方法可以做到这一点;这是三个选项:
public class XX_RPT_D84_AR_Invoice_Printing_GBS extends PdfPageEventHelper{
public void onEndPage(PdfWriter writer, Document document) {
Rectangle rect = writer.getBoxSize("art");
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_CENTER, new Phrase(String.format("page %d", 1)),
(rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 18, 0);
}
public static void main(String[] args) {
try {
OutputStream file = new FileOutputStream(new File("D:\\PDF1.pdf"));
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, file);
BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
BaseColor color = BaseColor.BLACK;
PdfContentByte cb = null;
Phrase[] header = new Phrase[3];
header [0] = new Phrase("Genpact India");
header [1] = new Phrase("(A Private Company with Unlimited Liability)");
header [2] = new Phrase("Registered Office: Delhi Information Technology Park, Shastri Park, Delhi-110053, India");
Font font = new Font(BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED),
9, Font.NORMAL);
boolean isPrinted = false;
Image image = Image.getInstance ("src/Logo/logo.png");
image.scaleAbsolute(120f, 31f);//image width,height
image.setDpi(-10, -5);
image.setSpacingBefore(-15);
Image sign = Image.getInstance ("src/Logo/sign.png");
sign.scaleAbsolute(100f, 50f);//image width,height
sign.setIndentationLeft(10);
PdfPTable table = new PdfPTable(new float[]{0.8f,5,1.5f,1.3f,2,2});
PdfPCell s_No = new PdfPCell(new Phrase("S No", font));
s_No.setBorder(Rectangle.BOX);
s_No.setPaddingTop(2);
PdfPCell description = new PdfPCell(new Phrase("Description", font));
description.setBorder(Rectangle.BOX);
description.setPaddingTop(2);
PdfPCell ratePerUnit = new PdfPCell(new Phrase("Rate Per Unit", font));
ratePerUnit.setBorder(Rectangle.BOX);
ratePerUnit.setPaddingTop(2);
PdfPCell quantity = new PdfPCell(new Phrase("Quantity", font));
quantity.setBorder(Rectangle.BOX);
quantity.setPaddingTop(2);
PdfPCell inrReferenceAmount = new PdfPCell(new Phrase("INR Reference Amount", font));
inrReferenceAmount.setBorder(Rectangle.BOX);
inrReferenceAmount.setPaddingTop(2);
PdfPCell amountINR = new PdfPCell(new Phrase("Amount INR", font));
amountINR.setBorder(Rectangle.BOX);
amountINR.setPaddingTop(2);
table.addCell(s_No);
table.addCell(description);
table.addCell(ratePerUnit);
table.addCell(quantity);
table.addCell(inrReferenceAmount);
table.addCell(amountINR);
int i = 1;
String value = "123";
PdfPCell cell = null;
int referenceAmount = 0;
int amount = 0;
PdfPCell staticData1 = new PdfPCell(new Phrase("Please remit funds via telex transfer/SWIFT as follows to HSBC BANK Limited (Bank address:"+
"JMD Regent Square Ist Floor, DLF Phase II, MG Road, Gurgaon 122002, Haryana, India) with the following"+
"Instructions:", font));
staticData1.setPaddingTop(20);
staticData1.setColspan(6);
staticData1.setBorder(Rectangle.NO_BORDER);
PdfPCell staticData2 = new PdfPCell(new Phrase("Ultimate Beneficiary: Genpact India", font));
staticData2.setColspan(6);
staticData2.setBorder(Rectangle.NO_BORDER);
PdfPCell staticData3 = new PdfPCell(new Phrase("A)For Inward remittances from out side India : Remit by SWIFT MT103 for further credit to Genpact"+
"India, Acct no - 054 021365 001 with HSBC India (SWIFT - HSBCINBB) through HSBC Mumbai's account"+
"no.000-04417-2, with HSBC Bank, New York, USA having CHIPS code 302755, SWIFT- MRMDUS33 & ABA Routing"+
"No. 021-001-088.", font));
staticData3.setColspan(6);
staticData3.setBorder(Rectangle.NO_BORDER);
PdfPCell staticData4 = new PdfPCell(new Phrase("B)For remittance from within India: Remit to Genpact India HSBC Account No. 054 021365 001, IFSC Code"+
"- HSBC0110005.", font));
staticData4.setColspan(6);
staticData4.setBorder(Rectangle.NO_BORDER);
PdfPCell staticData5 = new PdfPCell(new Phrase("You are requested to send the remittances as per the Payment Term mentioned above."+
"Please send the payment notification to :collections.helpdesk@genpact.com"+
"Incase of any queries contact : genpactbilling.helpdesk@genpact.com", font));
staticData5.setColspan(6);
staticData5.setBorder(Rectangle.NO_BORDER);
PdfPCell staticData6 = new PdfPCell(new Phrase("FOR & ON BEHALF OF", font));
staticData6.setPaddingTop(25);
staticData6.setColspan(6);
staticData6.setBorder(Rectangle.NO_BORDER);
PdfPCell staticData7 = new PdfPCell(new Phrase("Genpact India", font));
staticData7.setColspan(6);
staticData7.setBorder(Rectangle.NO_BORDER);
PdfPCell staticData8 = new PdfPCell(new Phrase("(Private Company with Unlimited liability)", font));
staticData8.setColspan(6);
staticData8.setBorder(Rectangle.NO_BORDER);
PdfPCell staticData9 = new PdfPCell(sign);
staticData9.setColspan(6);
staticData9.setBorder(Rectangle.NO_BORDER);
PdfPCell staticData10 = new PdfPCell(new Phrase("Vidut Aggarwal", font));
staticData10.setColspan(6);
staticData10.setBorder(Rectangle.NO_BORDER);
PdfPCell staticData11 = new PdfPCell(new Phrase("This is a system generated Invoice and does not require physical signature.", font));
staticData11.setColspan(6);
staticData11.setBorder(Rectangle.NO_BORDER);
Chunk chunk0 = new Chunk(" ");
Chunk chunk = new Chunk(" ");
chunk.setUnderline(+1f,-2f);//1st co-ordinate is for line width,2nd is space between
document.open();
cb = writer.getDirectContent();
cb.setLineWidth(1);
MyPageEventListener mpel = new MyPageEventListener();
mpel.onEndPage(writer, document);
image.setAbsolutePosition(20, 795);
document.add(image);
document.add(chunk0);
document.add(chunk);
document.add(Chunk.NEWLINE);
cb.saveState();
cb.beginText();
cb.moveText(280, 810);
cb.setFontAndSize(bf, 10);
cb.showText("INVOICE");
cb.endText();
cb.beginText();
cb.moveText(20, 775);
cb.setFontAndSize(bf, 10);
cb.showText("Service Tax No : " + "d_ServiceTaxNo");
cb.endText();
cb.beginText();
cb.moveText(320, 788);
cb.setFontAndSize(bf, 10);
cb.showText("Genpact India");
cb.endText();
cb.beginText();
cb.moveText(316, 775);
cb.setFontAndSize(bf, 10);
cb.showText("(Private Company with unlimited liability)");
cb.endText();
cb.beginText();
cb.moveText(20, 750);
cb.setFontAndSize(bf, 10);
cb.showText("PAN : " + "d_PAN");
cb.endText();
cb.beginText();
cb.moveText(318, 760);
cb.setFontAndSize(bf, 10);
cb.showText("d_AddressLine1");
cb.endText();
cb.beginText();
cb.moveText(20, 725);
cb.setFontAndSize(bf, 10);
cb.showText("Category of services: ");
cb.endText();
cb.beginText();
cb.moveText(318, 746);
cb.setFontAndSize(bf, 10);
cb.showText("d_AddressLine2");
cb.endText();
cb.beginText();
cb.moveText(318, 732);
cb.setFontAndSize(bf, 10);
cb.showText("d_AddressLine3");
cb.endText();
cb.setColorStroke(color);
cb.rectangle(20,715,245,-100);
cb.stroke();
cb.beginText();
cb.moveText(25, 700);
cb.setFontAndSize(bf, 10);
cb.showText("d_cust_name");
cb.endText();
cb.beginText();
cb.moveText(25, 670);
cb.setFontAndSize(bf, 10);
cb.showText("d_cust_address");
cb.endText();
cb.beginText();
cb.moveText(318, 715);
cb.setFontAndSize(bf, 10);
cb.showText("d_pin");
cb.endText();
cb.beginText();
cb.moveText(318, 699);
cb.setFontAndSize(bf, 10);
cb.showText("Tele : " + "d_Telephone");
cb.endText();
cb.beginText();
cb.moveText(318, 685);
cb.setFontAndSize(bf, 10);
cb.showText("Fax : " + "d_Fax");
cb.endText();
cb.setColorStroke(color);
cb.rectangle(275,680,300,-80);
cb.stroke();
cb.setColorStroke(color);
cb.rectangle(10,90,575,742);
cb.stroke();
cb.setColorStroke(color);
cb.rectangle(275,680,300,-80);
cb.stroke();
cb.setColorStroke(color);
cb.rectangle(20,610,245,-30);
cb.stroke();
cb.beginText();
cb.moveText(280, 660);
cb.setFontAndSize(bf, 10);
cb.showText("Invoice Number: " + "d_Invoice_no.");
cb.endText();
cb.beginText();
cb.moveText(280, 645);
cb.setFontAndSize(bf, 10);
cb.showText("Invoice Date: " + "d_Invoice_date");
cb.endText();
cb.beginText();
cb.moveText(280, 626);
cb.setFontAndSize(bf, 10);
cb.showText("Currency : " + "d_Currency");
cb.endText();
cb.beginText();
cb.moveText(280, 605);
cb.setFontAndSize(bf, 10);
cb.showText("Payment Term : " + "d_Payment_term");
cb.endText();
cb.beginText();
cb.moveText(25, 590);
cb.setFontAndSize(bf, 10);
cb.showText("Attention : " + "Finance Dept");
cb.endText();
cb.restoreState();
document.add(Chunk.NEWLINE);
table.setTotalWidth(555.0f);
int sNo = 1;
float tableHeight1 = table.getTotalHeight();
System.out.println(tableHeight1);
int rowCount = 2;
int yAxis = 570;
int newPageYAxis = 0;
while (i <= 10){
for (int l=1;l<=6;l++){
if(l==1){
cell = new PdfPCell(new Phrase(String.valueOf(sNo), font));
cell.setBorder(Rectangle.LEFT | Rectangle.RIGHT);
cell.setPaddingTop(4);
sNo++;
}else{
cell = new PdfPCell(new Phrase(value, font));
cell.setBorder(Rectangle.LEFT | Rectangle.RIGHT);
cell.setPaddingTop(4);
}
if(i==10 && l==5){
cell = new PdfPCell(new Phrase(value, font));
cell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
cell.setPaddingTop(4);
}
if(i==10 && l==6){
cell = new PdfPCell(new Phrase(value, font));
cell.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
cell.setPaddingTop(4);
}
if(l==5){
referenceAmount = referenceAmount + Integer.parseInt(value);
}
if(l==5){
amount = amount + Integer.parseInt(value);
}
System.out.println(table.getTotalHeight());
if(table.getTotalHeight()<=450){
table.addCell(cell);
table.writeSelectedRows(0, 6, rowCount, -1, 20, yAxis, cb);
yAxis = yAxis - 15;
}
else {
document.newPage();
table.addCell(cell);
table.writeSelectedRows(0, 6, rowCount, -1, 20, yAxis, cb);
newPageYAxis = newPageYAxis + 15;
}
}
i++;
rowCount++;
}
System.out.println(rowCount);
PdfPCell cell1 = new PdfPCell(new Phrase("", font));
cell1.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
cell1.setPaddingTop(4);
PdfPCell cell2 = new PdfPCell(new Phrase("Total", font));
cell2.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
cell2.setPaddingTop(4);
PdfPCell cell3 = new PdfPCell(new Phrase("", font));
cell3.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
cell3.setPaddingTop(4);
PdfPCell cell4 = new PdfPCell(new Phrase("", font));
cell4.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
cell4.setPaddingTop(4);
PdfPCell cell5 = new PdfPCell(new Phrase(String.valueOf(referenceAmount), font));
cell5.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
cell5.setPaddingTop(4);
PdfPCell cell6 = new PdfPCell(new Phrase(String.valueOf(amount), font));
cell6.setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
cell6.setPaddingTop(4);
System.out.println(table.getLastCompletedRowIndex());
if(table.getTotalHeight()<=450){
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
table.addCell(cell5);
table.addCell(cell6);
table.writeSelectedRows(0, 6, 0, rowCount, 20, 570, cb);
System.out.println("IF: " + table.getTotalHeight());
rowCount++;
} else if (isPrinted==false){
document.newPage();
cb.setColorStroke(color);
cb.rectangle(10,90,575,742);
cb.stroke();
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
table.addCell(cell4);
table.addCell(cell5);
table.addCell(cell6);
table.addCell(staticData1);
table.addCell(staticData2);
table.addCell(staticData3);
table.addCell(staticData4);
table.addCell(staticData5);
table.addCell(staticData6);
table.addCell(staticData7);
table.addCell(staticData8);
table.addCell(staticData9);
table.addCell(staticData10);
table.addCell(staticData11);
table.writeSelectedRows(0, 6, rowCount, -1, 20, (790-newPageYAxis), cb);
System.out.println("ELSE: " + table.getTotalHeight());
rowCount++;
isPrinted = true;
}
if (table.getTotalHeight() <= 450){
table.addCell(staticData1);
table.writeSelectedRows(0, 6, 0, rowCount, 20, 570, cb);
rowCount++;
} else if (isPrinted==false) {
document.newPage();
cb.setColorStroke(color);
cb.rectangle(10,90,575,742);
cb.stroke();
table.addCell(staticData1);
table.addCell(staticData2);
table.addCell(staticData3);
table.addCell(staticData4);
table.addCell(staticData5);
table.addCell(staticData6);
table.addCell(staticData7);
table.addCell(staticData8);
table.addCell(staticData9);
table.addCell(staticData10);
table.addCell(staticData11);
table.writeSelectedRows(0, 6, rowCount, -1, 20, (790-newPageYAxis), cb);
isPrinted = true;
}
if (table.getTotalHeight() <= 450){
table.addCell(staticData2);
table.writeSelectedRows(0, 6, 0, rowCount, 20, 570, cb);
rowCount++;
}else if (isPrinted==false){
document.newPage();
cb.setColorStroke(color);
cb.rectangle(10,90,575,742);
cb.stroke();
table.addCell(staticData2);
table.addCell(staticData3);
table.addCell(staticData4);
table.addCell(staticData5);
table.addCell(staticData6);
table.addCell(staticData7);
table.addCell(staticData8);
table.addCell(staticData9);
table.addCell(staticData10);
table.addCell(staticData11);
table.writeSelectedRows(0, 6, rowCount, -1, 20, (790-newPageYAxis), cb);
isPrinted = true;
}
if (table.getTotalHeight() <= 450){
table.addCell(staticData3);
table.writeSelectedRows(0, 6, 0, rowCount, 20, 570, cb);
rowCount++;
} else if (isPrinted==false){
document.newPage();
cb.setColorStroke(color);
cb.rectangle(10,90,575,742);
cb.stroke();
table.addCell(staticData3);
table.addCell(staticData4);
table.addCell(staticData5);
table.addCell(staticData6);
table.addCell(staticData7);
table.addCell(staticData8);
table.addCell(staticData9);
table.addCell(staticData10);
table.addCell(staticData11);
table.writeSelectedRows(0, 6, rowCount, -1, 20, (790-newPageYAxis), cb);
isPrinted = true;
}
if (table.getTotalHeight() <= 450){
table.addCell(staticData4);
table.writeSelectedRows(0, 6, 0, rowCount, 20, 570, cb);
rowCount++;
} else if (isPrinted==false){
document.newPage();
cb.setColorStroke(color);
cb.rectangle(10,90,575,742);
cb.stroke();
table.addCell(staticData4);
table.addCell(staticData5);
table.addCell(staticData6);
table.addCell(staticData7);
table.addCell(staticData8);
table.addCell(staticData9);
table.addCell(staticData10);
table.addCell(staticData11);
table.writeSelectedRows(0, 6, rowCount, -1, 20, (790-newPageYAxis), cb);
isPrinted = true;
}
if (table.getTotalHeight() <= 450){
table.addCell(staticData5);
table.writeSelectedRows(0, 6, 0, rowCount, 20, 570, cb);
rowCount++;
} else if (isPrinted==false){
document.newPage();
cb.setColorStroke(color);
cb.rectangle(10,90,575,742);
cb.stroke();
table.addCell(staticData5);
table.addCell(staticData6);
table.addCell(staticData7);
table.addCell(staticData8);
table.addCell(staticData9);
table.addCell(staticData10);
table.addCell(staticData11);
table.writeSelectedRows(0, 6, rowCount, -1, 20, (790-newPageYAxis), cb);
isPrinted = true;
}
if (table.getTotalHeight() <= 450){
table.addCell(staticData6);
table.writeSelectedRows(0, 6, 0, rowCount, 20, 570, cb);
rowCount++;
} else if (isPrinted==false){
document.newPage();
cb.setColorStroke(color);
cb.rectangle(10,90,575,742);
cb.stroke();
table.addCell(staticData6);
table.addCell(staticData7);
table.addCell(staticData8);
table.addCell(staticData9);
table.addCell(staticData10);
table.addCell(staticData11);
table.writeSelectedRows(0, 6, rowCount, -1, 20, (790-newPageYAxis), cb);
isPrinted = true;
}
if (table.getTotalHeight() <= 450){
table.addCell(staticData7);
table.writeSelectedRows(0, 6, 0, rowCount, 20, 570, cb);
rowCount++;
} else if (isPrinted==false){
document.newPage();
cb.setColorStroke(color);
cb.rectangle(10,90,575,742);
cb.stroke();
table.addCell(staticData7);
table.addCell(staticData8);
table.addCell(staticData9);
table.addCell(staticData10);
table.addCell(staticData11);
table.writeSelectedRows(0, 6, rowCount, -1, 20, (790-newPageYAxis), cb);
isPrinted = true;
}
if (table.getTotalHeight() <= 450){
table.addCell(staticData8);
table.writeSelectedRows(0, 6, 0, rowCount, 20, 570, cb);
rowCount++;
}
else if (isPrinted==false){
document.newPage();
cb.setColorStroke(color);
cb.rectangle(10,90,575,742);
cb.stroke();
table.addCell(staticData8);
table.addCell(staticData9);
table.addCell(staticData10);
table.addCell(staticData11);
table.writeSelectedRows(0, 6, rowCount, -1, 20, (790-newPageYAxis), cb);
isPrinted = true;
}
if (table.getTotalHeight() <= 450){
System.out.println("staticData9");
table.addCell(staticData9);
table.writeSelectedRows(0, 6, 0, rowCount, 20, 570, cb);
rowCount++;
} else if (isPrinted==false){
document.newPage();
cb.setColorStroke(color);
cb.rectangle(10,90,575,742);
cb.stroke();
table.addCell(staticData9);
table.addCell(staticData10);
table.addCell(staticData11);
table.writeSelectedRows(0, 6, rowCount, -1, 20, (790-newPageYAxis), cb);
isPrinted = true;
}
if (table.getTotalHeight() <= 450){
System.out.println("staticData10");
table.addCell(staticData10);
table.writeSelectedRows(0, 6, 0, rowCount, 20, 570, cb);
rowCount++;
}else if (isPrinted==false){
document.newPage();
cb.setColorStroke(color);
cb.rectangle(10,90,575,742);
cb.stroke();
System.out.println("else staticData10");
table.addCell(staticData10);
table.addCell(staticData11);
table.writeSelectedRows(0, 6, rowCount-1, -1, 20, (790-newPageYAxis), cb);
isPrinted = true;
}
if (table.getTotalHeight() <= 450){
table.addCell(staticData11);
table.writeSelectedRows(0, 6, 0, rowCount, 20, 570, cb);
rowCount++;
} else if (isPrinted==false){
document.newPage();
cb.setColorStroke(color);
cb.rectangle(10,90,575,742);
cb.stroke();
table.addCell(staticData11);
table.writeSelectedRows(0, 6, rowCount, -1, 20, (790-newPageYAxis), cb);
isPrinted = true;
}
System.out.println("Column Index at last " + table.getLastCompletedRowIndex());
System.out.println(table.getTotalHeight());
document.close();
file.close();
System.out.println("Pdf created successfully..");
} catch (Exception e) {
e.printStackTrace();
}
}
}