package com.javahonk;

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

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.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class GeneratePDF {


	private static Font TIME_ROMAN = 
			new Font(Font.FontFamily.TIMES_ROMAN, 18,Font.BOLD);
	private static Font TIME_ROMAN_SMALL = 
			new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.BOLD);


	/**
	 * @param args
	 */
	public static Document createPDF(String file) {
		
	Document document = null;

	try {
		document = new Document();
		PdfWriter.getInstance(document, new FileOutputStream(file));
		document.open();

		addMetaData(document);

		addTitlePage(document);
		
		createTable(document);
		
		document.close();

	} catch (FileNotFoundException | DocumentException e) {
		
		e.printStackTrace();
	}
	return document;

	}

	private static void addMetaData(Document document) {
		document.addTitle("Generate PDF report");
		document.addSubject("Generate PDF report");
		document.addAuthor("Java Honk");
		document.addCreator("Java Honk");
	}

	private static void addTitlePage(Document document)
			throws DocumentException {
		
		Paragraph preface = new Paragraph();
		creteEmptyLine(preface, 1);
		preface.add(new Paragraph("PDF Report", TIME_ROMAN));

		creteEmptyLine(preface, 1);
		SimpleDateFormat simpleDateFormat = 
				new SimpleDateFormat("MM/dd/yyyy");
		preface.add(new Paragraph(
				"Report created on "+ simpleDateFormat
				.format(new Date()),TIME_ROMAN_SMALL));
		document.add(preface);
		
		
	}

	private static void creteEmptyLine(Paragraph paragraph, 
			int number) {
		for (int i = 0; i < number; i++) {
			paragraph.add(new Paragraph(" "));
		}
	}
	
	private static void createTable(Document document) 
			throws DocumentException {		
	Paragraph paragraph = new Paragraph();
	creteEmptyLine(paragraph, 2);
	document.add(paragraph);
	PdfPTable table = new PdfPTable(3);		

	PdfPCell c1 = new PdfPCell(new Phrase("First Name"));
	c1.setHorizontalAlignment(Element.ALIGN_CENTER);
	table.addCell(c1);

	c1 = new PdfPCell(new Phrase("Last Name"));
	c1.setHorizontalAlignment(Element.ALIGN_CENTER);
	table.addCell(c1);

	c1 = new PdfPCell(new Phrase("Test"));
	c1.setHorizontalAlignment(Element.ALIGN_CENTER);
	table.addCell(c1);
	table.setHeaderRows(1);
	
	for (int i = 0; i < 5; i++) {
		table.setWidthPercentage(100);
		table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
		table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.addCell("Java");
		table.addCell("Honk");
		table.addCell("Success");
	}		

	document.add(table);
	}


}
