za.org.rfm.pdf.EventPDF.java Source code

Java tutorial

Introduction

Here is the source code for za.org.rfm.pdf.EventPDF.java

Source

package za.org.rfm.pdf;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
import za.org.rfm.model.Event;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Date;

/**
 * User: Russel.Mupfumira
 * Date: 2014/05/02
 * Time: 3:42 PM
 */
public class EventPDF {
    private Event event;
    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, 16, Font.BOLD);
    private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);

    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

    public static void generatePDF(Event event) {
        try {
            System.out.println("----now creating pdf----");
            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream("EventReport" + event.getId() + ".pdf"));
            document.open();
            addMetaData(document);
            addTitlePage(document);
            addContent(document);
            document.close();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    private static void addMetaData(Document document) {
        document.addTitle("Event report");
        document.addSubject("Test PDF report");
        document.addKeywords("RFM, PDF, iText");
        document.addAuthor("Lars Vogel");
        document.addCreator("Lars Vogel");
    }

    private static void addTitlePage(Document document) {
        try {
            Paragraph preface = new Paragraph();
            // We add one empty line
            addEmptyLine(preface, 1);
            // Lets write a big header
            preface.add(new Paragraph("Title of the document", catFont));

            addEmptyLine(preface, 1);
            // Will create: Report generated by: _name, _date
            preface.add(new Paragraph("Report generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                    smallBold));
            addEmptyLine(preface, 3);
            preface.add(new Paragraph("This document describes something which is very important ", smallBold));

            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 vogella.com ;-).",
                    redFont));

            document.add(preface);
            // Start a new page
            document.newPage();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    private static void addEmptyLine(Paragraph paragraph, int number) {
        for (int i = 0; i < number; i++) {
            paragraph.add(new Paragraph(" "));
        }
    }

    private static void addContent(Document document) {
    }
}