How to change the title for each pdf page in android

I created a pdf page using itext in an android application, I need to add a header and footer for the pdf page and change the title on each page.

I liked adding headers and footers.

The 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 adding new values ​​to the header, which are displayed as merge text with both the previous text and the new header text. how can i dynamically change the title for each page

0
source share
1 answer

When asked how he adds new values ​​to the title, the OP clarified in a comment that he is doing this

sending values ​​to the constructor for each page

This indicates that it creates a new instance of HeaderAddFooter each time and redirects it to the PdfWriter setPageEvent method.

Unfortunately, the OP was unable or unwilling to provide more code. So let his code look 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 little unnamed (and also incorrectly documented in Javadocs): it is not a real setter, but rather 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 accepted OP code does not replace the old page event listener with a new one, but instead adds it .

There are two ways to fix this:

  • Or install only one instance of HeaderAddFooter 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 will most likely require more visibility for these HeaderAddFooter variables. Alternatively, customization methods may be added for them.

  • Or use the special null handling 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 option (using setter methods for cleaner code) is a more common option.

+2
source

Source: https://habr.com/ru/post/1487298/


All Articles