public static Cell GetCell(Properties attributes) { Cell cell = new Cell(); String value; cell.SetHorizontalAlignment(attributes[ElementTags.HORIZONTALALIGN]); cell.SetVerticalAlignment(attributes[ElementTags.VERTICALALIGN]); value = attributes[ElementTags.WIDTH]; if (value != null) { cell.SetWidth(value); } value = attributes[ElementTags.COLSPAN]; if (value != null) { cell.Colspan = int.Parse(value); } value = attributes[ElementTags.ROWSPAN]; if (value != null) { cell.Rowspan = int.Parse(value); } value = attributes[ElementTags.LEADING]; if (value != null) { cell.Leading = float.Parse(value, System.Globalization.NumberFormatInfo.InvariantInfo); } cell.Header = Utilities.CheckTrueOrFalse(attributes, ElementTags.HEADER); if (Utilities.CheckTrueOrFalse(attributes, ElementTags.NOWRAP)) { cell.MaxLines = 1; } SetRectangleProperties(cell, attributes); return cell; }
protected void GeneratePDF() { // Refresh the summary grid else there will be nothing to generate (no postback) this.PopulateGrid(); // Create and initialize a new document object iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.LEGAL, 24, 24, 24, 24); document.PageSize.Rotate(); System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); try { // Create an instance of the writer object iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, memoryStream); // Add some meta information to the document Label lblPageTitle = (Label)(this.Page.Master.FindControl("lblDefaultMasterPageTitle")); document.AddAuthor(lblPageTitle.Text); document.AddSubject(this.lblReportTitle.Text); // Open the document document.Open(); // Create a table to match our current summary grid iTextSharp.text.Table table = null; if (this.DateRangeType == DateRangeTypes.UserSpecified) table = new iTextSharp.text.Table(6); else if (this.DateRangeType == DateRangeTypes.Daily) table = new iTextSharp.text.Table(7); else table = new iTextSharp.text.Table(8); table.TableFitsPage = true; // Apply spacing/padding/borders/column widths to the table table.Padding = 2; table.Spacing = 0; table.DefaultCellBorderWidth = 1; if (this.DateRangeType == DateRangeTypes.UserSpecified) { float[] headerwidths1 = { 30, 30, 30, 30, 30, 30 }; table.Widths = headerwidths1; } else if (this.DateRangeType == DateRangeTypes.Daily) { float[] headerwidths2 = { 40, 30, 30, 30, 30, 30, 30 }; table.Widths = headerwidths2; } else { float[] headerwidths3 = { 40, 30, 30, 30, 30, 30, 30, 35 }; table.Widths = headerwidths3; } table.Width = 100; // Add report title spanning all columns iTextSharp.text.Font titleFont = new iTextSharp.text.Font(Font.GetFamilyIndex("Tahoma"), 6, Font.BOLD); titleFont.Color = new iTextSharp.text.Color(System.Drawing.Color.Firebrick); iTextSharp.text.Cell titleCell = new iTextSharp.text.Cell(); titleCell.SetHorizontalAlignment("Left"); titleCell.SetVerticalAlignment("Top"); titleCell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.Color.White); titleCell.BorderWidth = 0; if (this.DateRangeType == DateRangeTypes.UserSpecified) titleCell.Colspan = 6; else if (this.DateRangeType == DateRangeTypes.Daily) titleCell.Colspan = 7; else titleCell.Colspan = 8; titleCell.AddElement(new iTextSharp.text.Phrase(this.lblReportTitle.Text, titleFont)); table.AddCell(titleCell); // Add table headers for (int i = 0; i < this.grdRaveForm.Columns.Count; i++) { iTextSharp.text.Font headerCellFont = new iTextSharp.text.Font(Font.GetFamilyIndex("Tahoma"), 8, Font.NORMAL + Font.UNDERLINE); headerCellFont.Color = new iTextSharp.text.Color(System.Drawing.Color.White); iTextSharp.text.Cell headerCell = new iTextSharp.text.Cell(); headerCell.SetHorizontalAlignment("Left"); headerCell.SetVerticalAlignment("Top"); headerCell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.Color.SteelBlue); headerCell.BorderColor = new iTextSharp.text.Color(System.Drawing.Color.White); headerCell.AddElement(new iTextSharp.text.Phrase(this.grdRaveForm.Columns[i].HeaderText, headerCellFont)); table.AddCell(headerCell); } table.EndHeaders(); // Add data to the table int j = 0; int k = 0; string phrase = ""; foreach (System.Data.DataRow row in this._pdfDataTable.Rows) { j++; // Increment the row counter k = 0; // Reset the column counter for the new row foreach (System.Data.DataColumn col in this._pdfDataTable.Columns) { k++; // Increment the column counter iTextSharp.text.Font cellFont = new iTextSharp.text.Font(Font.GetFamilyIndex("Tahoma"), 6, Font.NORMAL); if (j % 2 == 0) { cellFont.Color = new iTextSharp.text.Color(System.Drawing.Color.DarkRed); } else { cellFont.Color = new iTextSharp.text.Color(System.Drawing.Color.Black); } iTextSharp.text.Cell cell = new iTextSharp.text.Cell(); cell.SetHorizontalAlignment("Left"); cell.SetVerticalAlignment("Top"); cell.BorderColor = new iTextSharp.text.Color(System.Drawing.Color.White); if (j % 2 == 0) { cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.Color.LightGray); } else { cell.BackgroundColor = new iTextSharp.text.Color(System.Drawing.Color.White); } // Generate formatted phrase for cell. switch (col.ColumnName) { case "SummaryDate": case "MaxDate": phrase = String.Format("{0,10:MM/dd/yyyy}", row[col]); break; case "AvgSecs": case "StdDevSecs": phrase = String.Format("{0:F2}", row[col]); break; case "FormIDCount": phrase = String.Format("{0:G}", row[col]); break; default: phrase = row[col].ToString(); break; } cell.AddElement(new iTextSharp.text.Phrase(phrase, cellFont)); table.AddCell(cell); } } // Add the table to the document document.Add(table); // Close the document document.Close(); // Show the document Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=" + Enum.GetName(typeof(DateRangeTypes), this.DateRangeType) + "Performance" + ((DropDownList)this.Page.Master.FindControl("ddlReport")).SelectedItem.Text.Replace(" ", String.Empty) + ".pdf"); Response.ContentType = "application/pdf"; Response.BinaryWrite(memoryStream.ToArray()); Response.End(); } catch (Exception xcptn) { Response.Write(xcptn.Message); } }