Java tutorial
package com.workhub.utils; import java.io.FileOutputStream; import java.io.IOException; import java.net.MalformedURLException; import java.text.DateFormat; import java.util.Date; import java.util.List; import java.util.Locale; import com.itextpdf.text.Anchor; import com.itextpdf.text.BaseColor; 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.Image; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import com.workhub.model.ElementModel; import com.workhub.model.FileElementModel; import com.workhub.model.LinkElementModel; import com.workhub.model.PictureElementModel; import com.workhub.model.TextElementModel; public class PDFUtils { private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD); private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.RED); private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 15, Font.BOLD); private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD); public static void createPDF(String title, String creator, List<ElementModel> models, FileOutputStream outputStream) { try { Document document = new Document(); PdfWriter.getInstance(document, outputStream); document.open(); addMetaData(document, title, creator); addTitlePage(document, title, models, creator); addContent(document, models); document.close(); } catch (Exception e) { e.printStackTrace(); } } private static void addMetaData(Document document, String title, String creator) { document.addTitle(title); // document.addSubject("Using iText"); // document.addKeywords("Java, PDF, iText"); // document.addAuthor("Lars Vogel"); document.addCreator(creator); } private static void addTitlePage(Document document, String title, List<ElementModel> models, String creator) throws DocumentException { Paragraph preface = new Paragraph(); // We add one empty line addEmptyLine(preface, 1); // Lets write a big header Paragraph titleParag = new Paragraph(title, catFont); titleParag.setAlignment(Element.ALIGN_CENTER); preface.add(titleParag); addEmptyLine(preface, 1); // Will create: Report generated by: _name, _date // 1. Choix de la langue Locale locale = Locale.getDefault(); /** 2. Construction du DateFormat en choisiant un format : * SHORT = 01/01/2002 * FULL = lundi 1 janvier 2002 */ DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL, locale); preface.add(new Paragraph("Rapport gnr par: " + creator + ", " + dateFormat.format(new Date()), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ smallBold)); addEmptyLine(preface, 3); preface.add(new Paragraph("Ce document reprend les contenus suivants: ", subFont)); addEmptyLine(preface, 1); Paragraph sommaire = new Paragraph("", smallBold); preface.add(sommaire); createList(sommaire, models); /*preface.add(new Paragraph("This document is a preliminary version and not subject to your license agreement or any other agreement with vogella.com ;-).", redFont)); */ document.add(preface); // Start a new page document.newPage(); } private static void addContent(Document document, List<ElementModel> models) throws DocumentException { for (int i = 0; i < models.size(); i++) { ElementModel elementModel = models.get(i); Anchor anchor = new Anchor(elementModel.getTitle(), catFont); anchor.setName(elementModel.getTitle()); // Second parameter is the number of the chapter Chapter catPart = new Chapter(new Paragraph(anchor), i + 1); Paragraph content = new Paragraph(); catPart.add(content); addEmptyLine(content, 2); switch (elementModel.getType()) { case Constants.TYPE_ELEMENT_TEXT: content.add(new Paragraph(((TextElementModel) elementModel).getContent(), smallBold)); break; case Constants.TYPE_ELEMENT_LINK: content.add(new Paragraph(((LinkElementModel) elementModel).getContent(), smallBold)); break; case Constants.TYPE_ELEMENT_PICTURE: Image image1; try { if (((PictureElementModel) elementModel).getContent() != null) { image1 = Image.getInstance(((PictureElementModel) elementModel).getContent()); content.add(image1); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } break; case Constants.TYPE_ELEMENT_FILE: content.add(new Paragraph(((FileElementModel) elementModel).getTitle(), smallBold)); break; } document.add(catPart); } } // private static void createTable(Section subCatPart) // throws BadElementException { // PdfPTable table = new PdfPTable(3); // // // t.setBorderColor(BaseColor.GRAY); // // t.setPadding(4); // // t.setSpacing(4); // // t.setBorderWidth(1); // // PdfPCell c1 = new PdfPCell(new Phrase("Table Header 1")); // c1.setHorizontalAlignment(Element.ALIGN_CENTER); // table.addCell(c1); // // c1 = new PdfPCell(new Phrase("Table Header 2")); // c1.setHorizontalAlignment(Element.ALIGN_CENTER); // table.addCell(c1); // // c1 = new PdfPCell(new Phrase("Table Header 3")); // c1.setHorizontalAlignment(Element.ALIGN_CENTER); // table.addCell(c1); // table.setHeaderRows(1); // // table.addCell("1.0"); // table.addCell("1.1"); // table.addCell("1.2"); // table.addCell("2.1"); // table.addCell("2.2"); // table.addCell("2.3"); // // subCatPart.add(table); // // } private static void createList(Paragraph sommaire, List<ElementModel> models) { com.itextpdf.text.List list = new com.itextpdf.text.List(true, false, 10); for (ElementModel elementModel : models) { list.add(elementModel.getTitle()); } sommaire.add(list); } private static void addEmptyLine(Paragraph paragraph, int number) { for (int i = 0; i < number; i++) { paragraph.add(new Paragraph(" ")); } } }