com.thomasmore.service.PdfCreateServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.thomasmore.service.PdfCreateServiceImpl.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thomasmore.service;

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;
import com.thomasmore.model.Score;
import com.thomasmore.model.Student;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;
import javax.ejb.Stateless;

/**
 *
 * @author Jerre
 */
@Stateless
public class PdfCreateServiceImpl implements PdfCreateService {

    public void readPdf() {
        try {
            OutputStream file = new FileOutputStream(new File("D:\\SamplePDFje.pdf"));

            Document document = new Document();
            PdfWriter.getInstance(document, file);

            document.open();
            document.add(new Paragraph("First iText PDF"));
            document.add(new Paragraph(new Date().toString()));

            document.addAuthor("Krishna Sfsfsfsn");
            document.addCreationDate();
            document.addCreator("JavaBeat");
            document.addTitle("Sample PDF");

            //Create Paragraph
            Paragraph paragraph = new Paragraph("Title 1", new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD));

            //New line
            paragraph.add(new Paragraph(" "));
            paragraph.add("Test Paragraph");
            paragraph.add(new Paragraph(" "));
            document.add(paragraph);

            //Create a table in PDF
            PdfPTable pdfTable = new PdfPTable(3);
            PdfPCell cell1 = new PdfPCell(new Phrase("Table Header 1"));
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            pdfTable.addCell(cell1);

            cell1 = new PdfPCell(new Phrase("Table Header 2"));
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            pdfTable.addCell(cell1);

            cell1 = new PdfPCell(new Phrase("Table Header 3"));
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            pdfTable.addCell(cell1);
            pdfTable.setHeaderRows(1);

            pdfTable.addCell("Row 1 Col 1");
            pdfTable.addCell("Row 1 Col 2");
            pdfTable.addCell("Row 1 Col 3");

            pdfTable.addCell("Row 2 Col 1");
            pdfTable.addCell("Row 2 Col 2");
            pdfTable.addCell("Row 2 Col 3");

            document.add(pdfTable);

            document.close();
            file.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
    }

}