Java tutorial
/******************************************************************************* * Educational Online Test Delivery System * Copyright (c) 2013 American Institutes for Research * * Distributed under the AIR Open Source License, Version 1.0 * See accompanying file AIR-License-1_0.txt or at * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf ******************************************************************************/ package org.opentestsystem.delivery.testreg.rest.view; import com.lowagie.text.Document; import com.lowagie.text.Element; import com.lowagie.text.ExceptionConverter; import com.lowagie.text.Phrase; import com.lowagie.text.Rectangle; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.ColumnText; import com.lowagie.text.pdf.PdfContentByte; import com.lowagie.text.pdf.PdfPageEventHelper; import com.lowagie.text.pdf.PdfTemplate; import com.lowagie.text.pdf.PdfWriter; public class PdfReportPageEventHelper extends PdfPageEventHelper { protected PdfTemplate template; protected BaseFont helv; public PdfReportPageEventHelper(final PdfWriter writer) { template = writer.getDirectContent().createTemplate(100, 100); template.setBoundingBox(new Rectangle(-20, -20, 100, 100)); try { helv = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1257, BaseFont.NOT_EMBEDDED); } catch (Exception e) { throw new ExceptionConverter(e); } } @Override public void onEndPage(final PdfWriter writer, final Document document) { PdfContentByte cb = writer.getDirectContent(); if (document.getPageNumber() == 1) { ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase(""), (document.right() - document.left()) / 2 + document.leftMargin(), document.top() - 5, 0f); } ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase(""), document.left(), document.bottom() - 15, 0f); int pageN = writer.getPageNumber(); String text = "Page " + pageN + " of "; cb.beginText(); cb.setFontAndSize(helv, 11); cb.setTextMatrix(document.right() - document.rightMargin() - 10, document.bottom() - 15); cb.showText(text); cb.endText(); cb.addTemplate(template, document.right(), document.bottom() - 15); } @Override public void onCloseDocument(final PdfWriter writer, final Document document) { template.beginText(); template.setFontAndSize(helv, 11); template.setTextMatrix(0, 0); template.showText("" + (writer.getPageNumber() - 1)); template.endText(); } }