Tuesday, October 13, 2015

TABLE CREATION WITH COLSPAN AND ROWS SPAN IN PDF USING ITEXT


TABLE CREATION WITH COLSSPAN AND ROWS SPAN IN JAVA


import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.DottedLineSeparator;
import com.itextpdf.text.pdf.draw.LineSeparator;
import com.itextpdf.text.pdf.draw.VerticalPositionMark;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;


public class PdfGen {


    public static void main(String[] args) throws BadElementException, IOException, ParseException
    {
        //  text();
     
      String dt;
      dt=DateCreate("28-09-2015");
        System.out.println(dt);
     
    }
 
    public  static String DateCreate(String subdate) throws ParseException, BadElementException, IOException
    {
        String res;
           SimpleDateFormat fr=new SimpleDateFormat("dd-MM-yyyy");
        Date startdate=fr.parse(subdate);
        SimpleDateFormat di=new SimpleDateFormat("EEEE");
        String daych=di.format(startdate);
        if(daych.equals("Monday"))
        {
      SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy");
      Calendar cal  = Calendar.getInstance();
                      cal.setTime(startdate);
                        cal.add(Calendar.DATE, 6);
                     
                        String enddatestr = sdf.format(cal.getTime());
                        Date enddate=fr.parse(enddatestr);
                     
                      List<String> dt=  getDaysBetweenDates(startdate,enddate);
                     
            String dt1=Pdf(dt);
            res=dt1;
        }
        else
        {
           res="Check Date,It is not Monday";
        }
        return res;
    }
    public static List<String> getDaysBetweenDates(Date startdate, Date enddate)
{
 
    List<String> dates = new ArrayList<>();
    Calendar calendar = new GregorianCalendar();
    calendar.setTime(startdate);

    while (calendar.getTime().before(enddate))
    {
        Date result = calendar.getTime();
     
        SimpleDateFormat sdf=new SimpleDateFormat("dd-MM-yyyy");
        String dt=sdf.format(result);
        dates.add(dt);
        calendar.add(Calendar.DATE, 1);
    }
    return dates;
}
    public static String Pdf(List<String> subdate) throws FileNotFoundException, BadElementException, IOException
    {
     
                     
      Document document = new Document();
      try
      {
         String Greetline="                                                       PROJECT TRACK SHEET";
         String Empname1="Employee Name :_______________________";
         String EmpId=   "Employee id        :_______________________";
         String EmpTL=   "Team Leader      :_______________________";
          String Empband="Band                   :_______________________";
         String EmpPLT=  "Platform              :_______________________";
         PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:\\HelloWorld1.pdf"));
         document.open();
         Paragraph paragraph = new Paragraph();
           Chunk tab = new Chunk(new VerticalPositionMark(), 50f, false);
         // paragraph.setTabSettings(new TabSettings(tabList));
          Chunk CONNECT = new Chunk(
            new LineSeparator(0.5f, 95, BaseColor.BLUE, Element.ALIGN_CENTER, 3.5f));
        LineSeparator UNDERLINE =
            new LineSeparator(1, 100, null, Element.ALIGN_CENTER, -2);
        // creates tabs
        Chunk tab1 = new Chunk(new VerticalPositionMark(), 200, true);
        Chunk tab2 = new Chunk(new VerticalPositionMark(), 350, true);
          Chunk tab6 = new Chunk(new VerticalPositionMark(), 50f, false);
        Chunk tab3 = new Chunk(new DottedLineSeparator(), 450, true);
       Image image1 = Image.getInstance("imagevar.png");
          document.add(image1);
          document.add(new Paragraph(new Chunk(tab1)));
          document.add(new Paragraph(new Chunk(tab1)));
          document.add(new Paragraph(Greetline));
          document.add(new Paragraph(new Chunk(tab1)));
          document.add(new Paragraph(new Chunk(tab1)));
          document.add(new Paragraph(Empname1));
          document.add(new Paragraph(EmpId));
          document.add(new Paragraph(Empband));
          document.add(new Paragraph(EmpTL));
          document.add(new Paragraph(EmpPLT));
          Image image = Image.getInstance("D:\\images.jpg");
          image.setAbsolutePosition(390f, 500f);
          document.add(image);
          document.add(new Paragraph(new Chunk(tab1)));
          document.add(new Paragraph(new Chunk(tab1)));
           PdfPTable table = new PdfPTable(6); // 6 columns.
           table.setWidthPercentage(100);
           table.setWidths(new int[]{ 1, 1, 2, 1, 1,1});
            PdfPCell day = new PdfPCell(new Paragraph("  Day"));
            day.setRowspan(2);
            table.addCell(day);
            PdfPCell date = new PdfPCell(new Paragraph("  Date"));
            date.setRowspan(2);
            table.addCell(date);
            PdfPCell projass = new PdfPCell(new Paragraph("      Project Assigned"));
            projass.setRowspan(2);
            table.addCell(projass);
            PdfPCell workdone = new PdfPCell(new Paragraph("           Work Done"));
            workdone.setColspan(2);
            table.addCell(workdone);
            PdfPCell sign = new PdfPCell(new Paragraph("  Comments"));
            sign.setRowspan(2);
            table.addCell(sign);
            table.addCell("Fore noon");
            table.addCell("After noon");
            List<String> days= new ArrayList<>(
            Arrays.asList("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"));
            for(int i=0;i<days.size();i++)
            {
             
            table.addCell(" "+days.get(i));
            table.setSpacingBefore(10f);
            table.addCell(subdate.get(i));
            table.setSpacingBefore(10f);
            table.addCell(" ");
            table.setSpacingBefore(10f);
        table.addCell(" ");
         table.setSpacingBefore(10f);
         table.addCell(" ");
          table.setSpacingBefore(10f);
        table.addCell(" ");
            }
            document.add(new Paragraph(new Chunk(tab1)));
   
           document.add(table);
             document.add(new Paragraph(new Chunk(tab1)));
        document.add(new Paragraph(new Chunk(tab1)));
        document.add(new Paragraph(new Chunk(tab1)));
             document.add(new Paragraph("Total Working Days___________"));
             document.add(new Paragraph("Total Work in %     ___________"));
             document.add(new Paragraph(new Chunk(tab1)));
        document.add(new Paragraph(new Chunk(tab1)));
        document.add(new Paragraph(new Chunk(tab1)));
            document.add(new Paragraph("Employee Signature:                                                      HR Signature  :"));
            document.add(new Paragraph("                                                                                        Date          :"));
            //workdone.setColspan(2);
           //  table.setSpacingBefore(10f);
             //table.setSpacingBefore(10f);
            //float[] columnWidths = {2f, 1f, 1f};
         // table.setWidths(columnWidths);
         document.close();
     
         writer.close();
       
      }
      catch (DocumentException e)
      {
         e.printStackTrace();
       
      }
      return "success";
    }
 
}

No comments:

Post a Comment