Hi Everybody,
I have just started learning iText. Could you please tell me what stupid thing I have done here? The OnEndPage method is not working as expected (though I haven't changed anything in it).
Problem,
A set of Customer data.
a) Every Customer data starts on new page.
b) All headers in pages related to a particular customer should show the Customer name.
Tweaked the code See the code pasted below.
Result
Header is showing the same value as the first customer. Currently I am printing the same data twice in a loop, and watching on the count value.
Anybody please help.
Thanks and Regards
Nilabhra banerjee
public class EndPage extends PdfPageEventHelper
{
// This count is the customer number . Used to loop.
// and also in page header as count+1.
int count = 0;
public static void main(String[] args)
{
EndPage ep = new EndPage();
ep.mainTask();
}
public void onEndPage(PdfWriter writer, Document document) {
try {
Rectangle page = document.getPageSize();
PdfPTable head = new PdfPTable(3);
for (int k = 1; k <= 6; ++k)
{
head.addCell("head " + k + " Cust No. = " + (count+1));
}
System.out.println(page.width());
head.setTotalWidth(page.width() - document.leftMargin() - document.rightMargin());
head.writeSelectedRows(0, -1, document.leftMargin(), page.height() - document.topMargin() + head.getTotalHeight(),
writer.getDirectContent());
PdfPTable foot = new PdfPTable(3);
for (int k = 1; k <= 6; ++k)
foot.addCell("foot " + k);
foot.setTotalWidth(page.width() - document.leftMargin() - document.rightMargin());
foot.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(),
writer.getDirectContent());
}
catch (Exception e) {
throw new ExceptionConverter(e);
}
}
/**
* Main task is to reiterate through an arraylist.
* Here, in absence of arraylist I am printing the same thing 3 times.
*
*/
private void mainTask()
{
try {
Document document = new Document(PageSize.A4, 50, 50, 70, 70);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("endpage.pdf"));
writer.setPageEvent(new EndPage());
document.open();
// Currently considering I am going thru a ArrayList
for(count = 0; count < 2; count++){
if(count > 0){
writer.setPageEvent(new EndPage());
document.newPage();
}
document = loopedTask(document, writer, count);
}
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// This count is the customer number . Used to loop.
// and also in page header as count+1.
int count = 0;
public static void main(String[] args)
{
EndPage ep = new EndPage();
ep.mainTask();
}
public void onEndPage(PdfWriter writer, Document document) {
try {
Rectangle page = document.getPageSize();
PdfPTable head = new PdfPTable(3);
for (int k = 1; k <= 6; ++k)
{
head.addCell("head " + k + " Cust No. = " + (count+1));
}
System.out.println(page.width());
head.setTotalWidth(page.width() - document.leftMargin() - document.rightMargin());
head.writeSelectedRows(0, -1, document.leftMargin(), page.height() - document.topMargin() + head.getTotalHeight(),
writer.getDirectContent());
PdfPTable foot = new PdfPTable(3);
for (int k = 1; k <= 6; ++k)
foot.addCell("foot " + k);
foot.setTotalWidth(page.width() - document.leftMargin() - document.rightMargin());
foot.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(),
writer.getDirectContent());
}
catch (Exception e) {
throw new ExceptionConverter(e);
}
}
/**
* Main task is to reiterate through an arraylist.
* Here, in absence of arraylist I am printing the same thing 3 times.
*
*/
private void mainTask()
{
try {
Document document = new Document(PageSize.A4, 50, 50, 70, 70);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("endpage.pdf"));
writer.setPageEvent(new EndPage());
document.open();
// Currently considering I am going thru a ArrayList
for(count = 0; count < 2; count++){
if(count > 0){
writer.setPageEvent(new EndPage());
document.newPage();
}
document = loopedTask(document, writer, count);
}
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private Document loopedTask(Document document, PdfWriter writer,
int i)
{try {
String text = "Lots of text of loop " + i + ".";
PdfContentByte cB = writer.getDirectContent();
//will have some other design here
cB.roundRectangle(Float.parseFloat("50"),document.top() - Float.parseFloat("71"),Float.parseFloat("500"),Float.parseFloat("70"),Float.parseFloat("11"));
cB.stroke();
PdfPTable ptable = new PdfPTable(2);
ptable.setWidthPercentage(80);
ptable.getDefaultCell().setPaddingLeft(4);
ptable.getDefaultCell().setPaddingTop(0);
ptable.getDefaultCell().setPaddingBottom(4);
ptable.getDefaultCell().setBorder(0);
{try {
String text = "Lots of text of loop " + i + ".";
PdfContentByte cB = writer.getDirectContent();
//will have some other design here
cB.roundRectangle(Float.parseFloat("50"),document.top() - Float.parseFloat("71"),Float.parseFloat("500"),Float.parseFloat("70"),Float.parseFloat("11"));
cB.stroke();
PdfPTable ptable = new PdfPTable(2);
ptable.setWidthPercentage(80);
ptable.getDefaultCell().setPaddingLeft(4);
ptable.getDefaultCell().setPaddingTop(0);
ptable.getDefaultCell().setPaddingBottom(4);
ptable.getDefaultCell().setBorder(0);
// get data ... now same for all, but i changes
// i is nothing but count value.
String pName = "N. Banerjee(" + i + ")";
String pRN = "99900102";
String pGender = "Male";
String pAge = "35y 11m";
String pPhone = "(234)925924";
String patient = "Patient Name: " + pName + "\n" +" RN : " + pRN+ "\n" + " Gender: "
+ pGender + "\n" + " Age: " +pAge + "\n" + " Phone: " + pPhone ;
String rProvider = "N. Banerjee";
String rId = "000043238";
String provider = "Requesting Provider: " + rProvider + "\n" + " Provider Id: " + rId;
ptable.addCell(new Phrase(patient));
ptable.addCell(new Phrase(provider));
// Chunk ck = new Chunk(ptable);
document.add(ptable);
for (int k = 0; k < 10; ++k)
text += text;
document.add(new Paragraph(text));
}
catch (Exception de) {
de.printStackTrace();
}
return document;
}
// i is nothing but count value.
String pName = "N. Banerjee(" + i + ")";
String pRN = "99900102";
String pGender = "Male";
String pAge = "35y 11m";
String pPhone = "(234)925924";
String patient = "Patient Name: " + pName + "\n" +" RN : " + pRN+ "\n" + " Gender: "
+ pGender + "\n" + " Age: " +pAge + "\n" + " Phone: " + pPhone ;
String rProvider = "N. Banerjee";
String rId = "000043238";
String provider = "Requesting Provider: " + rProvider + "\n" + " Provider Id: " + rId;
ptable.addCell(new Phrase(patient));
ptable.addCell(new Phrase(provider));
// Chunk ck = new Chunk(ptable);
document.add(ptable);
for (int k = 0; k < 10; ++k)
text += text;
document.add(new Paragraph(text));
}
catch (Exception de) {
de.printStackTrace();
}
return document;
}
}
Send instant messages to your online friends http://uk.messenger.yahoo.com
------------------------------------------------------------------------- Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________ iText-questions mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/itext-questions
