private static bool TooWide(string word, Unit width, TextMeasurement tm)
 {
     float f = tm.MeasureString(word, UnitType.Point).Width;
     return f > width.Point;
 }
        private static string MakeFit(string word, Unit width, TextMeasurement tm)
        {
            var adjustedWord = new StringBuilder();
            var current = string.Empty;
            foreach (char c in word)
            {
                if (TooWide(current + c, width, tm))
                {
                    adjustedWord.Append(current);
                    adjustedWord.Append(Chars.CR);
                    current = c.ToString();
                }
                else
                {
                    current += c;
                }
            }
            adjustedWord.Append(current);

            return adjustedWord.ToString();
        }
        private static MigraDoc.Rendering.PdfDocumentRenderer CreatePeopleListDocument(Person currentUser, List<PersonListViewModel> churchList, string documentType)
        {
            // Create new MigraDoc document
            Document document = new Document();
            document.Info.Title = documentType + " List for " + currentUser.Church.Name;
            document.Info.Author = "oikonomos";
            document.Info.Subject = documentType + " List";
            Section sec = document.AddSection();
            sec.PageSetup.TopMargin = Unit.FromCentimeter(1);
            sec.PageSetup.LeftMargin = Unit.FromCentimeter(1);
            document.DefaultPageSetup.TopMargin = Unit.FromCentimeter(1);
            document.DefaultPageSetup.LeftMargin = Unit.FromCentimeter(1);

            Paragraph p = new Paragraph();
            p.AddText(documentType + " List for " + currentUser.Church.Name);
            p.Format.Font.Size = 6.0;
            sec.Footers.Primary.Add(p);

            MigraDoc.DocumentObjectModel.Tables.Table table = new MigraDoc.DocumentObjectModel.Tables.Table();
            AddHeaders(table, documentType + "s");

            var style = document.Styles["Normal"];
            style.Font.Size = 8.0;
            TextMeasurement tm = new TextMeasurement(style.Font.Clone());
            int familyId = 0;
            foreach (PersonListViewModel person in churchList)
            {
                Row row=table.AddRow();
                Cell cell = row.Cells[0];
                if (familyId != person.FamilyId)
                {
                    cell.Format.Font.Bold = true;
                    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;
            }

            sec.Add(table);

            MigraDoc.Rendering.PdfDocumentRenderer pdfRender = new MigraDoc.Rendering.PdfDocumentRenderer(false, PdfSharp.Pdf.PdfFontEmbedding.Always);
            pdfRender.Document = document; //document is where all of my info has been written to and is a MigraDoc type
            pdfRender.RenderDocument();

            return pdfRender;
        }
        private static string AdjustIfTooWideToFitIn(Cell cell, string text, TextMeasurement tm)
        {
            Column column = cell.Column;
            Unit availableWidth = column.Width - column.Table.Borders.Width - cell.Borders.Width - Unit.FromMillimeter(2);

            var tooWideWords = text.Split(" ".ToCharArray()).Distinct().Where(s => TooWide(s, availableWidth, tm));

            var adjusted = new StringBuilder(text);
            foreach (string word in tooWideWords)
            {
                var replacementWord = MakeFit(word, availableWidth, tm);
                adjusted.Replace(word, replacementWord);
            }

            return adjusted.ToString();
        }
        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 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;
                }
            }
        }