I'm working with a pdf which display many tables. When a table doesn't fit on the rest of the page, iText cut the table and set the rest of them on the next page.
我正在使用显示许多表的pdf。如果表格不适合页面的其余部分,iText会剪切表格并将其余部分设置在下一页上。
I want that if a table doesn't fit on the rest of the page, then place it on a new page.
我希望如果表格不适合页面的其余部分,则将其放在新页面上。
How can I do that?
我怎样才能做到这一点?
3 个解决方案
#1
3
There are different ways to do this; these are three options:
有不同的方法可以做到这一点;这三种选择:
- Calculate the height of the table, compare it to the space that is left. When you calculate the height of a table before it's added to the document, the height is always 0, UNLESS you define the width of the table using setTotalWidth() and setLockedWidth(). You can find the current Y position on the page using the getVerticalPosition() method (in PdfWriter).
- Add the table to a ColumnText object, and add the ColumnText object to a rectangle defining the space left on the page in simulation mode. If the content of the ColumnText object couldn't be rendered entirely, trigger a new Page before adding the table for real.
- If you take the most recent (not yet released) version from the trunk (version 5.3.3-SNAPSHOT) on SourceForge, you can avoid a breaks by using table.keepRowsTogether(0); where 0 is the index of the first row in the table.
计算表格的高度,将其与剩余的空间进行比较。在将表添加到文档之前计算表的高度时,高度始终为0,除非您使用setTotalWidth()和setLockedWidth()定义表的宽度。您可以使用getVerticalPosition()方法(在PdfWriter中)在页面上找到当前的Y位置。
将表添加到ColumnText对象,并将ColumnText对象添加到矩形,该矩形在模拟模式下定义页面上剩余的空间。如果无法完全呈现ColumnText对象的内容,请在添加真实表之前触发新页面。
如果从SourceForge上的trunk(版本5.3.3-SNAPSHOT)中获取最新(尚未发布)的版本,则可以通过使用table.keepRowsTogether(0)来避免中断;其中0是表中第一行的索引。
#2
0
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");
#3
0
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();
}
}
}
#1
3
There are different ways to do this; these are three options:
有不同的方法可以做到这一点;这三种选择:
- Calculate the height of the table, compare it to the space that is left. When you calculate the height of a table before it's added to the document, the height is always 0, UNLESS you define the width of the table using setTotalWidth() and setLockedWidth(). You can find the current Y position on the page using the getVerticalPosition() method (in PdfWriter).
- Add the table to a ColumnText object, and add the ColumnText object to a rectangle defining the space left on the page in simulation mode. If the content of the ColumnText object couldn't be rendered entirely, trigger a new Page before adding the table for real.
- If you take the most recent (not yet released) version from the trunk (version 5.3.3-SNAPSHOT) on SourceForge, you can avoid a breaks by using table.keepRowsTogether(0); where 0 is the index of the first row in the table.
计算表格的高度,将其与剩余的空间进行比较。在将表添加到文档之前计算表的高度时,高度始终为0,除非您使用setTotalWidth()和setLockedWidth()定义表的宽度。您可以使用getVerticalPosition()方法(在PdfWriter中)在页面上找到当前的Y位置。
将表添加到ColumnText对象,并将ColumnText对象添加到矩形,该矩形在模拟模式下定义页面上剩余的空间。如果无法完全呈现ColumnText对象的内容,请在添加真实表之前触发新页面。
如果从SourceForge上的trunk(版本5.3.3-SNAPSHOT)中获取最新(尚未发布)的版本,则可以通过使用table.keepRowsTogether(0)来避免中断;其中0是表中第一行的索引。
#2
0
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");
#3
0
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();
}
}
}