import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfWriter;
public class MainClass extends PdfPageEventHelper {
protected Phrase header = new Phrase("header");
protected PdfPTable footer = new PdfPTable(4);
public MainClass() {
footer.setTotalWidth(300);
footer.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
footer.addCell(new Phrase(new Chunk("First").setAction(new PdfAction(PdfAction.FIRSTPAGE))));
footer.addCell(new Phrase(new Chunk("Prev").setAction(new PdfAction(PdfAction.PREVPAGE))));
footer.addCell(new Phrase(new Chunk("Next").setAction(new PdfAction(PdfAction.NEXTPAGE))));
footer.addCell(new Phrase(new Chunk("Last").setAction(new PdfAction(PdfAction.LASTPAGE))));
}
public void onEndPage(PdfWriter writer, Document document) {
PdfContentByte cb = writer.getDirectContent();
if (document.getPageNumber() > 1) {
ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, header, (document.right() - document
.left())
/ 2 + document.leftMargin(), document.top() + 10, 0);
}
footer.writeSelectedRows(0, -1, (document.right() - document.left() - 300) / 2
+ document.leftMargin(), document.bottom() - 10, cb);
}
public static void main(String[] args) throws Exception {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
writer.setViewerPreferences(PdfWriter.PageLayoutTwoColumnLeft);
writer.setPageEvent(new MainClass());
document.open();
for (int k = 1; k <= 300; ++k) {
document.add(new Phrase("test "));
}
document.close();
}
}
|