private static void ReportBuilder(int cropYear, DateTime fromDate, DateTime toDate, string shid, string filePath, string logoUrl, System.IO.FileStream fs)
        {
            const string METHOD_NAME = "ReportBuilder: ";

            string lastPayee = "";
            decimal subTotalPayAmt;
            decimal subTotalNinetySixPct;
            decimal subTotalFirstNetTons;
            string rptTitle = cropYear.ToString() + " Direct Deliveries";

            Document document = null;
            PdfWriter writer = null;
            PdfPTable detailTable = null;
            PdfPTable bodyTable = null;
            iTextSharp.text.Image imgLogo = null;
            DirectDeliveryStatementEvent pgEvent = null;

            try {

            List<ListDirectDeliveryStatementItem> stateList = RptDirectDeliveryStatementData(cropYear, fromDate, toDate, shid);

                for (int i = 0; i < stateList.Count; i++) {

                    ListDirectDeliveryStatementItem state = stateList[i];

                    if (document == null) {

                        lastPayee = state.Payee;
                        CalcSubTotals(stateList, i, out subTotalFirstNetTons, out subTotalNinetySixPct, out subTotalPayAmt);

                        // IF YOU CHANGE MARGINS, CHANGE YOUR TABLE LAYOUTS !!!
                        //  ***  US LETTER: 612 X 792  ***
                        document = new Document(PortraitPageSize.PgPageSize, PortraitPageSize.PgLeftMargin,
                            PortraitPageSize.PgRightMargin, PortraitPageSize.PgTopMargin, PortraitPageSize.PgBottomMargin);

                        // we create a writer that listens to the document
                        // and directs a PDF-stream to a file
                        writer = PdfWriter.GetInstance(document, fs);

                        imgLogo = PdfReports.GetImage(logoUrl, 77, 45, iTextSharp.text.Element.ALIGN_CENTER);

                        // Attach my override event handler(s)
                        pgEvent = new DirectDeliveryStatementEvent();
                        pgEvent.FillEvent(state.Shid, state.BusinessName, state.AddrLine1, state.AddrLine2 , state.CSZ, subTotalPayAmt, 0, rptTitle, imgLogo);
                        writer.PageEvent = pgEvent;

                        // Open the document
                        document.Open();

                        bodyTable = PdfReports.CreateTable(_bodyTableLayout, 0);
                        detailTable = PdfReports.CreateTable(_detailTableLayout, 0);

                        AddBodyText(bodyTable, cropYear, fromDate.ToString(HEADER_DATE_FORMAT), toDate.ToString(HEADER_DATE_FORMAT), state.Payee);
                        AddDetailTable(detailTable, stateList, i, subTotalFirstNetTons, subTotalNinetySixPct, subTotalPayAmt);

                        PdfReports.AddTableNoSplit(document, pgEvent, bodyTable);
                        PdfReports.AddTableNoSplit(document, pgEvent, detailTable);
                    }

                    if (lastPayee != state.Payee) {

                        //-------------------------------------------------------------------------------------
                        // When you change members, kick out the page and move on to the next member,
                        // and reset flags.
                        //-------------------------------------------------------------------------------------

                        lastPayee = state.Payee;

                        bodyTable = PdfReports.CreateTable(_bodyTableLayout, 0);
                        detailTable = PdfReports.CreateTable(_detailTableLayout, 0);

                        CalcSubTotals(stateList, i, out subTotalFirstNetTons, out subTotalNinetySixPct, out subTotalPayAmt);

                        pgEvent.FillEvent(state.Shid, state.BusinessName, state.AddrLine1, state.AddrLine2, state.CSZ, subTotalPayAmt, 0, rptTitle, imgLogo);
                        document.NewPage();

                        AddBodyText(bodyTable, cropYear, fromDate.ToString(HEADER_DATE_FORMAT), toDate.ToString(HEADER_DATE_FORMAT), state.Payee);
                        AddDetailTable(detailTable, stateList, i, subTotalFirstNetTons, subTotalNinetySixPct, subTotalPayAmt);

                        PdfReports.AddTableNoSplit(document, pgEvent, bodyTable);
                        PdfReports.AddTableNoSplit(document, pgEvent, detailTable);
                    }
                }

            // ======================================================
            // Close document
            // ======================================================
            if (document != null) {

                pgEvent.IsDocumentClosing = true;
                document.Close();
                document = null;
            }
            if (writer == null) {
                // Warn that we have no data.
                WSCIEMP.Common.CWarning warn = new WSCIEMP.Common.CWarning("No records matched your report criteria.");
                throw (warn);
            }
            }
            catch (Exception ex) {
                string errMsg = "document is null: " + (document == null).ToString() + "; " +
                    "writer is null: " + (writer == null).ToString();
                WSCIEMP.Common.CException wscex = new WSCIEMP.Common.CException(METHOD_NAME + errMsg, ex);
                throw (wscex);
            }
            finally {

                if (document != null) {
                    pgEvent.IsDocumentClosing = true;
                    document.Close();
                }
                if (writer != null) {
                    writer.Close();
                }
            }
        }
        private static void ReportBuilder(int cropYear, DateTime fromDate, DateTime toDate, string shid, string filePath, string logoUrl, System.IO.FileStream fs)
        {
            const string METHOD_NAME = "ReportBuilder: ";

            string  lastPayee = "";
            decimal subTotalPayAmt;
            decimal subTotalNinetySixPct;
            decimal subTotalFirstNetTons;
            string  rptTitle = cropYear.ToString() + " Direct Deliveries";

            Document  document    = null;
            PdfWriter writer      = null;
            PdfPTable detailTable = null;
            PdfPTable bodyTable   = null;

            iTextSharp.text.Image        imgLogo = null;
            DirectDeliveryStatementEvent pgEvent = null;

            try {
                List <ListDirectDeliveryStatementItem> stateList = RptDirectDeliveryStatementData(cropYear, fromDate, toDate, shid);

                for (int i = 0; i < stateList.Count; i++)
                {
                    ListDirectDeliveryStatementItem state = stateList[i];

                    if (document == null)
                    {
                        lastPayee = state.Payee;
                        CalcSubTotals(stateList, i, out subTotalFirstNetTons, out subTotalNinetySixPct, out subTotalPayAmt);

                        // IF YOU CHANGE MARGINS, CHANGE YOUR TABLE LAYOUTS !!!
                        //  ***  US LETTER: 612 X 792  ***
                        document = new Document(PortraitPageSize.PgPageSize, PortraitPageSize.PgLeftMargin,
                                                PortraitPageSize.PgRightMargin, PortraitPageSize.PgTopMargin, PortraitPageSize.PgBottomMargin);

                        // we create a writer that listens to the document
                        // and directs a PDF-stream to a file
                        writer = PdfWriter.GetInstance(document, fs);

                        imgLogo = PdfReports.GetImage(logoUrl, 77, 45, iTextSharp.text.Element.ALIGN_CENTER);

                        // Attach my override event handler(s)
                        pgEvent = new DirectDeliveryStatementEvent();
                        pgEvent.FillEvent(state.Shid, state.BusinessName, state.AddrLine1, state.AddrLine2, state.CSZ, subTotalPayAmt, 0, rptTitle, imgLogo);
                        writer.PageEvent = pgEvent;

                        // Open the document
                        document.Open();

                        bodyTable   = PdfReports.CreateTable(_bodyTableLayout, 0);
                        detailTable = PdfReports.CreateTable(_detailTableLayout, 0);

                        AddBodyText(bodyTable, cropYear, fromDate.ToString(HEADER_DATE_FORMAT), toDate.ToString(HEADER_DATE_FORMAT), state.Payee);
                        AddDetailTable(detailTable, stateList, i, subTotalFirstNetTons, subTotalNinetySixPct, subTotalPayAmt);

                        PdfReports.AddTableNoSplit(document, pgEvent, bodyTable);
                        PdfReports.AddTableNoSplit(document, pgEvent, detailTable);
                    }


                    if (lastPayee != state.Payee)
                    {
                        //-------------------------------------------------------------------------------------
                        // When you change members, kick out the page and move on to the next member,
                        // and reset flags.
                        //-------------------------------------------------------------------------------------

                        lastPayee = state.Payee;

                        bodyTable   = PdfReports.CreateTable(_bodyTableLayout, 0);
                        detailTable = PdfReports.CreateTable(_detailTableLayout, 0);

                        CalcSubTotals(stateList, i, out subTotalFirstNetTons, out subTotalNinetySixPct, out subTotalPayAmt);

                        pgEvent.FillEvent(state.Shid, state.BusinessName, state.AddrLine1, state.AddrLine2, state.CSZ, subTotalPayAmt, 0, rptTitle, imgLogo);
                        document.NewPage();

                        AddBodyText(bodyTable, cropYear, fromDate.ToString(HEADER_DATE_FORMAT), toDate.ToString(HEADER_DATE_FORMAT), state.Payee);
                        AddDetailTable(detailTable, stateList, i, subTotalFirstNetTons, subTotalNinetySixPct, subTotalPayAmt);

                        PdfReports.AddTableNoSplit(document, pgEvent, bodyTable);
                        PdfReports.AddTableNoSplit(document, pgEvent, detailTable);
                    }
                }

                // ======================================================
                // Close document
                // ======================================================
                if (document != null)
                {
                    pgEvent.IsDocumentClosing = true;
                    document.Close();
                    document = null;
                }
                if (writer == null)
                {
                    // Warn that we have no data.
                    WSCIEMP.Common.CWarning warn = new WSCIEMP.Common.CWarning("No records matched your report criteria.");
                    throw (warn);
                }
            }
            catch (Exception ex) {
                string errMsg = "document is null: " + (document == null).ToString() + "; " +
                                "writer is null: " + (writer == null).ToString();
                WSCIEMP.Common.CException wscex = new WSCIEMP.Common.CException(METHOD_NAME + errMsg, ex);
                throw (wscex);
            }
            finally {
                if (document != null)
                {
                    pgEvent.IsDocumentClosing = true;
                    document.Close();
                }
                if (writer != null)
                {
                    writer.Close();
                }
            }
        }