Java tutorial
package uk.ac.openmf.utils; import java.util.ArrayList; import java.util.Date; import uk.ac.openmf.model.OpenMFLoanAccount; import uk.ac.openmf.model.OpenMFSavingsAccount; import uk.ac.openmf.model.OpenMFTask; import uk.ac.openmf.model.OpenMFUser; import uk.ac.openmf.web.AppContext; import com.itextpdf.text.Anchor; import com.itextpdf.text.Chapter; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.Paragraph; import com.itextpdf.text.Phrase; import com.itextpdf.text.Section; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; public class OpenMFPDFGenerator { private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 11, Font.NORMAL); // iText allows to add metadata to the PDF which can be viewed in your Adobe // Reader // under File -> Properties public static void addMetaData(Document document, String title, String subject) { document.addTitle(title); document.addSubject(subject); } public static void addTitlePage(Document document, String title) throws DocumentException { Paragraph preface = new Paragraph(); // We add one empty line addEmptyLine(preface, 1); // Lets write a big header preface.add(new Paragraph(title, catFont)); addEmptyLine(preface, 1); // Will create: Report generated by: _name, _date preface.add(new Paragraph("Report generated by: " + OpenMFConstants.FIELD_VALUE_SYSTEM + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ catFont)); addEmptyLine(preface, 3); preface.add(new Paragraph("This is a sample system generated report", catFont)); addEmptyLine(preface, 8); preface.add(new Paragraph( "This document is a preliminary version and not subject to your license agreement or any other agreement with OpenMicrofinance.", catFont)); document.add(preface); // Start a new page document.newPage(); } public static void addContent(Document document, String title, String clientId) throws DocumentException { Anchor anchor = new Anchor(title, catFont); anchor.setName(title); // Second parameter is the number of the chapter Chapter catPart = new Chapter(new Paragraph(anchor), 1); Paragraph subPara = new Paragraph("Loan Account Overview", catFont); addEmptyLine(subPara, 1); Section subCatPart = catPart.addSection(subPara); // add a table createLAOTable(subCatPart, clientId); Paragraph subPara1 = new Paragraph("Savings Account Overview", catFont); addEmptyLine(subPara1, 1); Section subCatPart1 = catPart.addSection(subPara1); // add a table createSAOTable(subCatPart1, clientId); // now add all this to the document document.add(catPart); } private static void createLAOTable(Section subCatPart, String clientId) throws DocumentException { PdfPTable table = new PdfPTable(5); // t.setBorderColor(BaseColor.GRAY); // t.setPadding(4); // t.setSpacing(4); // t.setBorderWidth(1); PdfPCell c1 = new PdfPCell(new Phrase("Loan Acc#")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Loan Code")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Approved Amount")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("OutStd Amount")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Start Date")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); float[] columnWidths = new float[] { 10f, 10f, 15f, 15f, 10f }; table.setWidths(columnWidths); table.setHeaderRows(1); ArrayList<OpenMFLoanAccount> loanAccounts = OMFUtils.getLoanAccountsByClientList(clientId); //ArrayList<OpenMFSavingsAccount> savingsAccounts = OMFUtils.getSavingsAccountsByClientList(clientId); for (OpenMFLoanAccount openMFLoanAccount : loanAccounts) { table.addCell(openMFLoanAccount.getLoanaccountnumber()); table.addCell(openMFLoanAccount.getLoancode()); table.addCell(openMFLoanAccount.getApprovedamount()); table.addCell(openMFLoanAccount.getBalanceoutstandingamount()); table.addCell(openMFLoanAccount.getLoanstartdate()); } subCatPart.add(table); } private static void createSAOTable(Section subCatPart, String clientId) throws DocumentException { PdfPTable table = new PdfPTable(5); PdfPCell c1 = new PdfPCell(new Phrase("Savings Acc#")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Savings Code")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Available Balance")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Total# Deposits")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Matures On")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); float[] columnWidths = new float[] { 10f, 10f, 15f, 15f, 10f }; table.setWidths(columnWidths); table.setHeaderRows(1); ArrayList<OpenMFSavingsAccount> savingsAccounts = OMFUtils.getSavingsAccountsByClientList(clientId); for (OpenMFSavingsAccount openMFSavingsAccount : savingsAccounts) { table.addCell(openMFSavingsAccount.getSavingsaccountnumber()); table.addCell(openMFSavingsAccount.getSavingscode()); table.addCell(openMFSavingsAccount.getAvailablebalance()); table.addCell(openMFSavingsAccount.getTotalnumdeposits()); table.addCell(openMFSavingsAccount.getMatureson()); } subCatPart.add(table); } public static void addTaskContent(Document document, String title, String omfuId) throws DocumentException { Anchor anchor = new Anchor(title, catFont); anchor.setName(title); // Second parameter is the number of the chapter Chapter catPart = new Chapter(new Paragraph(anchor), 1); Paragraph subPara = new Paragraph("User Tasks Overview", catFont); addEmptyLine(subPara, 1); Section subCatPart = catPart.addSection(subPara); // add a table createTasksTable(subCatPart, omfuId); // now add all this to the document document.add(catPart); } private static void createTasksTable(Section subCatPart, String omfuId) throws DocumentException { PdfPTable table = new PdfPTable(5); PdfPCell c1 = new PdfPCell(new Phrase("Task#")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Date Assigned")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Amount")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Acc#")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); c1 = new PdfPCell(new Phrase("Type")); c1.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell(c1); float[] columnWidths = new float[] { 10f, 10f, 15f, 15f, 10f }; table.setWidths(columnWidths); table.setHeaderRows(1); OpenMFUser omfuser = null; if (omfuId != null) { omfuser = AppContext.getAppContext().getUserManager().getUser(ServletUtils.validateEventId(omfuId)); } ArrayList<OpenMFTask> tasks = OMFUtils.getTasksByUsername(omfuser.getUsername(), false); for (OpenMFTask task : tasks) { table.addCell(task.getTaskId()); table.addCell(task.getDateassigned()); table.addCell(task.getAmount()); table.addCell(task.getAccountnumber()); table.addCell(task.getCollectiontype()); } subCatPart.add(table); } private static void addEmptyLine(Paragraph paragraph, int number) { for (int i = 0; i < number; i++) { paragraph.add(new Paragraph(" ")); } } }