Android : how to change header for every pdf page in android

on Tuesday, July 29, 2014


I created pdf page using itext in android application,need to add header and footer for pdf page and need to change header in every page.


i did like for adding header and footer


public class HeaderAddFooter extends PdfPageEventHelper {



private Phrase footer;
private Phrase header;
private String patientName;
private String patientID;
Font headerFont = new Font(FontFamily.TIMES_ROMAN, 12, Font.NORMAL);
Font footerFont = new Font(FontFamily.TIMES_ROMAN, 12, Font.NORMAL);
Font TitleFont = new Font(FontFamily.COURIER, 14, Font.NORMAL);

/*
* constructor
*/
public HeaderAddFooter(String name,String id) {
super();

header = new Phrase("***** Header *****");
footer = new Phrase("**** Footer ****");
patientName=name;
patientID=id;
}


@Override
public void onStartPage(PdfWriter writer, Document document) {

super.onStartPage(writer, document);


}

@Override
public void onEndPage(PdfWriter writer, Document document) {

PdfContentByte cb = writer.getDirectContent();

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
// Date date = new Date();
Calendar cal = Calendar.getInstance();



ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("welcome",headerFont),
document.leftMargin()-1, document.top()+10, 0);

ColumnText.showTextAligned(cb, Element.ALIGN_RIGHT, new Phrase("Name",headerFont),
document.right(), document.top()+15, 0);
ColumnText.showTextAligned(cb, Element.ALIGN_RIGHT, new Phrase("Designation",headerFont),
document.right(), document.top()+1, 0);


/*
* Foooter
*/

ColumnText.showTextAligned(cb, Element.ALIGN_MIDDLE, new Phrase("Page:"+String.format(" %d ",
writer.getPageNumber()),footerFont),
document.right() - 280 , document.bottom() - 10, 0);
ColumnText.showTextAligned(cb, Element.ALIGN_MIDDLE, new Phrase(dateFormat.format(cal.getTime()),footerFont),

document.right() - 350 , document.bottom() - 25, 0);


}


}


now when add new values to header it showing like merge text both previous text and new header text. how can i change header dynamically for every page


0 comments:

Post a Comment