private static void ReportBuilder(int shid, int cropYear, int calYear, string logoUrl, FileStream fs) { const string METHOD_NAME = "rptBeetPaymentBreakdown.ReportBuilder: "; Document document = null; PdfWriter writer = null; PdfPTable table = null; BeetPaymentBreakdownEvent pgEvent = null; iTextSharp.text.Image imgLogo = null; int curShid = 0; int lastShid = 0; decimal grossAmt = 0; decimal netAmt = 0; string rptTitle = "Western Sugar Cooperative Beet Payments by Year"; Font headerFont = FontFactory.GetFont("HELVETICA", 8F, Font.NORMAL); Font normalFont = FontFactory.GetFont("HELVETICA", 8F, Font.NORMAL); Font labelFont = FontFactory.GetFont("HELVETICA", 8F, Font.BOLD); try { List <BeetPaymentListItem> stateList = WSCPayment.RptBeetPayBreakdown(ConfigurationManager.ConnectionStrings["BeetConn"].ToString(), shid, cropYear, calYear); foreach (BeetPaymentListItem item in stateList) { curShid = item.SHID; 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); lastShid = curShid; grossAmt = 0; netAmt = 0; // Attach my override event handler(s) pgEvent = new BeetPaymentBreakdownEvent(); pgEvent.FillEvent(_primaryTableLayout, _hdrNames, curShid, item.PayeeName, rptTitle, imgLogo); writer.PageEvent = pgEvent; // Open the document document.Open(); table = PdfReports.CreateTable(_primaryTableLayout, 1); } if (curShid != lastShid) { // BLANK LINE PdfReports.AddText2Table(table, " ", normalFont, "center", 5); PdfReports.AddText2Table(table, grossAmt.ToString("c2"), labelFont, "right"); PdfReports.AddText2Table(table, netAmt.ToString("c2"), labelFont, "right"); PdfReports.AddText2Table(table, " ", normalFont, 13); PdfReports.AddTableNoSplit(document, pgEvent, table); lastShid = curShid; grossAmt = 0; netAmt = 0; pgEvent.FillEvent(_primaryTableLayout, _hdrNames, curShid, item.PayeeName, rptTitle, imgLogo); document.NewPage(); table = PdfReports.CreateTable(_primaryTableLayout, 1); } PdfReports.AddText2Table(table, item.CalendarYear.ToString(), normalFont, "center"); PdfReports.AddText2Table(table, item.CropYear.ToString(), normalFont, "center"); PdfReports.AddText2Table(table, item.PaymentNumber.ToString("N0"), normalFont, "center"); PdfReports.AddText2Table(table, item.PaymentDescription, normalFont, "left"); PdfReports.AddText2Table(table, item.TransmittalDate.ToString("MM/dd/yyyy"), normalFont, "center"); PdfReports.AddText2Table(table, item.GrossDollars.ToString("c2"), normalFont, "right"); PdfReports.AddText2Table(table, item.PaymentAmount.ToString("c2"), normalFont, "right"); grossAmt += item.GrossDollars; netAmt += item.PaymentAmount; } if (document != null) { // BLANK LINE PdfReports.AddText2Table(table, " ", normalFont, "center", 5); PdfReports.AddText2Table(table, grossAmt.ToString("c2"), labelFont, "right"); PdfReports.AddText2Table(table, netAmt.ToString("c2"), labelFont, "right"); PdfReports.AddText2Table(table, " ", normalFont, 13); PdfReports.AddTableNoSplit(document, pgEvent, table); } // ====================================================== // 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(); } } }