Exemplo n.º 1
0
        private static void MakePDF(SqlDataReader dr, int cropYear, int shid, System.IO.FileStream fs)
        {
            // bus    sta    trk    cnt    date   ycard  inWt   OutWt  dtWt   Left BDirt  ADirt  BTons  ATons
            float[] _primaryTableLayout = new float[] { 80.5F, 77.0F, 30F, 37.5F, 48.5F, 42.5F, 51.5F, 51.0F, 49.0F, 22F, 57.5F, 57.0F, 58.0F, 58.0F };  // Total = 720

            const string METHOD_NAME = "ReportBuilder";
            Document document = null;
            PdfWriter writer = null;
            TonsByTruckByContractEvent pgEvent = null;

            string[] columnNames = new string[_primaryTableLayout.Length];
            string rptTitle = "Western Sugar Cooperative\nTons By Truck By Contract";
            PdfPTable table = null;

            try {

                string busName = "";

                while (dr.Read()) {

                    if (document == null) {

                        // IF YOU CHANGE MARGINS, CHANGE YOUR TABLE LAYOUTS !!!
                        //  ***  US LETTER: 612 X 792  ***
                        //  ***  LANDSCAPE			   ***
                        //document = new Document(iTextSharp.text.PageSize.LETTER.Rotate(), 36, 36, 54, 72);	// may have to adjust margins?
                        document = new Document(LandscapePageSize.PgPageSize, LandscapePageSize.PgLeftMargin,
                            LandscapePageSize.PgRightMargin, LandscapePageSize.PgTopMargin, LandscapePageSize.PgBottomMargin);

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

                        // Column Names: Drive getting the column names by the size of the receiving array.
                        // the recordset may contain additional columns that work for the excel csv format, but not here.
                        for (int i = 0; i < columnNames.Length; i++) {
                            columnNames[i] = dr.GetName(i);
                        }

                        // Attach my override event handler(s)
                        busName = dr["Business Name"].ToString();
                        pgEvent = new TonsByTruckByContractEvent();
                        pgEvent.FillEvent(cropYear, shid, busName, rptTitle, columnNames, _primaryTableLayout);

                        writer.PageEvent = pgEvent;

                        // Open the document
                        document.Open();

                    }

                    //==========================================================
                    // Add data to the pdf
                    //==========================================================
                    table = PdfReports.CreateTable(_primaryTableLayout, 0);

                    string s;
                    PdfReports.AddText2Table(table, dr.GetString(dr.GetOrdinal("Business Name")), _normalFont);
                    PdfReports.AddText2Table(table, dr.GetString(dr.GetOrdinal("Station")), _normalFont);

                    s = dr.GetString(dr.GetOrdinal("Truck"));
                    if (s.IndexOf("Total") != -1) {
                        PdfReports.AddText2Table(table, dr.GetString(dr.GetOrdinal("Truck")), _normalFont, 2);
                    } else {
                        PdfReports.AddText2Table(table, dr.GetString(dr.GetOrdinal("Truck")), _normalFont);
                        PdfReports.AddText2Table(table, dr.GetString(dr.GetOrdinal("Contract")), _normalFont, "center");
                    }

                    PdfReports.AddText2Table(table, dr.GetString(dr.GetOrdinal("Date")), _normalFont, "center");
                    PdfReports.AddText2Table(table, dr.GetString(dr.GetOrdinal("Yard Card")), _normalFont, "center");
                    PdfReports.AddText2Table(table, dr.GetInt32(dr.GetOrdinal("In Wt")).ToString(), _normalFont, "right");
                    PdfReports.AddText2Table(table, dr.GetInt32(dr.GetOrdinal("Out Wt")).ToString(), _normalFont, "right");
                    PdfReports.AddText2Table(table, dr.GetInt32(dr.GetOrdinal("Dirt Wt")).ToString(), _normalFont, "right");
                    PdfReports.AddText2Table(table, dr.GetString(dr.GetOrdinal("Left")), _normalFont, "center");
                    PdfReports.AddText2Table(table, dr.GetInt32(dr.GetOrdinal("Before Dirt")).ToString(), _normalFont, "right");
                    PdfReports.AddText2Table(table, dr.GetInt32(dr.GetOrdinal("After Dirt")).ToString(), _normalFont, "right");
                    PdfReports.AddText2Table(table, dr.GetDecimal(dr.GetOrdinal("Before Tons")).ToString(), _normalFont, "right");
                    PdfReports.AddText2Table(table, dr.GetDecimal(dr.GetOrdinal("After Tons")).ToString(), _normalFont, "right");

                    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();
                }
            }
        }
Exemplo n.º 2
0
        private static void MakePDF(SqlDataReader dr, int cropYear, int shid, System.IO.FileStream fs) {

            // bus    sta    trk    cnt    date   ycard  inWt   OutWt  dtWt   Left BDirt  ADirt  BTons  ATons
            float[] _primaryTableLayout = new float[] { 80.5F, 77.0F, 30F, 37.5F, 48.5F, 42.5F, 51.5F, 51.0F, 49.0F, 22F, 57.5F, 57.0F, 58.0F, 58.0F };  // Total = 720			

            const string METHOD_NAME = "ReportBuilder";
            Document document = null;
            PdfWriter writer = null;
            TonsByTruckByContractEvent pgEvent = null;

            string[] columnNames = new string[_primaryTableLayout.Length];
            string rptTitle = "Western Sugar Cooperative\nTons By Truck By Contract";
            PdfPTable table = null;

            try {

                string busName = "";

                while (dr.Read()) {

                    if (document == null) {

                        // IF YOU CHANGE MARGINS, CHANGE YOUR TABLE LAYOUTS !!!
                        //  ***  US LETTER: 612 X 792  ***
                        //  ***  LANDSCAPE			   ***  
                        //document = new Document(iTextSharp.text.PageSize.LETTER.Rotate(), 36, 36, 54, 72);	// may have to adjust margins?
                        document = new Document(LandscapePageSize.PgPageSize, LandscapePageSize.PgLeftMargin,
                            LandscapePageSize.PgRightMargin, LandscapePageSize.PgTopMargin, LandscapePageSize.PgBottomMargin);

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

                        // Column Names: Drive getting the column names by the size of the receiving array.
                        // the recordset may contain additional columns that work for the excel csv format, but not here.
                        for (int i = 0; i < columnNames.Length; i++) {
                            columnNames[i] = dr.GetName(i);
                        }

                        // Attach my override event handler(s)
                        busName = dr["Business Name"].ToString();
                        pgEvent = new TonsByTruckByContractEvent();
                        pgEvent.FillEvent(cropYear, shid, busName, rptTitle, columnNames, _primaryTableLayout);

                        writer.PageEvent = pgEvent;

                        // Open the document
                        document.Open();

                    }

                    //==========================================================
                    // Add data to the pdf
                    //==========================================================
                    table = PdfReports.CreateTable(_primaryTableLayout, 0);

                    string s;
                    PdfReports.AddText2Table(table, dr.GetString(dr.GetOrdinal("Business Name")), _normalFont);
                    PdfReports.AddText2Table(table, dr.GetString(dr.GetOrdinal("Station")), _normalFont);

                    s = dr.GetString(dr.GetOrdinal("Truck"));
                    if (s.IndexOf("Total") != -1) {
                        PdfReports.AddText2Table(table, dr.GetString(dr.GetOrdinal("Truck")), _normalFont, 2);
                    } else {
                        PdfReports.AddText2Table(table, dr.GetString(dr.GetOrdinal("Truck")), _normalFont);
                        PdfReports.AddText2Table(table, dr.GetString(dr.GetOrdinal("Contract")), _normalFont, "center");
                    }

                    PdfReports.AddText2Table(table, dr.GetString(dr.GetOrdinal("Date")), _normalFont, "center");
                    PdfReports.AddText2Table(table, dr.GetString(dr.GetOrdinal("Yard Card")), _normalFont, "center");
                    PdfReports.AddText2Table(table, dr.GetInt32(dr.GetOrdinal("In Wt")).ToString(), _normalFont, "right");
                    PdfReports.AddText2Table(table, dr.GetInt32(dr.GetOrdinal("Out Wt")).ToString(), _normalFont, "right");
                    PdfReports.AddText2Table(table, dr.GetInt32(dr.GetOrdinal("Dirt Wt")).ToString(), _normalFont, "right");
                    PdfReports.AddText2Table(table, dr.GetString(dr.GetOrdinal("Left")), _normalFont, "center");
                    PdfReports.AddText2Table(table, dr.GetInt32(dr.GetOrdinal("Before Dirt")).ToString(), _normalFont, "right");
                    PdfReports.AddText2Table(table, dr.GetInt32(dr.GetOrdinal("After Dirt")).ToString(), _normalFont, "right");
                    PdfReports.AddText2Table(table, dr.GetDecimal(dr.GetOrdinal("Before Tons")).ToString(), _normalFont, "right");
                    PdfReports.AddText2Table(table, dr.GetDecimal(dr.GetOrdinal("After Tons")).ToString(), _normalFont, "right");

                    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();
                }
            }
        }