com.pearson.controller.PdfGentrate.java Source code

Java tutorial

Introduction

Here is the source code for com.pearson.controller.PdfGentrate.java

Source

package com.pearson.controller;

import java.io.FileOutputStream;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class PdfGentrate {

    public static void main(String[] args) {

        try {
            /*Document document = new Document(PageSize.
            A4, 50, 50, 50, 50);
            PdfWriter writer = PdfWriter.getInstance
            (document, new FileOutputStream("C:\\my.pdf"));
            document.open();               
            // create a chunk object using chunk class  of itext library.
            Chunk underlined = new Chunk("Welcome to Pearson Q-Service Portal : ");   
            // set the distance between text and line.
            underlined.setTextRise(8.0f);      
            // set the width of the line, 'y' position,  color and design of the line
            underlined.setUnderline(new Color(0x00, 0x00, 0xFF),0.0f, 0.2f, 3.0f, 0.0f, PdfContentByte.LINE_CAP_PROJECTING_SQUARE);      // finally add object to the document.
            document.add(underlined);
            document.add(new Paragraph("Hi username",
            FontFactory.getFont(FontFactory.COURIER, 14, 
            Font.BOLD, new Color(255, 150, 200))));;
                 
                
                 document.add(new Paragraph("Tiltle",catFont));
                 document.add(new Paragraph("Report generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                   smallBold));
                 document.add(new Paragraph("This document is a preliminary version and not subject to your license agreement or any other agreement with vogella.com ;-).",
                   redFont));
                     
                 PdfPTable table = new PdfPTable(3); // 3 columns.   
                table.setWidthPercentage(100); //Width 100%     
                table.setSpacingBefore(10f); //Space before table    
                 table.setSpacingAfter(10f); //Space after table         //Set Column widths     
                float[] columnWidths = {1f, 1f, 1f};     
                table.setWidths(columnWidths);    
                PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));     
                cell1.setBorderColor(BaseColor.BLUE);     
                cell1.setPaddingLeft(10);     
                cell1.setHorizontalAlignment(Element.ALIGN_CENTER);     
                cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);    
                PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));    
              cell2.setBorderColor(BaseColor.GREEN);    
              cell2.setPaddingLeft(10);    
              cell2.setHorizontalAlignment(Element.ALIGN_CENTER);      
                cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);      
                PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));   
                cell3.setBorderColor(BaseColor.RED);      
                cell3.setPaddingLeft(10);      
                cell3.setHorizontalAlignment(Element.ALIGN_CENTER);    
              cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);   
                //To avoid having the cell border and the content overlap, if you are having thick cell borders     
                //cell1.setUserBorderPadding(true);     
                //cell2.setUserBorderPadding(true);   
                //cell3.setUserBorderPadding(true);   
                table.addCell(cell1);   
              table.addCell(cell2);   
              table.addCell(cell3);     
                 document.add(table);     
                 document.close();     
                 writer.close();
                    
                  
                
            document.close();
            } 
            catch (Exception e2) {
            System.out.println(e2.getMessage());
            }
            }*/

            Document document = new Document();

            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("AddTableExample.pdf"));
            document.open();
            PdfPTable table = new PdfPTable(3); // 3 columns.      
            table.setWidthPercentage(100); //Width 100%       
            table.setSpacingBefore(10f); //Space before table    
            table.setSpacingAfter(10f); //Space after table         //Set Column widths     
            float[] columnWidths = { 1f, 1f, 1f };
            table.setWidths(columnWidths);
            PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
            cell1.setBorderColor(BaseColor.BLUE);
            cell1.setPaddingLeft(10);
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
            PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
            cell2.setBorderColor(BaseColor.GREEN);
            cell2.setPaddingLeft(10);
            cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
            PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));
            cell3.setBorderColor(BaseColor.RED);
            cell3.setPaddingLeft(10);
            cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
            cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
            //To avoid having the cell border and the content overlap, if you are having thick cell borders        //cell1.setUserBorderPadding(true);        //cell2.setUserBorderPadding(true);   
            //cell3.setUserBorderPadding(true);    
            table.addCell(cell1);
            table.addCell(cell2);
            table.addCell(cell3);
            document.add(table);
            document.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}