private static void AddRoleHeader(MigraDoc.DocumentObjectModel.Tables.Table table, string headingType)
 {
     Font font = new Font("Arial", 8);
     MigraDoc.DocumentObjectModel.Tables.Row rowHeader = table.AddRow();
     rowHeader.HeadingFormat = true;
     rowHeader.Format.Font.ApplyFont(font);
     rowHeader.Shading.Color = Colors.PaleGoldenrod;
     rowHeader.Format.Font.Bold = true;
     Cell cellHeader = rowHeader.Cells[0];
     cellHeader.AddParagraph(headingType);
     cellHeader.MergeRight = 5;
 }
        private static void AddHomegroupData(Document document, List<PersonViewModel> personList, MigraDoc.DocumentObjectModel.Tables.Table table)
        {
            var style = document.Styles["Normal"];
            style.Font.Size = 8.0;
            TextMeasurement tm = new TextMeasurement(style.Font.Clone());
            int familyId = 0;
            var roleName = string.Empty;
            foreach (PersonViewModel person in personList)
            {
                if (roleName != person.RoleName)
                {
                    roleName = person.RoleName;
                    AddRoleHeader(table, roleName);
                }

                Row row = table.AddRow();
                Cell cell = row.Cells[0];
                if (familyId != person.FamilyId)
                {
                    cell.AddParagraph(AdjustIfTooWideToFitIn(cell, person.Surname, tm));
                }
                cell = row.Cells[1];
                cell.AddParagraph(AdjustIfTooWideToFitIn(cell, person.Firstname, tm));
                cell = row.Cells[2];
                if (familyId != person.FamilyId)
                {
                    cell.AddParagraph(AdjustIfTooWideToFitIn(cell, person.HomePhone ?? string.Empty, tm));
                }
                cell = row.Cells[3];
                cell.AddParagraph(AdjustIfTooWideToFitIn(cell, person.WorkPhone ?? string.Empty, tm));
                cell = row.Cells[4];
                cell.AddParagraph(AdjustIfTooWideToFitIn(cell, person.CellPhone ?? string.Empty, tm));
                cell = row.Cells[5];
                cell.AddParagraph(AdjustIfTooWideToFitIn(cell, person.Email ?? string.Empty, tm));
                familyId = person.FamilyId;
            }
        }
        private static int AddAttendanceHeaders(int month1, int month2, List<int> month1Events, List<int> month2Events, MigraDoc.DocumentObjectModel.Tables.Table table, string titleMessage)
        {
            //Headers
            // Create a font
            Font font = new Font("Arial", 8);
            int totalDays = month1Events.Count + month2Events.Count;
            table.Borders.Width = 0.25;

            table.AddColumn(Unit.FromCentimeter(3));  //Firstname
            table.AddColumn(Unit.FromMillimeter(28)); //Surname
            foreach (int day in month1Events)
            {
                table.AddColumn(Unit.FromMillimeter(56/totalDays)); //Weeks in month 1
            }
            foreach (int day in month2Events)
            {
                table.AddColumn(Unit.FromMillimeter(56/totalDays)); //Weeks in month 2
            }
            table.AddColumn(Unit.FromCentimeter(2)); //Member/Visitor
            table.AddColumn(Unit.FromCentimeter(5)); //Comments

            MigraDoc.DocumentObjectModel.Tables.Row row = table.AddRow();
            row.HeadingFormat = true;
            row.Format.Font.ApplyFont(font);
            row.Format.Alignment = ParagraphAlignment.Center;
            row.Shading.Color = Colors.PaleGoldenrod;
            row.Format.Font.Bold = true;
            Cell cell = row.Cells[0];
            cell.AddParagraph(titleMessage);
            cell.MergeRight = 1;
            cell = row.Cells[2];
            cell.AddParagraph(Months[month1-1]);
            if (month1Events.Count > 1)
            {
                cell.MergeRight = month1Events.Count-1;
            }
            cell = row.Cells[2+month1Events.Count];
            cell.AddParagraph(Months[month2-1]);
            if (month2Events.Count > 1)
            {
                cell.MergeRight = month2Events.Count - 1;
            }
            cell = row.Cells[2+month1Events.Count+month2Events.Count];
            cell.AddParagraph(" ");

            //Add the 2nd row
            row = table.AddRow();
            row.HeadingFormat = true;
            row.Format.Font.ApplyFont(font);
            row.Shading.Color = Colors.PaleGoldenrod;
            cell = row.Cells[0];
            cell.AddParagraph("Surname");
            cell = row.Cells[1];
            cell.AddParagraph("Firstname");
            int colCount = 2;
            foreach (int day in month1Events)
            {
                cell = row.Cells[colCount];
                cell.AddParagraph(day.ToString());
                cell.Format.Alignment = ParagraphAlignment.Center;
                colCount++;
            }
            foreach (int day in month2Events)
            {
                cell = row.Cells[colCount];
                cell.AddParagraph(day.ToString());
                cell.Format.Alignment = ParagraphAlignment.Center;
                colCount++;
            }
            cell = row.Cells[colCount++];
            cell.AddParagraph("Role");
            cell = row.Cells[colCount];
            cell.AddParagraph("Comments");

            return totalDays + 3;
        }
        private static void AddHeaders(MigraDoc.DocumentObjectModel.Tables.Table table, string optionalHeader)
        {
            //Headers
            // Create a font
            Font font = new Font("Arial", 8);

            table.Borders.Width = 0.25;

            table.AddColumn(Unit.FromCentimeter(3));
            table.AddColumn(Unit.FromMillimeter(28));
            table.AddColumn(Unit.FromMillimeter(28));
            table.AddColumn(Unit.FromMillimeter(28));
            table.AddColumn(Unit.FromMillimeter(28));
            table.AddColumn(Unit.FromCentimeter(5));

            if(optionalHeader != null)
                AddRoleHeader(table, optionalHeader);

            MigraDoc.DocumentObjectModel.Tables.Row row = table.AddRow();
            row.HeadingFormat = true;
            row.Format.Font.ApplyFont(font);
            row.Format.Font.Bold = true;
            row.Shading.Color = Colors.PaleGoldenrod;
            Cell cell = row.Cells[0];
            cell.AddParagraph("Surname");
            cell = row.Cells[1];
            cell.AddParagraph("Firstname");
            cell = row.Cells[2];
            cell.AddParagraph("Home");
            cell = row.Cells[3];
            cell.AddParagraph("Work");
            cell = row.Cells[4];
            cell.AddParagraph("Cell");
            cell = row.Cells[5];
            cell.AddParagraph("Email");
        }
        private static void AddAttendanceData(List<AttendanceEventViewModel> membersList, 
            Document document, 
            List<int> month1Events, 
            List<int> month2Events, 
            int month1, 
            int month2, 
            MigraDoc.DocumentObjectModel.Tables.Table table, 
            int totalColumns,
            Dictionary<int, string> comments)
        {
            var style = document.Styles["Normal"];
            style.Font.Size = 8.0;
            TextMeasurement tm = new TextMeasurement(style.Font.Clone());
            int familyId = 0;
            int personId = 0;
            //Sort the List
            membersList.Sort(delegate(AttendanceEventViewModel e1, AttendanceEventViewModel e2)
            {
                int familyIdComp = e1.FamilyId.CompareTo(e2.FamilyId);
                if (familyIdComp == 0)
                {
                    int personIdComp = e1.PersonId.CompareTo(e2.PersonId);
                    if (personIdComp == 0)
                    {
                        return e1.Date.CompareTo(e2.Date);
                    }
                    else
                    {
                        return personIdComp;
                    }
                }
                return familyIdComp;
            });

            if (membersList.Count == 0)
            {
                Row emptyRow = table.AddRow();
                Cell emptyCell = emptyRow.Cells[0];
                emptyCell.AddParagraph("No attendance has been captured over the last two months");
                emptyCell.MergeRight = 4;
            }
            else
            {
                Row row = null;
                foreach (AttendanceEventViewModel attendanceEvent in membersList)
                {
                    Cell cell;
                    if (personId != attendanceEvent.PersonId)
                    {
                        row = table.AddRow();
                        cell = row.Cells[0];
                        if (familyId != attendanceEvent.FamilyId)
                        {
                            cell.AddParagraph(AdjustIfTooWideToFitIn(cell, attendanceEvent.Surname, tm));
                        }
                        cell = row.Cells[1];
                        cell.AddParagraph(AdjustIfTooWideToFitIn(cell, attendanceEvent.Firstname, tm));
                        cell = row.Cells[totalColumns - 1];
                        cell.AddParagraph(AdjustIfTooWideToFitIn(cell, attendanceEvent.Role, tm));
                        var comment = (from c in comments where c.Key==attendanceEvent.PersonId select c.Value).FirstOrDefault();
                        if (!string.IsNullOrEmpty(comment))
                        {
                        cell = row.Cells[totalColumns];
                        cell.AddParagraph(AdjustIfTooWideToFitIn(cell, comment, tm));
                        }
                    }

                    cell = row.Cells[GetEventColumn(attendanceEvent.Date, month1, month2, month1Events, month2Events)];
                    if (attendanceEvent.Attended)
                    {
                        cell.AddParagraph("x");
                        cell.Format.Alignment = ParagraphAlignment.Center;
                    }
                    else
                    {
                        cell.AddParagraph(" ");
                        cell.Format.Alignment = ParagraphAlignment.Center;
                    }

                    familyId = attendanceEvent.FamilyId;
                    personId = attendanceEvent.PersonId;
                }
            }
        }