public void ProcessRequest(HttpContext context)
        {
            int ShowID = Convert.ToInt32(context.Request["showid"]);
            int RingID = Convert.ToInt32(context.Request["ringid"]);
            String cmd = context.Request["doc"].ToString();

            Shows show = new Shows(ShowID);
            ShowDetails sd = new ShowDetails();
            DataTable table = sd.GetShowDetails(ShowID).Tables[0];
            String pdfPath = context.Server.MapPath(cmd + ".pdf");
            Boolean publish = false;
            if (!String.IsNullOrEmpty(context.Request["publish"]) && context.Request["publish"].ToString() == "1") publish = true;

            Document doc = new Document(PageSize.A4, -50, -50, 2, 2);
            Stream output;
            if (publish)
            {
                String path = context.Server.MapPath(@"..\schedules\");
                path += DateTime.Today.ToString("yyyy");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                path += String.Format("\\{0:yyyyMM}_{1}.pdf", show.ShowDate, show.ShowName);
                output = new FileStream(path, FileMode.Create);
            }
            else
            {
                output = new MemoryStream();
            }
            var writer = PdfWriter.GetInstance(doc, output);
            StyleSheet sheet = new StyleSheet();

            doc.Open();
            Font smallFont = new Font(Font.COURIER, 8, Font.NORMAL, Color.BLACK);
            Font normalFont = new Font(Font.COURIER, 10, Font.NORMAL, Color.BLACK);
            Font headerFont = new Font(Font.HELVETICA, 12, Font.BOLD, Color.BLACK);
            Font ClassTitleFont = new Font(Font.HELVETICA, 16, Font.NORMAL, Color.BLACK);
            Font bigFont = new Font(Font.HELVETICA, 25, Font.NORMAL, Color.BLACK);
            Font mediumFont = new Font(Font.HELVETICA, 20, Font.NORMAL, Color.BLACK);
            ShowClasses sc = new ShowClasses();
            Rings currentRing = new Rings(RingID);
            List<ShowClasses> showClasses = ShowClasses.GetAllClassesForShowRing(ShowID, RingID);
            PdfPTable callingListTbl = null;
            foreach (ShowClasses showClass in showClasses)
            {
                List<int> resultCnts = ShowClasses.getDogsInClass(showClass.ID, (showClass.Catagory == 0));

                int gradeidx = 0;
                foreach (int dogCounts in resultCnts)
                {
                    PdfPTable ptable = new PdfPTable(3);
                    PdfPCell cell;

                    String grades;
                    String subTitle;
                    if (showClass.Catagory == 0)
                    {
                        if (gradeidx < showClass.Grades.Length)
                        {
                            grades = "Grade " + showClass.Grades[gradeidx];
                        }
                        else
                        {
                            grades = showClass.Grades;
                        }
                        subTitle = String.Format("{0} {1} {2} {3} - Judge: {4}", showClass.longHeight, showClass.longCatagory, showClass.LongClassName, grades, Judge.getJudgeForClass(showClass.ID));
                    }
                    else
                    {
                        if (showClass.Grades.Length == 1)
                        {
                            grades = showClass.Grades;
                        }
                        else
                        {
                            grades = showClass.Grades[0] + " - " + showClass.Grades[showClass.Grades.Length - 1];
                        }
                        subTitle = String.Format("{0} {1} {3} {2} - Judge: {4}", showClass.longHeight, showClass.longCatagory, showClass.LongClassName, grades, Judge.getJudgeForClass(showClass.ID));

                    }
                    String tmp = String.Format("Result Sheet - Ring No:{0}", currentRing.RingNo);
                    cell = new PdfPCell(new Phrase(new Chunk(tmp, bigFont)));
                    cell.Colspan = 2;
                    cell.BorderWidth = 0;
                    ptable.AddCell(cell);

                    tmp = String.Format("Class No:{0}", showClass.ClassNo);
                    cell = new PdfPCell(new Phrase(new Chunk(tmp, bigFont)));
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    cell.BorderWidth = 2;
                    cell.Padding = 5;
                    ptable.AddCell(cell);

                    cell = new PdfPCell(new Phrase(new Chunk(subTitle, mediumFont)));
                    cell.Colspan = 3;
                    cell.BorderWidth = 0;
                    ptable.AddCell(cell);

                    cell = new PdfPCell(new Phrase(new Chunk("", ClassTitleFont)));
                    cell.Colspan = 3;
                    cell.BorderWidth = 0;
                    ptable.AddCell(cell);
                    ptable.AddCell(cell);
                    ptable.AddCell(cell);
                    doc.Add(ptable);

                    float[] colWidths = { 75, 75, 200, 200, 50, 75 };
                    callingListTbl = new PdfPTable(colWidths);

                    cell = new PdfPCell(new Phrase(new Chunk("Place", headerFont)));
                    cell.BorderWidth = 1;
                    cell.BorderWidthBottom = 0;
                    cell.FixedHeight = 35f;
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    callingListTbl.AddCell(cell);

                    cell = new PdfPCell(new Phrase(new Chunk("Ring Number", headerFont)));
                    cell.BorderWidth = 1;
                    cell.BorderWidthBottom = 0;
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    callingListTbl.AddCell(cell);

                    cell = new PdfPCell(new Phrase(new Chunk("Handler", headerFont)));
                    cell.BorderWidth = 1;
                    cell.BorderWidthBottom = 0;
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    callingListTbl.AddCell(cell);

                    cell = new PdfPCell(new Phrase(new Chunk("Dog Name", headerFont)));
                    cell.BorderWidth = 1;
                    cell.BorderWidthBottom = 0;
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    callingListTbl.AddCell(cell);

                    cell = new PdfPCell(new Phrase(new Chunk("Clear/ Faults", headerFont)));
                    cell.BorderWidth = 1;
                    cell.BorderWidthBottom = 0;
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    callingListTbl.AddCell(cell);

                    cell = new PdfPCell(new Phrase(new Chunk("Time", headerFont)));
                    cell.BorderWidth = 1;
                    cell.BorderWidthBottom = 0;
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    callingListTbl.AddCell(cell);

                    int maxRosettes = calcRosettes(dogCounts);
                    int maxTrophies = calcTrophies(dogCounts);
                    float rowHeight = 25f;
                    if (showClass.EntryType == 4)
                    {
                        rowHeight = 65f;
                    }
                    else if (showClass.EntryType == 5)
                    {
                        rowHeight = 120f;
                    }

                    for (int resultRow = 0; resultRow < maxRosettes; resultRow++)
                    {
                        int bwb = 0;
                        if (resultRow == maxRosettes - 1)
                        {
                            bwb = 1;
                        }
                        Color background = Color.WHITE;
                        if (maxTrophies > 0)
                        {
                            background = Color.LIGHT_GRAY;
                        }
                        maxTrophies--;

                        cell = new PdfPCell(new Phrase(new Chunk(calcPlace(resultRow + 1), normalFont)));
                        cell.BorderWidth = 1;
                        cell.BorderWidthBottom = bwb;
                        cell.FixedHeight = rowHeight;
                        cell.HorizontalAlignment = Element.ALIGN_CENTER;
                        cell.BackgroundColor = background;
                        callingListTbl.AddCell(cell);

                        cell = new PdfPCell(new Phrase(new Chunk("", normalFont)));
                        cell.BorderWidth = 1;
                        cell.BackgroundColor = background;
                        cell.BorderWidthBottom = bwb;

                        callingListTbl.AddCell(cell);
                        callingListTbl.AddCell(cell);
                        callingListTbl.AddCell(cell);
                        callingListTbl.AddCell(cell);
                        callingListTbl.AddCell(cell);
                    }
                    doc.Add(callingListTbl);
                    doc.NewPage();
                    gradeidx++;
                }
            }

            doc.Close();

            if (publish)
            {

            }
            else
            {
                context.Response.ClearContent();
                context.Response.ContentType = "application/pdf";
                context.Response.AddHeader("content-disposition", String.Format("inline;filename=PreviewSchedule.pdf"));
                context.Response.BinaryWrite((output as MemoryStream).ToArray());
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            //PDFBuilder.HtmlToPdfBuilder builder = new HtmlToPdfBuilder(PageSize.A4);
            //builder.AddPage();
            //builder.ImportStylesheet(context.Server.MapPath("pdfstylesheet.css"));

            int ShowID = Convert.ToInt32(context.Request["showid"]);

            Shows show = new Shows(ShowID);

            ShowDetails sd = new ShowDetails();
            DataTable table = sd.GetShowDetails(ShowID).Tables[0];
            String pdfPath = context.Server.MapPath("PreviewSchedule.pdf");
            String contents = File.ReadAllText(context.Server.MapPath("schedule1FrontPage.html"));
            Boolean publish = false;
            if (!String.IsNullOrEmpty(context.Request["publish"]) && context.Request["publish"].ToString() == "1") publish = true;

            String logoImg = context.Server.MapPath("FirstContactAgility.png") ;
            contents = contents.Replace("[CLUBLOGO]", "src='" + logoImg + "'");
            contents = contents.Replace("[SHOWDATES]", show.ShowDate.ToString("dddd, dd MMM yyyy"));
            contents = contents.Replace("[CLUBNAME]", show.ShowName);
            contents = contents.Replace("[SHOWVENUE]", show.Venue  + ", " + show.VenuePostcode);
            contents = contents.Replace("[OPENTIME]", "8:00am");
            contents = contents.Replace("[BRIEFING]", "8:15am");
            contents = contents.Replace("[JUDGINGSTARTS]", "8:30am");
            contents = contents.Replace("[SdHOWVENUE]", show.Venue + "," + show.VenuePostcode);
            contents = contents.Replace("[CLOSINGDATE]", show.ClosingDate.ToString("dd MMM yyyy"));
            contents = contents.Replace("[CHAIRMANNAME]", show.Chairman.Name);
            contents = contents.Replace("[CHAIRMANADDRESS]", show.Chairman.AddressDetails);
            contents = contents.Replace("[SECRETARYNAME]", show.Secretary.Name);
            contents = contents.Replace("[SECRETARYADDRESS]", show.Secretary.AddressDetails);
            contents = contents.Replace("[TREASURERNAME]", show.Treasurer.Name);
            contents = contents.Replace("[TREASURERADDRESS]", show.Treasurer.AddressDetails);
            contents = contents.Replace("[SHOWSECNAME]", show.ShowSec.Name);
            contents = contents.Replace("[SHOWSECADDRESS]", show.ShowSec.AddressDetails);
            contents = contents.Replace("[SHOWMANAGERNAME]", show.ShowManager.Name);
            contents = contents.Replace("[SHOWMANAGERADDRESS]", show.ShowManager.AddressDetails);
            contents = contents.Replace("[EQUIPMENTNAME]", show.Equipment.Name);
            contents = contents.Replace("[EQUIPMENTADDRESS]", show.Equipment.AddressDetails);
            contents = contents.Replace("[VETNAME]", show.Vet.Name);
            contents = contents.Replace("[VETADDRESS]", show.Vet.AddressDetails);
            contents = contents.Replace("[SHOWENTRIESNAME]", show.ShowEntries.Name);
            contents = contents.Replace("[SHOWENTRIESADDRESS]", show.ShowEntries.AddressDetails);
            contents = contents.Replace("[PAYABLENAME]", show.Payable.Name);
            contents = contents.Replace("[PAYABLEADDRESS]", show.Payable.AddressDetails);

            Document doc = new Document(PageSize.A4, 25, 10, 10, 10);
            Stream  output ;
            if (publish)
            {
                String path = context.Server.MapPath(@"..\schedules\");
                path += DateTime.Today.ToString("yyyy");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                path += String.Format("\\{0:yyyyMMM}_{1}.pdf", show.ShowDate, show.ShowName);
                output = new FileStream(path, FileMode.Create);

                show.setStatus(Shows.SHOW_STATUS.PUBLISHED);
            } else {
                output = new MemoryStream();
            }
            var writer = PdfWriter.GetInstance(doc, output);
            StyleSheet sheet = new StyleSheet();

            doc.Open();
            var parsedHTML = HTMLWorker.ParseToList(new StringReader(contents), sheet);
            foreach (var htmlElement in parsedHTML)
            {
                doc.Add(htmlElement as IElement);
            }
            doc.NewPage();
            foreach (DataRow row in table.Rows)
            {
                ShowDetails _showDetails = new ShowDetails(row);
                DataTable showDayTable = _showDetails.GetDayData(true);

                String dayHtml = generateClassesForDay(_showDetails.ShowDetailsID, showDayTable);
                doc.NewPage();
                var dayClasses = HTMLWorker.ParseToList(new StringReader(dayHtml), sheet);
                foreach (var htmlElement in dayClasses)
                {
                    doc.Add(htmlElement as IElement);
                }
            };

            doc.NewPage();
            contents = File.ReadAllText(context.Server.MapPath("schedule2RulesNRegs.html"));
            parsedHTML = HTMLWorker.ParseToList(new StringReader(contents), sheet);
            foreach (var htmlElement in parsedHTML)
            {
                doc.Add(htmlElement as IElement);
            }

            #if TEST
            foreach (DataRow row in table.Rows)
            {
                PdfPTable pdfTable = new PdfPTable(5);
                ShowDetails _showDetails = new ShowDetails(row);
                DataTable showDayTable = _showDetails.GetDayData(true);
                String dateThing = getDateOrdinalSuffix(_showDetails.ShowDate.Day);
                String datestr = _showDetails.ShowDate.ToString("dddd MMM d").ToUpper() + dateThing;

                PdfPCell pdfCell = new PdfPCell();
                pdfCell = new PdfPCell(new Phrase(datestr ));
                pdfCell.Colspan = 5;
                pdfTable.AddCell(pdfCell);

                pdfTable.AddCell("ClsNo");
                pdfCell = new PdfPCell(new Phrase("Name"));
                pdfCell.Colspan = 3;
                pdfTable.AddCell(pdfCell);
                pdfTable.AddCell("Grades");

                pdfTable = new PdfPTable(5);
                foreach (DataRow clsRow in showDayTable.Rows)
                {
                    PdfPTable grades = new PdfPTable(7);
                    String str = clsRow["grades"].ToString();
                    for (int i = 1; i <= 7; i++)
                    {
                        if (str.IndexOf((char)(i + 48)) > -1)
                        {
                            grades.AddCell(i.ToString());
                        }
                        else
                        {
                            grades.AddCell(" ");
                        }
                    }
                    pdfTable.AddCell(clsRow["ClsNo"].ToString() );

                    pdfCell = new PdfPCell(new Phrase(clsRow["Name"].ToString()));
                    pdfCell.Colspan = 3;
                    pdfTable.AddCell(pdfCell);
                    pdfTable.AddCell(grades);
                    doc.Add(pdfTable);
                }
                doc.NewPage();
            }
            #endif
            doc.Close();

            if (publish)
            {

            }
            else
            {
                context.Response.ClearContent();
                context.Response.ContentType = "application/pdf";
                context.Response.AddHeader("content-disposition", String.Format("inline;filename=PreviewSchedule.pdf"));
                context.Response.BinaryWrite( (output as MemoryStream).ToArray());
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            int ShowID = Convert.ToInt32(context.Request["showid"]);
            int RingID = Convert.ToInt32(context.Request["ringid"]);
            String cmd = context.Request["doc"].ToString();
            Shows show = new Shows(ShowID);

            ShowDetails sd = new ShowDetails();
            DataTable table = sd.GetShowDetails(ShowID).Tables[0];
            String pdfPath = context.Server.MapPath(cmd + ".pdf");
            Boolean publish = false;
            if (!String.IsNullOrEmpty(context.Request["publish"]) && context.Request["publish"].ToString() == "1") publish = true;

            Document doc = new Document(PageSize.A4, -50, -50, 2, 2);
            Stream output;
            if (publish)
            {
                String path = context.Server.MapPath(@"..\schedules\");
                path += DateTime.Today.ToString("yyyy");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                path += String.Format("\\{0:yyyyMM}_{1}.pdf", show.ShowDate, show.ShowName);
                output = new FileStream(path, FileMode.Create);
            }
            else
            {
                output = new MemoryStream();
            }
            var writer = PdfWriter.GetInstance(doc, output);
            StyleSheet sheet = new StyleSheet();

            doc.Open();
            Font font = new Font(Font.COURIER, 10, Font.NORMAL, Color.BLACK);
            Font resFont = new Font(Font.COURIER, 9, Font.ITALIC, Color.BLACK);
            Font headerFont = new Font(Font.COURIER, 10, Font.BOLD, Color.BLACK);
            Font headerFont2 = new Font(Font.HELVETICA, 12, Font.BOLD, Color.BLACK);
            Font ClassTitleFont = new Font(Font.HELVETICA, 16, Font.NORMAL, Color.BLACK);
            ShowClasses sc = new ShowClasses();
            Rings currentRing = new Rings(RingID);
            List<ShowClasses> showClasses = ShowClasses.GetAllClassesForShowRing(ShowID, RingID);
            foreach (ShowClasses showClass in showClasses)
            {
                PdfPTable ptable = new PdfPTable(3);
                PdfPCell cell;

                String grades = showClass.Grades;
                if (grades.Length == 1)
                {
                    grades = "Grade " + grades;
                }
                else
                {
                    grades = "Grades " + grades[0] + "-" + grades[grades.Length - 1];
                }
                String tmp = String.Format("Ring No:{5}   Class No:{0} {1} {2} {3} ({4})", showClass.ClassNo, showClass.longHeight, showClass.longCatagory, showClass.LongClassName, grades, currentRing.RingNo);
                cell = new PdfPCell(new Phrase(new Chunk(tmp, ClassTitleFont)));
                cell.Colspan = 3;
                cell.BorderWidth = 0;
                ptable.AddCell(cell);
                cell = new PdfPCell(new Phrase(new Chunk("", ClassTitleFont)));
                cell.Colspan = 3;
                cell.BorderWidth = 0;
                ptable.AddCell(cell);
                ptable.AddCell(cell);
                ptable.AddCell(cell);
                doc.Add(ptable);

                DataSet callingList = sc.getCallingList(ShowID, showClass.ID);
                float[] colWidths = { 35, 50, 200, 200 };
                PdfPTable callingListTbl = new PdfPTable(colWidths);

                cell = new PdfPCell(new Phrase(new Chunk("RO", headerFont)));
                cell.BorderWidth = 0;
                callingListTbl.AddCell(cell);

                cell = new PdfPCell(new Phrase(new Chunk("Ring No", headerFont)));
                cell.BorderWidth = 0;
                callingListTbl.AddCell(cell);

                cell = new PdfPCell(new Phrase(new Chunk("Handler", headerFont)));
                cell.BorderWidth = 0;
                callingListTbl.AddCell(cell);

                cell = new PdfPCell(new Phrase(new Chunk("Dog Name", headerFont)));
                cell.BorderWidth = 0;
                callingListTbl.AddCell(cell);
                int altFlag = 0;
                Color altLine = Color.LIGHT_GRAY;
                foreach (DataRow row in callingList.Tables[0].Rows)
                {
                    Color altColor = Color.WHITE;
                    if (altFlag % 2 == 0 && showClass.EntryType != 5)
                    {
                        altColor = altLine;
                    }

                    cell = new PdfPCell(new Phrase(new Chunk(row["RO"].ToString(), font)));
                    cell.BorderWidth = 0;
                    cell.BorderWidth = 2;
                    cell.BorderColor = Color.RED;
                    cell.BackgroundColor = altColor;
                    cell.HorizontalAlignment = Element.ALIGN_RIGHT;
                    callingListTbl.AddCell(cell);

                    if (showClass.EntryType == 5)
                    {
                        cell = new PdfPCell(new Phrase(new Chunk("Team Name:", headerFont2)));
                        cell.BackgroundColor = altColor;
                        cell.BorderWidth = 1;
                        cell.Colspan = 2;
                        cell.FixedHeight = 30F;
                        callingListTbl.AddCell(cell);

                        cell = new PdfPCell(new Phrase(new Chunk(ShowClasses.expandHeight(row), headerFont2)));
                        cell.BackgroundColor = altColor;
                        cell.BorderWidth = 1;
                        cell.FixedHeight = 30F;
                        cell.HorizontalAlignment = Element.ALIGN_RIGHT;
                        callingListTbl.AddCell(cell);

                        cell = new PdfPCell(new Phrase(new Chunk("", font)));
                        cell.BackgroundColor = altColor;
                        cell.Colspan = 1;
                        cell.BorderWidth = 0;
                        cell.FixedHeight = 30F;
                        callingListTbl.AddCell(cell);
                    }

                    int borderWidth = 0;
                    if (showClass.EntryType == 5) borderWidth = 1;
                    cell = new PdfPCell(new Phrase(new Chunk(row["RingNumber"].ToString(), font)));
                    cell.HorizontalAlignment = Element.ALIGN_RIGHT;
                    cell.BackgroundColor = altColor;
                    cell.BorderWidth = borderWidth;
                    callingListTbl.AddCell(cell);

                    int DefaultHandler = Convert.ToInt32(row["DefaultHandler"]);
                    int AltHandler = Convert.ToInt32(row["AltHandler"]);
                    String handlerName = row["Name"].ToString();
                    User user;
                    if (DefaultHandler > 0) {
                        user = new User(DefaultHandler);
                        handlerName = user.Name;
                    }
                    if (AltHandler > 0)
                    {
                        user = new User(AltHandler);
                        handlerName = user.Name;
                    }

                    cell = new PdfPCell(new Phrase(new Chunk(handlerName, font)));
                    cell.BackgroundColor = altColor;
                    cell.BorderWidth = borderWidth;
                    callingListTbl.AddCell(cell);

                    cell = new PdfPCell(new Phrase(new Chunk(row["KCName"].ToString(), font)));
                    cell.BackgroundColor = altColor;
                    cell.BorderWidth = borderWidth;
                    callingListTbl.AddCell(cell);

                    if (showClass.EntryType == 4 || showClass.EntryType == 5)
                    {
                        int UserID = Convert.ToInt32(row["UserID"]);
                        int DogID = Convert.ToInt32(row["DogID"]);
                        //MultiDog md = new MultiDog(UserID, DogID, showClass.ID);

                        List<MultiDog> otherHandlers = MultiDog.getMultiDog(UserID, DogID, showClass.ID);

                        String reserves = "";
                        int dogCnt = 0;
                        foreach (MultiDog md in otherHandlers)
                        {
                            String res = "";
                            if (dogCnt > 2 && md.Handlername.Length > 0)
                            {
                                if (reserves.Length > 0) reserves += "; ";
                                reserves += String.Format("{0} & {1}", md.Handlername, md.Dogname);
                            }

                            if (dogCnt < 3)
                            {

                                cell = new PdfPCell(new Phrase(new Chunk("", font)));
                                cell.BorderWidth = 0;
                                cell.BackgroundColor = altColor;
                                cell.HorizontalAlignment = Element.ALIGN_RIGHT;
                                callingListTbl.AddCell(cell);

                                cell = new PdfPCell(new Phrase(new Chunk(res, font)));
                                cell.HorizontalAlignment = Element.ALIGN_RIGHT;
                                cell.BackgroundColor = altColor;
                                cell.BorderWidth = 1;
                                callingListTbl.AddCell(cell);

                                cell = new PdfPCell(new Phrase(new Chunk(md.Handlername, font)));
                                cell.BackgroundColor = altColor;
                                cell.BorderWidth = 1;
                                callingListTbl.AddCell(cell);

                                cell = new PdfPCell(new Phrase(new Chunk(md.Dogname, font)));
                                cell.BackgroundColor = altColor;
                                cell.BorderWidth = 1;
                                callingListTbl.AddCell(cell);
                            }
                            dogCnt++;

                        }

                        cell = new PdfPCell(new Phrase(new Chunk("", font)));
                        cell.BackgroundColor = altColor;
                        cell.BorderWidth = 0;
                        cell.FixedHeight = 30F;
                        callingListTbl.AddCell(cell);

                        cell = new PdfPCell(new Phrase(new Chunk("Reserves: " + reserves, resFont)));
                        cell.BackgroundColor = altColor;
                        cell.Colspan = 3;
                        cell.BorderWidth = 1;
                        cell.FixedHeight = 30F;
                        callingListTbl.AddCell(cell);
                    }
                    altFlag++;

                }
                doc.Add(callingListTbl);
                doc.NewPage();
            }

            doc.Close();

            if (publish)
            {

            }
            else
            {
                context.Response.ClearContent();
                context.Response.ContentType = "application/pdf";
                context.Response.AddHeader("content-disposition", String.Format("inline;filename=PreviewSchedule.pdf"));
                context.Response.BinaryWrite((output as MemoryStream).ToArray());
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            int ShowID = Convert.ToInt32(context.Request["showid"]);
            Shows show = new Shows(ShowID);

            String sortby = Convert.ToString(context.Request["sortby"]);

            ShowDetails sd = new ShowDetails();
            DataTable table = sd.GetShowDetails(ShowID).Tables[0];

            Document doc = new Document(PageSize.A4, -10, -50, 2, 2);
            Stream output = new MemoryStream();
            var writer = PdfWriter.GetInstance(doc, output);
            StyleSheet sheet = new StyleSheet();

            doc.Open();
            Font smallFont = new Font(Font.COURIER, 8, Font.NORMAL, Color.BLACK);
            Font normalFont = new Font(Font.COURIER, 8, Font.NORMAL, Color.BLACK);
            Font normalFontBold = new Font(Font.COURIER, 8, Font.BOLD, Color.BLACK);
            Font headerFont = new Font(Font.HELVETICA, 12, Font.BOLD, Color.BLACK);
            Font ClassTitleFont = new Font(Font.HELVETICA, 16, Font.NORMAL, Color.BLACK);
            Font bigFont = new Font(Font.HELVETICA, 25, Font.NORMAL, Color.BLACK);
            Font mediumFont = new Font(Font.HELVETICA, 20, Font.NORMAL, Color.BLACK);

            PdfPTable ptable = new PdfPTable(1);
            PdfPCell cell;

            cell = new PdfPCell();
            cell.FixedHeight = 150;
            cell.BorderWidth = 0;
            ptable.AddCell(cell);

            cell = new PdfPCell(new Phrase(new Chunk("Master List", bigFont)));
            cell.HorizontalAlignment = Element.ALIGN_CENTER;
            cell.BorderWidth = 0;
            cell.FixedHeight = 100;
            ptable.AddCell(cell);

            cell = new PdfPCell(new Phrase(new Chunk(show.ShowName, bigFont)));
            cell.HorizontalAlignment = Element.ALIGN_CENTER;
            cell.BorderWidth = 0;
            ptable.AddCell(cell);

            cell = new PdfPCell(new Phrase(new Chunk(show.ShowDate.ToString("dd MMM yyyy"), bigFont)));
            cell.BorderWidth = 0;
            cell.HorizontalAlignment = Element.ALIGN_CENTER;
            ptable.AddCell(cell);
            doc.Add(ptable);
            doc.NewPage();

            float[] colWidths = { 100, 40, 175, 200};
            int count = 0;
            String lastName = "";
            DataSet masterList = DogClasses.getMasterList(ShowID, sortby);
            String tmp = "";

            ptable = new PdfPTable(colWidths);
            cell = new PdfPCell(new Phrase(new Chunk("Handler Name", headerFont)));
            cell.BorderWidth = 0;
            ptable.AddCell(cell);

            cell = new PdfPCell(new Phrase("Ring No", headerFont));
            cell.BorderWidth = 0;
            ptable.AddCell(cell);

            cell = new PdfPCell(new Phrase("Dog Name", headerFont));
            cell.BorderWidth = 0;
            ptable.AddCell(cell);

            cell = new PdfPCell(new Phrase("Class Details", headerFont));
            cell.BorderWidth = 0;
            ptable.AddCell(cell);
            doc.Add(ptable);

            bool topBorder = false;
            Color altLine = new Color(224, 224, 224);
            foreach (DataRow row in masterList.Tables[0].Rows)
            {
                Color altColor = Color.WHITE;
                if (count % 2 == 0 )
                {
                    altColor = altLine;
                }

                MasterList masterListItem = new MasterList(row);
                ptable = new PdfPTable(colWidths);
                if (lastName == masterListItem.Name)
                {
                    cell = new PdfPCell(new Phrase(new Chunk("", normalFont)));
                    topBorder = false;
                }
                else
                {
                    topBorder = true;
                    tmp = masterListItem.Name;
                    //if (masterListItem.AltHandler > 0)
                    //{
                    //    User u = new User(masterListItem.AltHandler);
                    //    tmp = u.Name;
                    //}

                    cell = new PdfPCell(new Phrase(new Chunk(tmp, normalFont)));
                    cell.BorderColorTop = Color.BLACK;
                    cell.BorderWidthTop = 1;
                }
                cell.BorderWidth = 0;
                cell.BackgroundColor = altColor;
                ptable.AddCell(cell);

                cell = new PdfPCell(new Phrase(new Chunk(masterListItem.RingNumber.ToString(), normalFont)));
                cell.BorderWidth = 0;
                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.BackgroundColor = altColor;
                if (topBorder)
                {
                    cell.BorderColorTop = Color.BLACK;
                    cell.BorderWidthTop = 1;
                }
                ptable.AddCell(cell);

                Paragraph p = new Paragraph();
                Phrase ph = new Phrase();
                ph.Add(new Chunk(masterListItem.KCName, normalFont));
                ph.Add(Chunk.NEWLINE);
                ph.Add(new Chunk(masterListItem.KCNumber, normalFont));
                cell = new PdfPCell(ph);
                cell.BorderWidth = 0;
                cell.BackgroundColor = altColor;
                if (topBorder)
                {
                    cell.BorderColorTop = Color.BLACK;
                    cell.BorderWidthTop = 1;
                }
                ptable.AddCell(cell);

                PdfPTable classDetailsTbl = new PdfPTable(1);
                List<DogClasses> dcList = DogClasses.Retrieve(masterListItem.DogID, ShowID);
                p = new Paragraph();
                bool first = true;
                foreach (DogClasses dc in dcList)
                {
                    ShowClasses sc = new ShowClasses(dc.Classid);
                    if (!first)
                    {
                        p.Add(new Chunk(String.Format(",  {0,3}:", sc.ClassNo), normalFont));
                    }
                    else
                    {
                        p.Add(new Chunk(String.Format("{0,3}:", sc.ClassNo), normalFont));
                    }
                    p.Add(new Chunk(String.Format("{0,3}", dc.RO), normalFontBold));
                    first = false;
                }

                cell = new PdfPCell(p);
                cell.BorderWidth = 0;
                classDetailsTbl.AddCell(cell);
                cell = new PdfPCell(classDetailsTbl);
                cell.BorderWidth = 0;
                cell.BackgroundColor = altColor;
                if (topBorder)
                {
                    cell.BorderColorTop = Color.BLACK;
                    cell.BorderWidthTop = 1;
                }
                ptable.AddCell(cell);

                doc.Add(ptable);

                lastName = masterListItem.Name;

                count++;
                //if (count > 100 ) break;
            }

            doc.Close();

            context.Response.ClearContent();
            context.Response.ContentType = "application/pdf";
            context.Response.AddHeader("content-disposition", String.Format("inline;filename=PreviewSchedule.pdf"));
            context.Response.BinaryWrite((output as MemoryStream).ToArray());
        }
        public void ProcessRequest(HttpContext context)
        {
            //PDFBuilder.HtmlToPdfBuilder builder = new HtmlToPdfBuilder(PageSize.A4);
            //builder.AddPage();
            //builder.ImportStylesheet(context.Server.MapPath("pdfstylesheet.css"));

            int ShowID = Convert.ToInt32(context.Request["showid"]);

            Shows show = new Shows(ShowID);

            ShowDetails sd = new ShowDetails();
            DataTable table = sd.GetShowDetails(ShowID).Tables[0];
            String pdfPath = context.Server.MapPath("PreviewSchedule.pdf");
            Boolean publish = false;
            if (!String.IsNullOrEmpty(context.Request["publish"]) && context.Request["publish"].ToString() == "1") publish = true;

            Document doc = new Document(PageSize.A4, 25, 10, 10, 10);
            Stream  output ;
            if (publish)
            {
                String path = context.Server.MapPath(@"..\schedules\");
                path += DateTime.Today.ToString("yyyy");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                path += String.Format("\\{0:yyyyMM}_{1}.pdf", show.ShowDate, show.ShowName);
                output = new FileStream(path, FileMode.Create);
            } else {
                output = new MemoryStream();
            }
            var writer = PdfWriter.GetInstance(doc, output);
            StyleSheet sheet = new StyleSheet();

            doc.Open();

            int clsNo = 0;
            ShowClasses sc = new ShowClasses();
            DataSet callingList = sc.getCallingList(ShowID);
            foreach (DataRow row in callingList.Tables[0].Rows)
            {
                if (clsNo > 0 && clsNo != Convert.ToInt32( row["ClsNo"]))  {
                    doc.NewPage();
                }

                var dayClasses = HTMLWorker.ParseToList(new StringReader(dayHtml), sheet);
                foreach (var htmlElement in dayClasses)
                {
                    doc.Add(htmlElement as IElement);
                }
            };

            doc.NewPage();
            contents = File.ReadAllText(context.Server.MapPath("schedule2RulesNRegs.html"));
            parsedHTML = HTMLWorker.ParseToList(new StringReader(contents), sheet);
            foreach (var htmlElement in parsedHTML)
            {
                doc.Add(htmlElement as IElement);
            }

            doc.Close();

            if (publish)
            {

            }
            else
            {
                context.Response.ClearContent();
                context.Response.ContentType = "application/pdf";
                context.Response.AddHeader("content-disposition", String.Format("inline;filename=PreviewSchedule.pdf"));
                context.Response.BinaryWrite( (output as MemoryStream).ToArray());
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            int ShowID = Convert.ToInt32(context.Request["showid"]);
            int RingID = Convert.ToInt32(context.Request["ringid"]);
            String cmd = context.Request["doc"].ToString();
            Shows show = new Shows(ShowID);

            ShowDetails sd = new ShowDetails();
            DataTable table = sd.GetShowDetails(ShowID).Tables[0];
            String pdfPath = context.Server.MapPath(cmd + ".pdf");
            Boolean publish = false;
            if (!String.IsNullOrEmpty(context.Request["publish"]) && context.Request["publish"].ToString() == "1") publish = true;

            Document doc = new Document(PageSize.A6, 35, 0, 20, 2);
            Stream output;
            if (publish)
            {
                String path = context.Server.MapPath(@"..\schedules\");
                path += DateTime.Today.ToString("yyyy");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                path += String.Format("\\{0:yyyyMM}_{1}.pdf", show.ShowDate, show.ShowName);
                output = new FileStream(path, FileMode.Create);
            }
            else
            {
                output = new MemoryStream();
            }
            var writer = PdfWriter.GetInstance(doc, output);
            StyleSheet sheet = new StyleSheet();

            doc.Open();
            Font font = new Font(Font.HELVETICA, 10, Font.NORMAL, Color.BLACK);
            Font smallFont = new Font(Font.HELVETICA, 8, Font.NORMAL, Color.BLACK);
            Font notSoBigFont = new Font(Font.HELVETICA, 11, Font.BOLD, Color.BLACK);
            Font fontWhite = new Font(Font.HELVETICA, 10, Font.NORMAL, Color.WHITE);
            Font headerFont = new Font(Font.HELVETICA, 12, Font.BOLD, Color.BLACK);
            Font spaceFont = new Font(Font.HELVETICA, 40, Font.BOLD, Color.WHITE);
            Font totalsFont = new Font(Font.HELVETICA, 14, Font.BOLD, Color.BLACK);
            Font bigBoyFont = new Font(Font.HELVETICA, 20, Font.BOLD, Color.BLACK);
            Font bigBoyFont2 = new Font(Font.HELVETICA, 25, Font.BOLD, Color.BLACK);
            PdfPCell blankCell = new PdfPCell(new Phrase(new Chunk("", font)));
            ShowClasses sc = new ShowClasses();
            Rings currentRing = new Rings(RingID);
            List<ShowClasses> showClasses = ShowClasses.GetAllClassesForShowRing(ShowID, RingID);
            String tmp;
            foreach (ShowClasses showClass in showClasses)
            {
                PdfPCell cell;
                DataSet callingList = sc.getCallingList(ShowID, showClass.ID);

                String grades = showClass.Grades;
                if (grades.Length == 1)
                {
                    grades = "Grade " + grades;
                }
                else
                {
                    grades = "Grades " + grades[0] + "-" + grades[grades.Length - 1];
                }
                PdfPTable ptable = new PdfPTable(1);
                tmp = String.Format("Class {0}", showClass.ClassNo);
                cell = new PdfPCell(new Phrase(new Chunk(tmp, bigBoyFont2)));
                cell.BorderWidth = 0;
                cell.Padding = 5;
                cell.HorizontalAlignment = Element.ALIGN_MIDDLE;
                cell.VerticalAlignment = Element.ALIGN_CENTER;
                ptable.AddCell(cell);

                tmp = String.Format("{0} {1} {2} {3}", showClass.longHeight, showClass.longCatagory, showClass.LongClassName, showClass.ClassName);
                cell = new PdfPCell(new Phrase(new Chunk(tmp, totalsFont)));
                cell.BorderWidth = 0;
                cell.Padding = 5;
                cell.HorizontalAlignment = Element.ALIGN_MIDDLE;
                cell.VerticalAlignment = Element.ALIGN_CENTER;
                ptable.AddCell(cell);

                tmp = String.Format("{0}", grades);
                cell = new PdfPCell(new Phrase(new Chunk(tmp, totalsFont)));
                cell.BorderWidth = 0;
                cell.Padding = 5;
                cell.FixedHeight = 100f;
                cell.HorizontalAlignment = Element.ALIGN_MIDDLE;
                cell.VerticalAlignment = Element.ALIGN_CENTER;
                ptable.AddCell(cell);

                tmp = String.Format("Ring {0}", currentRing.RingNo);
                cell = new PdfPCell(new Phrase(new Chunk(tmp, totalsFont)));
                cell.BorderWidth = 0;
                cell.Padding = 5;
                cell.FixedHeight = 30f;
                cell.HorizontalAlignment = Element.ALIGN_MIDDLE;
                ptable.AddCell(cell);

                tmp = String.Format("Ring Order = {0}", showClass.Position + 1);
                cell = new PdfPCell(new Phrase(new Chunk(tmp, totalsFont)));
                cell.BorderWidth = 0;
                cell.Padding = 5;
                cell.FixedHeight = 30f;
                cell.HorizontalAlignment = Element.ALIGN_MIDDLE;
                ptable.AddCell(cell);

                doc.Add(ptable);
                doc.NewPage();

                foreach (DataRow row in callingList.Tables[0].Rows)
                {
                    int Cat = Convert.ToInt32(row["Catagory"]);
                    if (Cat == 0)
                    {
                        PdfPTable tbl = new PdfPTable(1);
                        int dogGrade = Convert.ToInt32(row["Grade"]);
                        if (dogGrade == 99 ) {
                            tmp = "Veteran";
                        } else {
                            tmp = String.Format("Grade {0}", row["Grade"]);
                        }
                        cell = new PdfPCell(new Phrase(new Chunk(tmp, bigBoyFont2)));
                        cell.BackgroundColor = CMYKColor.LIGHT_GRAY;
                        cell.BorderWidth = 2;
                        cell.Padding = 9;
                        cell.PaddingTop = 4;
                        cell.HorizontalAlignment = Element.ALIGN_RIGHT;
                        tbl.AddCell(cell);
                        doc.Add(tbl);

                    }

                    float[] colWidths = { 300, 150 };
                    PdfPTable callingListTbl = new PdfPTable(colWidths);
                    tmp = String.Format("Class {0}: {1} {2} {3} {4}", showClass.ClassNo, showClass.longHeight, showClass.LongClassName, grades, showClass.ClassName);
                    cell = new PdfPCell(new Phrase(new Chunk(tmp, notSoBigFont)));
                    cell.BorderWidth = 2;
                    cell.BorderColorRight = Color.BLACK;
                    cell.BorderColorBottom = Color.BLACK;
                    cell.Padding = 5;
                    cell.PaddingTop = 1;
                    cell.PaddingLeft = 2;
                    cell.FixedHeight = 50f;
                    callingListTbl.AddCell(cell);

                    tmp = String.Format("{0}", row["RO"]);
                    cell = new PdfPCell(new Phrase(new Chunk(tmp, bigBoyFont)));
                    cell.BorderWidth = 2;
                    cell.BorderColorLeft = Color.BLACK;
                    cell.BorderColorBottom = Color.BLACK;
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    cell.Padding = 5;
                    callingListTbl.AddCell(cell);

                    cell = new PdfPCell(new Phrase(new Chunk("Course Time", notSoBigFont)));
                    cell.BorderWidth = 2;
                    cell.Padding = 5;
                    cell.BorderColorRight = Color.BLACK;
                    cell.HorizontalAlignment = Element.ALIGN_RIGHT;
                    callingListTbl.AddCell(cell);

                    cell = new PdfPCell(new Phrase(new Chunk("", notSoBigFont)));
                    cell.BorderWidth = 2;
                    cell.Padding = 5;
                    cell.BorderColorBottom = Color.BLACK;
                    callingListTbl.AddCell(cell);

                    int DefaultHandler = Convert.ToInt32(row["DefaultHandler"]);
                    int AltHandler = Convert.ToInt32(row["AltHandler"]);
                    String handlerName = row["Name"].ToString();
                    User user;
                    if (DefaultHandler > 0)
                    {
                        user = new User(DefaultHandler);
                        handlerName = user.Name;
                    }
                    if (AltHandler > 0)
                    {
                        user = new User(AltHandler);
                        handlerName = user.Name;
                    }

                    if (showClass.EntryType == 5)
                    {
                        cell = new PdfPCell(new Phrase(new Chunk(String.Format("{0}", handlerName), smallFont)));
                        cell.BorderWidth = 0;
                        cell.Colspan = 1;
                        cell.HorizontalAlignment = Element.ALIGN_LEFT;
                        callingListTbl.AddCell(cell);

                        cell = new PdfPCell(new Phrase(new Chunk(String.Format("{0,-10}", row["KCName"]), smallFont)));
                        cell.Colspan = 1;
                        cell.HorizontalAlignment = Element.ALIGN_RIGHT;
                        cell.BorderWidth = 0;
                    }
                    else
                    {
                        cell = new PdfPCell(new Phrase(new Chunk(String.Format("{0}", handlerName), notSoBigFont)));
                        cell.BorderWidth = 0;
                        cell.Colspan = 2;
                        callingListTbl.AddCell(cell);

                        cell = new PdfPCell(new Phrase(new Chunk(String.Format("{0,-10} {1}", row["RingNumber"], row["KCName"]), notSoBigFont)));
                        cell.BorderWidth = 0;
                    }
                    if (showClass.EntryType < 4)
                    {
                        cell.FixedHeight = 170f;
                    }
                    cell.Colspan = 2;
                    callingListTbl.AddCell(cell);

                    if (showClass.EntryType == 4 || showClass.EntryType == 5 )
                    {
                        int UserID = Convert.ToInt32(row["UserID"]);
                        int DogID = Convert.ToInt32(row["DogID"]);
                        List<MultiDog> otherHandlers = MultiDog.getMultiDog(UserID, DogID, showClass.ID);

                        int dogCnt = 0;
                        foreach (MultiDog md in otherHandlers)
                        {

                            if (dogCnt < 3)
                            {
                                //html += String.Format("<div class='dogInClass'><div style='width:8px;height:10px;' class='dogIcon'></div>[{1}] {0}</div>", md.Dogname, dogRow["RO"]);
                                if (showClass.EntryType == 4)
                                {
                                    cell = new PdfPCell(new Phrase(new Chunk(String.Format("{0}", md.Handlername), notSoBigFont)));
                                    cell.BorderWidth = 0;
                                    cell.Colspan = 2;
                                    callingListTbl.AddCell(cell);
                                    cell = new PdfPCell(new Phrase(new Chunk(String.Format("{0,-10} {1}", "", md.Dogname), notSoBigFont)));
                                    cell.BorderWidth = 0;
                                    cell.FixedHeight = 160f;
                                    cell.Colspan = 2;
                                }
                                else
                                {
                                    cell = new PdfPCell(new Phrase(new Chunk(String.Format("{0}", md.Handlername), smallFont)));
                                    cell.BorderWidth = 0;
                                    cell.Colspan = 1;
                                    cell.HorizontalAlignment = Element.ALIGN_LEFT;
                                    callingListTbl.AddCell(cell);
                                    cell = new PdfPCell(new Phrase(new Chunk(String.Format("{0,-10} {1}", "", md.Dogname), smallFont)));
                                    cell.HorizontalAlignment = Element.ALIGN_RIGHT;
                                    cell.BorderWidth = 0;
                                    cell.Colspan = 1;

                                }
                                callingListTbl.AddCell(cell);
                            }
                            dogCnt++;
                        }

                        if (showClass.EntryType == 5)
                        {
                            cell = new PdfPCell(new Phrase(new Chunk("", smallFont)));
                            cell.BorderWidth = 0;
                            cell.FixedHeight = 140f;
                            cell.Colspan = 2;
                            callingListTbl.AddCell(cell);

                        }
                    }

                    cell = new PdfPCell(new Phrase(new Chunk("Time", notSoBigFont)));
                    cell.BorderWidth = 2;
                    cell.BorderColorRight = Color.BLACK;
                    cell.BorderColorBottom = Color.BLACK;
                    cell.Padding = 5;
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    callingListTbl.AddCell(cell);
                    callingListTbl.AddCell(blankCell);

                    cell = new PdfPCell(new Phrase(new Chunk("Course Faults", notSoBigFont)));
                    cell.BorderWidth = 2;
                    cell.BorderColorRight = Color.BLACK;
                    cell.BorderColorBottom = Color.BLACK;
                    cell.Padding = 5;
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    callingListTbl.AddCell(cell);
                    callingListTbl.AddCell(blankCell);

                    cell = new PdfPCell(new Phrase(new Chunk("Time Faults", notSoBigFont)));
                    cell.BorderWidth = 2;
                    cell.BorderColorRight = Color.BLACK;
                    cell.BorderColorBottom = Color.BLACK;
                    cell.Padding = 5;
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    callingListTbl.AddCell(cell);
                    callingListTbl.AddCell(blankCell);

                    cell = new PdfPCell(new Phrase(new Chunk("Totals Faults", notSoBigFont)));
                    cell.BorderWidth = 2;
                    cell.BorderColorRight = Color.BLACK;
                    cell.BorderColorBottom = Color.BLACK;
                    cell.Padding = 5;
                    cell.HorizontalAlignment = Element.ALIGN_CENTER;
                    callingListTbl.AddCell(cell);
                    callingListTbl.AddCell(blankCell);
                    doc.Add(callingListTbl);
                    doc.NewPage();

                }
            }

            doc.Close();

            if (publish)
            {

            }
            else
            {
                context.Response.ClearContent();
                context.Response.ContentType = "application/pdf";
                context.Response.AddHeader("content-disposition", String.Format("inline;filename=PreviewSchedule.pdf"));
                context.Response.BinaryWrite((output as MemoryStream).ToArray());
            }
        }