I have read a lot about creating headers/footers using page events. I
created a new class that extends PdfPageHelper, with an OnEndPage method
that inserts my header and footer. The problem is that I want my new class
(called ComplexHeader) to be reusable. Rather than create the header within
the constructor of ComplexHeader, I want to pass it in, and then just place
it on the page within the OnEndPage method. I need the header to take up to
three tables, plus images, if necessary. Right now, I am grouping those
tables/images into a chapter in my main file, and then passing the chapter
to ComplexHeader in a method called setHeader.
Now the problem: within setHeader, I attempt to assign the chapter that is
passed to a private variable that can be accessed throughout ComplexHeader.
I cannot figure out how to copy a chapter to another chapter (copy the local
chapter variable to a global chapter variable) or even to any other
higher-level object that can then be placed at an absolute position on the
page. Please see my code below and give any suggestions. The object passed
in to ComplexHeader does not have to be a chapter, but it must be some sort
of object that can contain tables within it.
In my main program, I have this code:
<code>
Complex Header complexHeader = new ComplexHeader();
writer.setPageEvent(complexHeader);
//Create multiple tables here, and add them to the chapter
//...
//...
complexHeader.setPageHeader(chapter, dataHeight, document);
//Open the document
//Create the rest of the elements for the document and add them to the
document
//Close the document
</code>
Then I have the ComplexHeader class:
<code>
public class ComplexHeader extends PdfPageEventHelper {
private UFmt UFmt = new UFmt();
private float pdfHeaderPadding = 20;
private Chapter pdfHeaderChapter = null;
private float headerDataHeight;
private float origTopMargin;
private boolean setHeader = false;
private int currChapter = 1;
public ComplexHeader() {
; //Do nothing, just initialize the object
}
public void setPageHeader (Chapter iHeaderChapter, float iDataHeight,
Document document) {
pdfHeaderChapter = new Chapter(new Paragraph(""), currChapter);
currChapter++;
//This line doesn't work, but I need to be able to access the data
in iHeaderChapter from within the OnEndPage method -- how else can I do
this?
pdfHeaderChapter.add(iHeaderChapter);
headerDataHeight = iDataHeight;
document.setMargins(document.leftMargin(), document.rightMargin(),
(document.topMargin() + headerDataHeight + pdfHeaderPadding),
document.bottomMargin());
}
public void onEndPage(PdfWriter writer, Document document) {
PdfContentByte cb = writer.getDirectContent();
if (pdfHeaderChapter != null) {
//Using ColumnText so the header can be positioned absolutely,
but if something else works, can use any object, as long as it can be
positioned absolutely
ColumnText headerCT = new ColumnText(cb);
headerCT.setSimpleColumn(document.left(),document.top(),
document.right(), document.top() + headerDataHeight, 0, Element.ALIGN_LEFT);
headerCT.addElement(pdfHeaderChapter);
}
}
}
</code>
Thanks in advance for your help!
--
View this message in context:
http://itext-general.2136553.n4.nabble.com/Page-Events-can-I-create-a-copy-of-a-chapter-tp2221377p2221377.html
Sent from the iText - General mailing list archive at Nabble.com.
------------------------------------------------------------------------------
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://www.itextpdf.com/book/
Check the site with examples before you ask questions:
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/