how to change header for every pdf page in android

2019-06-10 19:39发布

问题:

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

回答1:

When asked how he adds new values to header, the OP clarified in a comment that he does so by

sending values to constructor for each page

This seems to indicate that he every once in a while creates a new HeaderAddFooter instance and forwards it to the PdfWriter method setPageEvent.

Unfortunately the OP was not able or willing to provide more code. Thus, let's assume his code looks like this

Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, ...);
document.open();

for (int page = ...)
{
    String nameForPage = ...
    String idForPage = ...
    writer.setPageEvent(new HeaderAddFooter(nameForPage, idForPage));

    ... add content for page ...
    document.newPage();
}

document.close();

This, unfortunately, does not work as expected because PdfWriter.setPageEvent is a bit misnamed (and also mis-documented in the Javadocs): It is not a true setter but instead more like an adder:

public void setPageEvent(final PdfPageEvent event) {
    if (event == null) this.pageEvent = null;
    else if (this.pageEvent == null) this.pageEvent = event;
    else if (this.pageEvent instanceof PdfPageEventForwarder) ((PdfPageEventForwarder)this.pageEvent).addPageEvent(event);
    else {
        PdfPageEventForwarder forward = new PdfPageEventForwarder();
        forward.addPageEvent(this.pageEvent);
        forward.addPageEvent(event);
        this.pageEvent = forward;
    }
}

Thus, the OP's assumed code does not replace the former page event listener by a new one but insteads adds to it.

There are two ways to fix this:

  • Either set only one HeaderAddFooter instance initially outside the loop and change the contents of its members inside the loop:

    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, ...);
    document.open();
    HeaderAddFooter headerAddFooter = new HeaderAddFooter("", "");
    writer.setPageEvent(headerAddFooter);
    
    for (int page = ...)
    {
        String nameForPage = ...
        String idForPage = ...
        headerAddFooter.patientName = nameForPage;
        headerAddFooter.patientID = idForPage;
    
        ... add content for page ...
        document.newPage();
    }
    
    document.close();
    

    This most likely will require making those HeaderAddFooter member variables more visible. Alternatively setter methods may be added for them.

  • Or make use of the special treatment of null in PdfWriter.setPageEvent:

    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, ...);
    document.open();
    
    for (int page = ...)
    {
        String nameForPage = ...
        String idForPage = ...
        writer.setPageEvent(null);
        writer.setPageEvent(new HeaderAddFooter(nameForPage, idForPage));
    
        ... add content for page ...
        document.newPage();
    }
    
    document.close();
    

The first alternative (with the use of setter methods for cleaner code) is the more common variant.