private static void ReportBuilder(int cropYear, ArrayList shidList, string busName, ArrayList growerPerformanceList, ArrayList regionCodeList, ArrayList areaCodeList, ArrayList regionNameList, ArrayList areaNameList, string logoUrl, string filePath, System.IO.FileStream fs) { const string METHOD_NAME = "ReportBuilder: "; const string CharBlank = " "; const string CharAffirm = "X"; Document document = null; PdfWriter writer = null; PdfPTable table = null; ShareholderSummaryEvent pgEvent = null; iTextSharp.text.Image imgLogo = null; int iShid = 0; string areaCode = ""; string regionCode = ""; string regionName = ""; string areaName = ""; int growerPerformanceID = 0; string rptTitle = "Western Sugar Cooperative\nShareholder Summary for " + cropYear.ToString() + " Crop Year"; string okFertility = ""; string okIrrigation = ""; string okStand = ""; string okWeed = ""; string okDisease = ""; string okVariety = ""; string descFertility = ""; string descIrrigation = ""; string descStand = ""; string descWeed = ""; string descDisease = ""; string descVariety = ""; // Build the contract information. try { for (int j = 0; j < shidList.Count; j++) { string shid = shidList[j].ToString(); iShid = Convert.ToInt32(shid); if (growerPerformanceList.Count == 0) { busName = ""; using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BeetConn"].ToString())) { using (SqlDataReader dr = WSCField.SharholderSummaryGetAreas(conn, cropYear, iShid)) { while (dr.Read()) { growerPerformanceList.Add(dr["GrowerPerformanceID"].ToString()); regionCodeList.Add(dr["RegionCode"].ToString()); areaCodeList.Add(dr["AreaCode"].ToString()); regionNameList.Add(dr["RegionName"].ToString()); areaNameList.Add(dr["AreaName"].ToString()); if (busName.Length == 0) { busName = dr["BusName"].ToString(); } } } } } // --------------------------------------------------------------------------------------------------------- // Given all of the pieces, crop year, shid, growerPerformanceID, region, and area, generate the report // --------------------------------------------------------------------------------------------------------- if (areaCodeList.Count > 0) { for (int i = 0; i < areaCodeList.Count; i++) { growerPerformanceID = Convert.ToInt32(growerPerformanceList[i]); regionCode = regionCodeList[i].ToString(); areaCode = areaCodeList[i].ToString(); regionName = regionNameList[i].ToString(); areaName = areaNameList[i].ToString(); // ------------------------------------------------ // Collect the data: Get the report card. // ------------------------------------------------ using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BeetConn"].ToString())) { using (SqlDataReader dr = WSCField.GrowerAdviceGetBySHID(conn, growerPerformanceID)) { if (dr.Read()) { //busName = dr["gadBusinessName"].ToString(); okFertility = dr["gadGoodFertilityManagement"].ToString(); okIrrigation = dr["gadGoodIrrigationManagement"].ToString(); okStand = dr["gadGoodStandEstablishment"].ToString(); okWeed = dr["gadGoodWeedControl"].ToString(); okDisease = dr["gadGoodDiseaseControl"].ToString(); okVariety = dr["gadGoodVarietySelection"].ToString(); descFertility = dr["gadTextFertilityManagement"].ToString(); descIrrigation = dr["gadTextIrrigationManagement"].ToString(); descStand = dr["gadTextStandEstablishment"].ToString(); descWeed = dr["gadTextWeedControl"].ToString(); descDisease = dr["gadTextDiseaseControl"].ToString(); descVariety = dr["gadTextVarietySelection"].ToString(); } else { //busName = ""; okFertility = ""; okIrrigation = ""; okStand = ""; okWeed = ""; okDisease = ""; okVariety = ""; descFertility = ""; descIrrigation = ""; descStand = ""; descWeed = ""; descDisease = ""; descVariety = ""; } } } if (document == null) { // IF YOU CHANGE MARGINS, CHANGE YOUR TABLE LAYOUTS !!! // *** US LETTER: 612 X 792 *** //document = new Document(iTextSharp.text.PageSize.LETTER, 36, 36, 54, 72); 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, 127, 50, iTextSharp.text.Element.ALIGN_CENTER); // Attach my override event handler(s) //busName = dr["Business Name"].ToString(); pgEvent = new ShareholderSummaryEvent(); pgEvent.FillEvent(cropYear, shid, busName, rptTitle, regionName, areaName, imgLogo); writer.PageEvent = pgEvent; // Open the document document.Open(); } else { // everytime thru kick out a new page because we're on a different shid/region/area combination. pgEvent.FillEvent(cropYear, shid, busName, rptTitle, regionName, areaName, imgLogo); document.NewPage(); } // ----------------------------------------------------- // Create the report card // ----------------------------------------------------- table = PdfReports.CreateTable(_adviceTableLayout, 1); Color borderColor = Color.BLACK; //new Color(255, 0, 0); float borderWidth = 1.0F; int borderTypeAll = Rectangle.TOP_BORDER | Rectangle.RIGHT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.LEFT_BORDER; float fPadding = 3; // HEADER iTextSharp.text.pdf.PdfPCell cell = PdfReports.AddText2Cell("Okay", _labelFont, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_BOTTOM, fPadding, borderWidth, borderTypeAll, borderColor); table.AddCell(cell); cell = PdfReports.AddText2Cell("Opportunity\nfor\nImprovement", _labelFont, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_BOTTOM, fPadding, borderWidth, borderTypeAll, borderColor); table.AddCell(cell); cell = PdfReports.AddText2Cell("Big Six Growing Practices", _labelFont, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_BOTTOM, fPadding, borderWidth, borderTypeAll, borderColor); table.AddCell(cell); /// ---------------------------------------- // TBODY /// ---------------------------------------- // Fertility cell = PdfReports.AddText2Cell((okFertility == "Y" ? CharAffirm : CharBlank), _labelFont, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_BOTTOM, fPadding, borderWidth, borderTypeAll, borderColor); table.AddCell(cell); cell = PdfReports.AddText2Cell((okFertility == "N" ? CharAffirm : CharBlank), _labelFont, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_BOTTOM, fPadding, borderWidth, borderTypeAll, borderColor); table.AddCell(cell); cell = PdfReports.AddText2Cell("Fertility Management", _labelFont, PdfPCell.ALIGN_LEFT, PdfPCell.ALIGN_BOTTOM, fPadding, borderWidth, borderTypeAll, borderColor); table.AddCell(cell); // Irrigation cell = PdfReports.AddText2Cell((okIrrigation == "Y" ? CharAffirm : CharBlank), _labelFont, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_BOTTOM, fPadding, borderWidth, borderTypeAll, borderColor); table.AddCell(cell); cell = PdfReports.AddText2Cell((okIrrigation == "N" ? CharAffirm : CharBlank), _labelFont, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_BOTTOM, fPadding, borderWidth, borderTypeAll, borderColor); table.AddCell(cell); cell = PdfReports.AddText2Cell("Irrigation Water Management", _labelFont, PdfPCell.ALIGN_LEFT, PdfPCell.ALIGN_BOTTOM, fPadding, borderWidth, borderTypeAll, borderColor); table.AddCell(cell); // Stand cell = PdfReports.AddText2Cell((okStand == "Y" ? CharAffirm : CharBlank), _labelFont, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_BOTTOM, fPadding, borderWidth, borderTypeAll, borderColor); table.AddCell(cell); cell = PdfReports.AddText2Cell((okStand == "N" ? CharAffirm : CharBlank), _labelFont, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_BOTTOM, fPadding, borderWidth, borderTypeAll, borderColor); table.AddCell(cell); cell = PdfReports.AddText2Cell("Stand Establishment (Harvest Plant Population)", _labelFont, PdfPCell.ALIGN_LEFT, PdfPCell.ALIGN_BOTTOM, fPadding, borderWidth, borderTypeAll, borderColor); table.AddCell(cell); // Weed cell = PdfReports.AddText2Cell((okWeed == "Y" ? CharAffirm : CharBlank), _labelFont, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_BOTTOM, fPadding, borderWidth, borderTypeAll, borderColor); table.AddCell(cell); cell = PdfReports.AddText2Cell((okWeed == "N" ? CharAffirm : CharBlank), _labelFont, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_BOTTOM, fPadding, borderWidth, borderTypeAll, borderColor); table.AddCell(cell); cell = PdfReports.AddText2Cell("Weed Control", _labelFont, PdfPCell.ALIGN_LEFT, PdfPCell.ALIGN_BOTTOM, fPadding, borderWidth, borderTypeAll, borderColor); table.AddCell(cell); // Disease cell = PdfReports.AddText2Cell((okDisease == "Y" ? CharAffirm : CharBlank), _labelFont, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_BOTTOM, fPadding, borderWidth, borderTypeAll, borderColor); table.AddCell(cell); cell = PdfReports.AddText2Cell((okDisease == "N" ? CharAffirm : CharBlank), _labelFont, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_BOTTOM, fPadding, borderWidth, borderTypeAll, borderColor); table.AddCell(cell); cell = PdfReports.AddText2Cell("Disease & Insect Control", _labelFont, PdfPCell.ALIGN_LEFT, PdfPCell.ALIGN_BOTTOM, fPadding, borderWidth, borderTypeAll, borderColor); table.AddCell(cell); // Varitey cell = PdfReports.AddText2Cell((okVariety == "Y" ? CharAffirm : CharBlank), _labelFont, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_BOTTOM, fPadding, borderWidth, borderTypeAll, borderColor); table.AddCell(cell); cell = PdfReports.AddText2Cell((okVariety == "N" ? CharAffirm : CharBlank), _labelFont, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_BOTTOM, fPadding, borderWidth, borderTypeAll, borderColor); table.AddCell(cell); cell = PdfReports.AddText2Cell("Proper Variety Selection", _labelFont, PdfPCell.ALIGN_LEFT, PdfPCell.ALIGN_BOTTOM, fPadding, borderWidth, borderTypeAll, borderColor); table.AddCell(cell); PdfReports.AddText2Table(table, " ", _normalFont, 3); PdfReports.AddTableNoSplit(document, pgEvent, table); // ========================================================== // Recommendations for Improvement. // ========================================================== table = PdfReports.CreateTable(_adviceTableLayout, 1); // Caption iTextSharp.text.Phrase phrase = new iTextSharp.text.Phrase("Recommendations for Improvement", _labelFont); cell = new iTextSharp.text.pdf.PdfPCell(phrase); cell.Colspan = 3; cell.HorizontalAlignment = PdfPCell.ALIGN_LEFT; cell.VerticalAlignment = PdfPCell.ALIGN_BOTTOM; cell.Padding = fPadding; cell.BorderWidth = borderWidth; cell.Border = Rectangle.TOP_BORDER | Rectangle.RIGHT_BORDER | Rectangle.LEFT_BORDER; cell.BorderColor = borderColor; table.AddCell(cell); // Content phrase = new iTextSharp.text.Phrase((descFertility.Length > 0 ? descFertility + "\n\n" : "") + (descIrrigation.Length > 0 ? descIrrigation + "\n\n" : "") + (descStand.Length > 0 ? descStand + "\n\n" : "") + (descWeed.Length > 0 ? descWeed + "\n\n" : "") + (descDisease.Length > 0 ? descDisease + "\n\n" : "") + (descVariety.Length > 0 ? descVariety + "\n\n" : ""), _normalFont); cell = new iTextSharp.text.pdf.PdfPCell(phrase); cell.Colspan = 3; cell.HorizontalAlignment = PdfPCell.ALIGN_LEFT; cell.VerticalAlignment = PdfPCell.ALIGN_BOTTOM; cell.Padding = fPadding; cell.BorderWidth = borderWidth; cell.Border = Rectangle.RIGHT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.LEFT_BORDER; cell.BorderColor = borderColor; table.AddCell(cell); PdfReports.AddText2Table(table, " ", _normalFont, table.NumberOfColumns); PdfReports.AddText2Table(table, " ", _normalFont, table.NumberOfColumns); PdfReports.AddTableNoSplit(document, pgEvent, table); // ------------------------------------------------ // Create the contract dump. // ------------------------------------------------ ArrayList cntPerfs; using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BeetConn"].ToString())) { cntPerfs = WSCField.ShareholderSummaryContracts(conn, iShid, cropYear, regionCode, areaCode); } // ======================================= // HEADER // ======================================= table = PdfReports.CreateTable(_contractTableLayout, 0); pgEvent.BuildContractDumpHeader(document, _contractTableLayout); pgEvent.ContractTableLayout = _contractTableLayout; // DATA for (int k = 0; k < cntPerfs.Count; k++) { ContractPerformanceState perf = (ContractPerformanceState)cntPerfs[k]; switch (perf.RowType) { case 1: table = PdfReports.CreateTable(_contractTableLayout, 0); PdfReports.AddText2Table(table, perf.ContractNumber, _normalFont, "center"); PdfReports.AddText2Table(table, perf.ContractStation, _normalFont); PdfReports.AddText2Table(table, perf.FieldDescription, _normalFont); PdfReports.AddText2Table(table, perf.LandownerName, _normalFont); PdfReports.AddText2Table(table, perf.HarvestFinalNetTons, _normalFont, "right"); PdfReports.AddText2Table(table, perf.TonsPerAcre, _normalFont, "center"); PdfReports.AddText2Table(table, perf.HarvestSugarPct, _normalFont, "center"); PdfReports.AddText2Table(table, perf.HarvestTarePct, _normalFont, "center"); PdfReports.AddText2Table(table, perf.HarvestSLMPct, _normalFont, "center"); PdfReports.AddText2Table(table, perf.HarvestExtractableSugar, _normalFont, "center"); PdfReports.AddText2Table(table, perf.BeetsPerAcre, _normalFont, "center"); PdfReports.AddTableNoSplit(document, pgEvent, table); break; case 2: table = PdfReports.CreateTable(_contractTableLayout, 0); PdfReports.AddText2Table(table, " ", _normalFont, _contractTableLayout.Length); PdfReports.AddText2Table(table, " ", _normalFont); PdfReports.AddText2Table(table, "Overall Average", _labelFont, 3); PdfReports.AddText2Table(table, perf.HarvestFinalNetTons, _normalFont, "right"); PdfReports.AddText2Table(table, perf.TonsPerAcre, _normalFont, "center"); PdfReports.AddText2Table(table, perf.HarvestSugarPct, _normalFont, "center"); PdfReports.AddText2Table(table, perf.HarvestTarePct, _normalFont, "center"); PdfReports.AddText2Table(table, perf.HarvestSLMPct, _normalFont, "center"); PdfReports.AddText2Table(table, perf.HarvestExtractableSugar, _normalFont, "center"); PdfReports.AddText2Table(table, perf.BeetsPerAcre, _normalFont, "center"); break; case 3: PdfReports.AddText2Table(table, " ", _normalFont); PdfReports.AddText2Table(table, "Top 20% Area Average", _labelFont, 4); PdfReports.AddText2Table(table, perf.TonsPerAcre, _normalFont, "center"); PdfReports.AddText2Table(table, perf.HarvestSugarPct, _normalFont, "center"); PdfReports.AddText2Table(table, perf.HarvestTarePct, _normalFont, "center"); PdfReports.AddText2Table(table, perf.HarvestSLMPct, _normalFont, "center"); PdfReports.AddText2Table(table, perf.HarvestExtractableSugar, _normalFont, "center"); PdfReports.AddText2Table(table, perf.BeetsPerAcre, _normalFont, "center"); break; case 4: PdfReports.AddText2Table(table, " ", _normalFont); PdfReports.AddText2Table(table, "Your Rankings", _labelFont, 10); PdfReports.AddText2Table(table, " ", _normalFont); PdfReports.AddText2Table(table, areaName, _labelFont, 4); PdfReports.AddText2Table(table, perf.TonsPerAcre, _normalFont, "center"); PdfReports.AddText2Table(table, perf.HarvestSugarPct, _normalFont, "center"); PdfReports.AddText2Table(table, perf.HarvestTarePct, _normalFont, "center"); PdfReports.AddText2Table(table, perf.HarvestSLMPct, _normalFont, "center"); PdfReports.AddText2Table(table, perf.HarvestExtractableSugar, _normalFont, "center"); PdfReports.AddText2Table(table, perf.BeetsPerAcre, _normalFont, "center"); break; case 5: PdfReports.AddText2Table(table, " ", _normalFont); PdfReports.AddText2Table(table, regionName, _labelFont, 4); PdfReports.AddText2Table(table, perf.TonsPerAcre, _normalFont, "center"); PdfReports.AddText2Table(table, perf.HarvestSugarPct, _normalFont, "center"); PdfReports.AddText2Table(table, perf.HarvestTarePct, _normalFont, "center"); PdfReports.AddText2Table(table, perf.HarvestSLMPct, _normalFont, "center"); PdfReports.AddText2Table(table, perf.HarvestExtractableSugar, _normalFont, "center"); PdfReports.AddText2Table(table, perf.BeetsPerAcre, _normalFont, "center"); PdfReports.AddTableNoSplit(document, pgEvent, table); break; } } pgEvent.ContractTableLayout = null; } } // -------------------------------------------- // -------- reset for next iteration -------- // -------------------------------------------- growerPerformanceList.Clear(); regionCodeList.Clear(); areaCodeList.Clear(); regionNameList.Clear(); areaNameList.Clear(); } // ====================================================== // 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(MOD_NAME + METHOD_NAME + errMsg, ex); throw (wscex); } finally { if (document != null) { pgEvent.IsDocumentClosing = true; document.Close(); } if (writer != null) { writer.Close(); } } }