public FileStreamResult CreateDivisionSheet(int tournamentId, int divisionId)
        {
            InitDocument();

            iTextSharpHeaderFooter pageEventHandler = new iTextSharpHeaderFooter();
            pageEventHandler.BaseFont = baseFont;
            pageEventHandler.ShowFooter = true;
            PDFWriter.PageEvent = pageEventHandler;

            document.Open();

            createEventSheet(tournamentId, db.TournamentDivisions.Single(m => m.TournamentId == tournamentId && m.DivisionId == divisionId));

            document.Close();

            return getFileStreamResult(ms, "tournamentsheet.pdf");
        }
        public FileStreamResult CreateDivisionSheets(int id, int? RankId, int? DivisionTypeId, int? AgeGroupId, bool IncludeRankSheet, bool IncludeAgeGroupSheet)
        {
            InitDocument();

            iTextSharpHeaderFooter pageEventHandler = new iTextSharpHeaderFooter();
            pageEventHandler.BaseFont = baseFont;
            PDFWriter.PageEvent = pageEventHandler;

            document.Open();

            IEnumerable<TournamentDivision> tournamentDivisions = db.TournamentDivisions.Where(m => m.TournamentId == id);
            if(RankId.HasValue)
            {
                tournamentDivisions = tournamentDivisions.Where(m => m.Division.RankId == RankId.Value);
            }
            if(DivisionTypeId.HasValue)
            {
                tournamentDivisions = tournamentDivisions.Where(m => m.Division.DivisionTypeId == DivisionTypeId.Value);
            }
            if(AgeGroupId.HasValue)
            {
                tournamentDivisions = tournamentDivisions.Where(m => m.Division.AgeGroupId == AgeGroupId.Value || m.Division.AgeGroup.ParentAgeGroupId == AgeGroupId.Value);
            }

            IEnumerable<Rank> ranks = db.Ranks;

            foreach (IGrouping<int, TournamentDivision> rankDivisionList in tournamentDivisions.ToLookup(m => m.Division.RankId))
            {
                if (IncludeRankSheet)
                {
                    // Create Rank Insert Sheet
                    document.SetPageSize(PageSize.A4.Rotate());
                    document.NewPage();
                    Paragraph rankParagraph = new Paragraph(ranks.Single(m => m.RankId == rankDivisionList.Key).Description.ToUpper(), fontHeader);
                    rankParagraph.Alignment = Rectangle.ALIGN_RIGHT;
                    document.Add(rankParagraph);
                    pageEventHandler.ShowFooter = false;
                }

                foreach (IGrouping<int, TournamentDivision> ageGroupDivisionList in rankDivisionList.Where(m => !m.Division.AgeGroup.IsSparringGroup).ToLookup(m => m.Division.AgeGroupId))
                {
                    if (IncludeAgeGroupSheet)
                    {
                        // Create Age Group Insert Sheet
                        document.SetPageSize(PageSize.A4.Rotate());
                        document.NewPage();
                        Paragraph rankParagraph = new Paragraph(ranks.Single(m => m.RankId == rankDivisionList.Key).Description.ToUpper(), fontHeader);
                        rankParagraph.Alignment = Rectangle.ALIGN_RIGHT;
                        document.Add(rankParagraph);
                        Paragraph ageGroupParagraph = new Paragraph(db.AgeGroups.Single(m => m.AgeGroupId == ageGroupDivisionList.Key).Description, fontHeader);
                        ageGroupParagraph.Alignment = Rectangle.ALIGN_RIGHT;
                        document.Add(ageGroupParagraph);
                        pageEventHandler.ShowFooter = false;
                    }

                    foreach (TournamentDivision tournamentDivision in tournamentDivisions.Where(m => m.TournamentId == id && m.Division.RankId == rankDivisionList.Key && (m.Division.AgeGroupId == ageGroupDivisionList.Key || m.Division.AgeGroup.ParentAgeGroupId == ageGroupDivisionList.Key)))
                    {
                        document.SetPageSize(PageSize.A4);
                        document.NewPage();
                        pageEventHandler.ShowFooter = true;

                        if (db.CompetitorDivisions.Any(m => m.Competitor.TournamentId == id && m.DivisionId == tournamentDivision.DivisionId))
                        {
                            createEventSheet(id, tournamentDivision);
                        }
                    }
                }
            }

            document.Close();

            return getFileStreamResult(ms, "tournamentsheets.pdf");
        }