private void addhaederfooter()
        {
            string taskid = HttpContext.Current.Session["Name"].ToString();

            HttpContext.Current.Session.Timeout = 60;
            string inputfile  = System.Web.HttpContext.Current.Server.MapPath("~/storepdf/" + taskid + ".pdf");
            string outputfile = System.Web.HttpContext.Current.Server.MapPath("~/editorpdf/" + taskid + ".pdf");
            //string pictPath = "~/img/caslab.jpg";
            DocumentCore dc     = DocumentCore.Load(inputfile);
            HeaderFooter header = new HeaderFooter(dc, HeaderFooterType.HeaderDefault);

            SautinSoft.Document.Paragraph par1 = new SautinSoft.Document.Paragraph(dc);
            par1.ParagraphFormat.Alignment = HorizontalAlignment.Left;
            //header.Content.Start.Insert("CASLAB", new CharacterFormat() { Size = 15.0, FontColor = Color.Blue });
            CharacterFormat cf1 = new CharacterFormat()
            {
                FontName = "Consolas", Size = 15.0, FontColor = Color.Blue
            };

            par1.Content.Start.Insert("CASLAB", cf1.Clone());
            header.Blocks.Add(par1);
            SautinSoft.Document.Paragraph par2 = new SautinSoft.Document.Paragraph(dc);
            par2.ParagraphFormat.Alignment = HorizontalAlignment.Right;
            //header.Content.Start.Insert("CASLAB", new CharacterFormat() { Size = 15.0, FontColor = Color.Blue });
            CharacterFormat cf2 = new CharacterFormat()
            {
                FontName = "Consolas", Size = 15.0
            };

            par2.Content.Start.Insert(taskid, cf2.Clone());
            header.Blocks.Add(par2);
            SautinSoft.Document.Paragraph par = new SautinSoft.Document.Paragraph(dc);
            par.ParagraphFormat.Alignment = HorizontalAlignment.Right;
            CharacterFormat cf = new CharacterFormat()
            {
                FontName = "Consolas", Size = 12.0
            };

            par.Content.Start.Insert("Page", cf.Clone());
            Field fPage = new Field(dc, FieldType.Page);

            fPage.CharacterFormat = cf.Clone();
            par.Content.End.Insert(fPage.Content);
            par.Content.End.Insert(" of ", cf.Clone());
            Field fPages = new Field(dc, FieldType.NumPages);

            fPages.CharacterFormat = cf.Clone();

            //header.Content.Start.Insert(taskid, new CharacterFormat() { Size = 15.0, FontColor = Color.Black });
            par.Content.End.Insert(fPages.Content);

            header.Blocks.Add(par);
            SautinSoft.Document.Paragraph par3 = new SautinSoft.Document.Paragraph(dc);
            par3.ParagraphFormat.Alignment = HorizontalAlignment.Center;
            //header.Content.Start.Insert("CASLAB", new CharacterFormat() { Size = 15.0, FontColor = Color.Blue });
            CharacterFormat cf3 = new CharacterFormat();

            par3.Content.Start.Insert("______________________________________________________________________________________________", cf3.Clone());
            header.Blocks.Add(par3);
            //Picture pict1 = new Picture(dc, InlineLayout.Inline(new Size(100, 100)), pictPath);

            foreach (SautinSoft.Document.Section s in dc.Sections)
            {
                s.HeadersFooters.Add(header.Clone(true));
            }
            HeaderFooter footer = new HeaderFooter(dc, HeaderFooterType.FooterDefault);

            SautinSoft.Document.Paragraph par4 = new SautinSoft.Document.Paragraph(dc);
            par4.ParagraphFormat.Alignment = HorizontalAlignment.Center;
            //header.Content.Start.Insert("CASLAB", new CharacterFormat() { Size = 15.0, FontColor = Color.Blue });
            CharacterFormat cf4 = new CharacterFormat();

            par4.Content.Start.Insert("______________________________________________________________________________________________", cf4.Clone());
            footer.Blocks.Add(par4);
            par4.Content.End.Insert("This document and all the information contained here are confidential and exclusive  property of CASLAB and maynot be reproduced,disclosed,or made public in any manner prior to express written authorization by CASLAB.", new CharacterFormat()
            {
                Size = 12.0, FontColor = Color.Black
            });
            foreach (SautinSoft.Document.Section s in dc.Sections)
            {
                s.HeadersFooters.Add(footer.Clone(true));
            }
            dc.Save(outputfile);
        }
        public void GenerateWordDocument(string path, Project project, string imagePath, string docTempPath)
        {
            var id = project.id;

            var execSumarry          = _projectService.GetExecSummary(id);
            var company              = _insendluEntities.Companies.Single();
            var dpwProperty          = _projectService.RiskAnalysis(id);
            var methodology          = _projectService.GetProjMethodolgy(id);
            var cosPlan              = _projectService.GetCostPlan(id);
            var coverPage            = _projectService.GetProjectCoverPage(id);
            var jvCompany            = _projectService.GetProjectJvCompany(id);
            var policyOverview       = _projectService.GetProjectPolicy(id);
            var scope                = _projectService.GetProjectScopeOfWork(id);
            var implementation       = _projectService.GetProjectImplementationTime(id);
            var team                 = _projectService.GetProjectTeam(id);
            var refernces            = _projectService.GetProjectReference(id);
            var beeStatus            = _projectService.GetProjectBeeStatus(id);
            var whyChoose            = _projectService.GetProjectWhyChooseBizStandard();
            var confidencialityState = _insendluEntities.ConfidentialityStatements.Single();
            var coverPageStandard    = _projectService.GetCoverpageStandard();
            var confidence           = confidencialityState.statement;

            var executive = execSumarry != null ? execSumarry.content : String.Empty;
            var companyMssionStatement = company != null ? company.mission_statement : String.Empty;
            var companyBackGround      = company != null ? company.background : String.Empty;
            var companyService         = company != null ? company.service_offering : String.Empty;
            var headOffice             = company != null ? company.headoffice : String.Empty;

            var dpw = dpwProperty != null ? dpwProperty.risk_analysis : String.Empty;
            var methodologyContent = methodology != null ? methodology.content : String.Empty;
            var planCost           = cosPlan != null ? cosPlan.deliverable : String.Empty;
            //Coverpage objects *************************************************************
            var preparedFor = coverPage != null ? coverPage.prepared_for : String.Empty;
            var reasonprop  = coverPage != null ? coverPage.reason_for_proposal : String.Empty;
            var preparedBy  = string.Empty;
            var address     = string.Empty;

            preparedFor = preparedFor ?? String.Empty;

            if (coverPageStandard != null)
            {
                preparedBy = coverPageStandard.prepared_by;
                address    = coverPageStandard.address;
            }
            //*******************************************************************************
            var jvCompanyContent = jvCompany != null ? jvCompany.content : String.Empty;
            var overview         = policyOverview != null ? policyOverview.content : String.Empty;
            var aim          = scope != null ? scope.aim : String.Empty;
            var purpose      = scope != null ? scope.purpose : String.Empty;
            var deliverables = scope != null ? scope.deliverables : String.Empty;
            var implement    = implementation != null ? implementation.content : String.Empty;
            var teamCheck    = team != null ? team.content : String.Empty;
            //var refree = refernces != null ? refernces.content : String.Empty;
            var bee        = beeStatus != null ? beeStatus.content : String.Empty;
            var why        = whyChoose != null ? whyChoose.heading : String.Empty;
            var whyBullets = whyChoose != null ? whyChoose.bullet : String.Empty;

            var compServiceList = companyService.Split(',');
            var officeHead      = headOffice.Split(',');
            var addressList     = address.Split(',');

            var cvsList = new List <CV>();
            var refList = new List <Reference>();

            if (refernces != null)
            {
                var cvTemp  = refernces.cvs.Split(',');
                var refTemp = refernces.references.Split(',');

                for (var i = 0; i < cvTemp.Length; i++)
                {
                    if (!string.IsNullOrWhiteSpace(cvTemp[i]))
                    {
                        cvsList.Add(GetCv(Convert.ToInt32(cvTemp[i])));
                    }
                }
                for (var i = 0; i < refTemp.Length; i++)
                {
                    if (!string.IsNullOrWhiteSpace(refTemp[i]))
                    {
                        refList.Add(GetReference(Convert.ToInt32(refTemp[i])));
                    }
                }
            }
            var templatePath = docTempPath;
            var documentName = project.name;
            var tablePath    = path + documentName + "_table.docx";
            var docxPath     = documentName + ".docx";
            var resultsPath  = Path.GetDirectoryName(templatePath) + @"\" + documentName.ToUpper() + @"\" + docxPath;

            // Let's create a simple DOCX document.
            var docx = new DocumentCore();
            var dc   = DocumentCore.Load(templatePath);

            #region Project Reference
            var section2 = new Section(docx);
            section2.PageSetup.PaperType   = PaperType.A4;
            section2.PageSetup.Orientation = Orientation.Portrait;
            section2.PageSetup.PageWidth   = LengthUnitConverter.Convert(8.5, LengthUnit.Inch, LengthUnit.Point);
            section2.PageSetup.PageHeight  = LengthUnitConverter.Convert(11.0, LengthUnit.Inch, LengthUnit.Point);

            section2.PageSetup.PageMargins = new PageMargins()
            {
                Top    = LengthUnitConverter.Convert(10, LengthUnit.Millimeter, LengthUnit.Point),
                Right  = LengthUnitConverter.Convert(20, LengthUnit.Millimeter, LengthUnit.Point),
                Bottom = LengthUnitConverter.Convert(10, LengthUnit.Millimeter, LengthUnit.Point),
                Left   = LengthUnitConverter.Convert(20, LengthUnit.Millimeter, LengthUnit.Point)
            };

            docx.Sections.Add(section2);

            section2.Blocks.Add(new Paragraph(docx, "RELEVANT PROJECT REFERENCES"));
            var heading = new Table(docx);
            var twid2   = LengthUnitConverter.Convert(200, LengthUnit.Millimeter, LengthUnit.Point);
            heading.TableFormat.PreferredWidth = new TableWidth(twid2, TableWidthUnit.Point);
            heading.TableFormat.Alignment      = HorizontalAlignment.Center;

            string [] colectin = new string[] { " Client", " Project Details", " Date Started", " Project Value", " Contacts" };
            var       countz   = 0;
            for (int i = 0; i < 1; i++)
            {
                TableRow row = new TableRow(docx);

                for (int j = 0; j < colectin.Length; j++)
                {
                    TableCell cell = new TableCell(docx);


                    cell.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Single, Color.Cyan, 1.0);

                    cell.CellFormat.BackgroundColor = new Color(0xCCCCCC);

                    row.Cells.Add(cell);


                    Paragraph p = new Paragraph(docx);
                    p.ParagraphFormat.Alignment   = HorizontalAlignment.Center;
                    p.ParagraphFormat.SpaceBefore = LengthUnitConverter.Convert(3, LengthUnit.Millimeter, LengthUnit.Point);
                    p.ParagraphFormat.SpaceAfter  = LengthUnitConverter.Convert(3, LengthUnit.Millimeter, LengthUnit.Point);

                    p.Content.End.Insert(colectin[j], new CharacterFormat()
                    {
                        FontName = "Cambria", FontColor = Color.Black, Size = 11.0, Bold = true
                    });
                    cell.Blocks.Add(p);
                    countz++;
                }
                heading.Rows.Add(row);
            }
            section2.Blocks.Add(heading);
            var t2 = new Table(docx);

            var twid = LengthUnitConverter.Convert(200, LengthUnit.Millimeter, LengthUnit.Point);
            t2.TableFormat.PreferredWidth = new TableWidth(twid, TableWidthUnit.Point);
            t2.TableFormat.Alignment      = HorizontalAlignment.Center;

            var counter2 = 0;

            for (int r = 0; r < refList.Count; r++)
            {
                TableRow row = new TableRow(docx);

                for (int c = 0; c < 5; c++)
                {
                    TableCell cell = new TableCell(docx);
                    cell.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Single, Color.Cyan, 1.0);

                    if (counter2 % 2 == 1)
                    {
                        cell.CellFormat.BackgroundColor = new Color(0xCCCCCC);
                    }

                    row.Cells.Add(cell);

                    Paragraph p = new Paragraph(docx);
                    p.ParagraphFormat.Alignment   = HorizontalAlignment.Justify;
                    p.ParagraphFormat.SpaceBefore = LengthUnitConverter.Convert(5, LengthUnit.Millimeter, LengthUnit.Point);
                    p.ParagraphFormat.SpaceAfter  = LengthUnitConverter.Convert(5, LengthUnit.Millimeter, LengthUnit.Point);

                    if (c == 0)
                    {
                        p.Content.Start.Insert(refList[r].client_name, new CharacterFormat
                        {
                            FontName  = "Cambria",
                            FontColor = Color.Black,
                            Size      = 11.0,
                            Bold      = true
                        });
                    }
                    else if (c == 1)
                    {
                        p.Content.Start.Insert(refList[r].project_details, new CharacterFormat
                        {
                            FontName  = "Cambria",
                            FontColor = Color.Black,
                            Size      = 11.0
                        });
                    }
                    else if (c == 2)
                    {
                        p.Content.Start.Insert(refList[r].date_undertaken, new CharacterFormat
                        {
                            FontName  = "Cambria",
                            FontColor = Color.Black,
                            Size      = 11.0
                        });
                    }
                    else if (c == 3)
                    {
                        p.Content.Start.Insert(refList[r].project_value, new CharacterFormat
                        {
                            FontName  = "Cambria",
                            FontColor = Color.Black,
                            Size      = 11.0
                        });
                    }
                    else
                    {
                        var contactL    = refList[r].contact_id;
                        var contactList = new string[] {};

                        if (contactL != null)
                        {
                            contactList = refList[r].contact_id.Split(',');
                        }

                        foreach (var contact in contactList)
                        {
                            p.Content.End.Insert("- " + RemoveSpecialCharacters(contact), new CharacterFormat
                            {
                                FontName  = "Cambria",
                                FontColor = Color.Black,
                                Size      = 11.0
                            });
                        }
                    }

                    cell.Blocks.Add(p);

                    counter2++;
                }
                t2.Rows.Add(row);
            }
            section2.Blocks.Add(t2);
            #endregion
            #region CV
            var section4 = new Section(docx);
            section4.PageSetup.PaperType   = PaperType.A4;
            section4.PageSetup.Orientation = Orientation.Portrait;
            section4.PageSetup.PageWidth   = LengthUnitConverter.Convert(8.5, LengthUnit.Inch, LengthUnit.Point);
            section4.PageSetup.PageHeight  = LengthUnitConverter.Convert(11.0, LengthUnit.Inch, LengthUnit.Point);

            section4.PageSetup.PageMargins = new PageMargins
            {
                Top    = LengthUnitConverter.Convert(10, LengthUnit.Millimeter, LengthUnit.Point),
                Right  = LengthUnitConverter.Convert(20, LengthUnit.Millimeter, LengthUnit.Point),
                Bottom = LengthUnitConverter.Convert(10, LengthUnit.Millimeter, LengthUnit.Point),
                Left   = LengthUnitConverter.Convert(20, LengthUnit.Millimeter, LengthUnit.Point)
            };

            docx.Sections.Add(section4);
            section4.Blocks.Add(new Paragraph(docx, "PROJECT TEAM AND RESEARCH TEAM MEMBERS"));

            var researchMemberHead = new Table(docx);
            var res = LengthUnitConverter.Convert(200, LengthUnit.Millimeter, LengthUnit.Point);
            researchMemberHead.TableFormat.PreferredWidth = new TableWidth(res, TableWidthUnit.Point);
            researchMemberHead.TableFormat.Alignment      = HorizontalAlignment.Center;

            string [] researchCollection = new string[] { "NAME AND CREDENTIALS", "POSITION", "RESPONSIBILITIES" };
            countz = 0;
            for (int i = 0; i < 1; i++)
            {
                TableRow row = new TableRow(docx);

                for (int j = 0; j < researchCollection.Length; j++)
                {
                    TableCell cell = new TableCell(docx);


                    cell.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Single, Color.Black, 1.0);

                    row.Cells.Add(cell);


                    Paragraph p = new Paragraph(docx);
                    p.ParagraphFormat.Alignment   = HorizontalAlignment.Center;
                    p.ParagraphFormat.SpaceBefore = LengthUnitConverter.Convert(5, LengthUnit.Millimeter, LengthUnit.Point);
                    p.ParagraphFormat.SpaceAfter  = LengthUnitConverter.Convert(5, LengthUnit.Millimeter, LengthUnit.Point);

                    p.Content.End.Insert(researchCollection[j], new CharacterFormat()
                    {
                        FontName = "Cambria", FontColor = Color.Black, Size = 11.0, Bold = true
                    });
                    cell.Blocks.Add(p);
                    countz++;
                }
                researchMemberHead.Rows.Add(row);
            }
            section4.Blocks.Add(researchMemberHead);

            var tTable = new Table(docx);

            var tw = LengthUnitConverter.Convert(200, LengthUnit.Millimeter, LengthUnit.Point);
            tTable.TableFormat.PreferredWidth = new TableWidth(tw, TableWidthUnit.Point);
            tTable.TableFormat.Alignment      = HorizontalAlignment.Center;

            countz = 0;

            for (int r = 0; r < cvsList.Count; r++)
            {
                TableRow row = new TableRow(docx);

                for (int c = 0; c < 3; c++)
                {
                    TableCell cell = new TableCell(docx);

                    cell.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Single, Color.Black, 1.0);

                    row.Cells.Add(cell);


                    Paragraph p = new Paragraph(docx);
                    p.ParagraphFormat.Alignment   = HorizontalAlignment.Justify;
                    p.ParagraphFormat.SpaceBefore = LengthUnitConverter.Convert(5, LengthUnit.Millimeter, LengthUnit.Point);
                    p.ParagraphFormat.SpaceAfter  = LengthUnitConverter.Convert(5, LengthUnit.Millimeter, LengthUnit.Point);

                    if (c == 0)
                    {
                        p.Content.Start.Insert(cvsList[r].credentials, new CharacterFormat
                        {
                            FontName  = "Cambria",
                            FontColor = Color.Black,
                            Size      = 11.0,
                            Bold      = false
                        });

                        p.Content.Start.Insert(cvsList[r].name + cvsList[r].surname + " ( " + cvsList[r].qualification + " )", new CharacterFormat
                        {
                            FontName  = "Cambria",
                            FontColor = Color.Black,
                            Size      = 11.0,
                            Bold      = true
                        });
                    }
                    else if (c == 1)
                    {
                        p.Content.Start.Insert(cvsList[r].position, new CharacterFormat
                        {
                            FontName  = "Cambria",
                            FontColor = Color.Black,
                            Size      = 11.0
                        });
                    }
                    else
                    {
                        var responsibity = cvsList[r].responsibilities.Split('#');

                        foreach (var response in responsibity)
                        {
                            p.Content.End.Insert("- " + response, new CharacterFormat
                            {
                                FontName  = "Cambria",
                                FontColor = Color.Black,
                                Size      = 11.0
                            });
                        }
                    }
                    cell.Blocks.Add(p);

                    countz++;
                }
                tTable.Rows.Add(row);
            }
            section4.Blocks.Add(tTable);
            #endregion
            #region DataSource

            var datasource = new []
            {
                new {
                    CONFIDENTIALITY   = RemoveSpecialCharacters(confidence),
                    EXECUTIVESUMMARY  = executive,
                    COMPANYBACKGROUND = companyBackGround,
                    PURPOSE           = purpose,
                    DELIVERABLES      = deliverables,
                    WHYCHOOSE         = RemoveSpecialCharacters(why),
                    WHYCHOOSEBULLET   = RemoveSpecialCharacters(whyBullets),
                    BBBEE             = bee
                }
            };
            #endregion

            // Cover page Section
            //*************************************************

            // 4. Why Choose Insedlu Business Companion

            // 5. Black Economic Empowerment
            // 6. Project Risk Analysis
            // 7. Isedlu Resourse Capabilities
            // 8. Conclusion

            dc.MailMerge.Execute(datasource);

            // Save DOCX to a file
            dc.Save(resultsPath);
            docx.Save(tablePath);
        }