LJ Nieland
LJ Nieland

Reputation: 11

How to create table in footer with content from database

Trying to create stationary for pdf generated invoices, I tried to use different techniques from the book iText in Action. But what I need doesn't seem to be in the book. In my case, the text in each cell are not literals as in the example I send, but need to be read from a database. It's not a good idea to put the code for reading from database in these onEndPage method. If I have to do this in the caller method, how will the data being tranferred tot MyPageEventListener? In other words: how can I put it all together?

public class MyPageEventListener extends PdfPageEventHelper {
  . . .
  @Override
  public void onEndPage(PdfWriter writer, Document document) {
     //code skeleton to write page header
     PdfPTable tbl = new PdfPTable(3);
     tbl.addCell("1st cell");  //companyname
     tbl.addCell("2nd cell");  //address
     tbl.addCell("3rd cell");  //city
     tbl.addCell("4st cell");  //bankaccount
     tbl.addCell("5nd cell");  //swiftcode
     tbl.addCell("6rd cell");  //vat number
     float x = document.leftMargin();
     float hei = header.getTotalHeight(); //altezza dell'intestazione
     //align bottom between page edge and page margin
     float y = c_document.top() + hei;

     //write the table
     tbl.writeSelectedRows(0, -1, x, y, writer.getDirectContent());
  }    
}

I didn't find the answer anywhere else yet, so please every help will be highly appreciated. Lourens.

Upvotes: 1

Views: 324

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77606

I wrote the book, so allow me to answer.

You should have something like this:

public class MyData {
    private String companyName;
    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }
    public String getCompanyName() {
        return companyName;
    }
}

public class MyPageEventListener extends PdfPageEventHelper {
  private MyData myData;
  public setMyData(MyData myData) {
    this.myData = myData;
  }

  ....

  @Override
  public void onEndPage(PdfWriter writer, Document document) {
     //code skeleton to write page header
     PdfPTable tbl = new PdfPTable(3);
     tbl.addCell(myData.getCompanyName());  //companyname
     tbl.addCell(myData.getAddress());  //address
     tbl.addCell(myData.getCity());  //city
     ...
     float x = document.leftMargin();
     float hei = header.getTotalHeight(); //altezza dell'intestazione
     //align bottom between page edge and page margin
     float y = c_document.top() + hei;

     //write the table
     tbl.writeSelectedRows(0, -1, x, y, writer.getDirectContent());
  }    
}

Now, whenever you want to change the footer info, you do so from the class that creates your PDF. This is shown in the MovieCountries1 example in the book you refer to. In that example, the header is the name of a country taken from the database. Instead of a setMyData() method, I used a setHeader() method to change the content of the header.

Being the original developer of iText and the author of the book "iText in Action", I value your feedback. Please take some time to fill out our survey.

Upvotes: 1

Related Questions