using System; using System.Collections; using System.Collections.Generic; using iTextSharp.text; using iTextSharp.text.pdf; using Spring.Objects; using Sleis.Utility; namespace Sleis.Pdf { public class ObjectTableWriter : ElementWriter { //public string Select { get; set; } public List Columns { get; set; } public int? BorderWidth { get; set; } public override void Init() { ArgumentValidationUtility.ThrowOnEmpty(Select, "Select"); ArgumentValidationUtility.ThrowOnEmpty(Columns, "Columns"); if (!BorderWidth.HasValue) { BorderWidth = 1; } } public override void Write(Document pdf, ObjectWrapper context) { LOG.Debug("Init Write"); BeforeWrite(pdf, context); //Table object selectionValue = null; try { selectionValue = context.GetPropertyValue(Select); } catch (Exception err) { LOG.Debug(String.Format("Error getting accessing {0} : {1}", Select, err.Message)); return; }//swallowed IEnumerable list = selectionValue as IEnumerable; //Write the table only if there is a content if (list != null) { //Write label Paragraph labelP = new Paragraph(Label, LabelFont); if (SpaceBefore.HasValue) { labelP.SpacingBefore = SpaceBefore.Value; } if (SpaceAfter.HasValue) { labelP.SpacingAfter = SpaceAfter.Value; } pdf.Add(labelP); PdfPTable table = new PdfPTable(Columns.Count); //workaround for WidthPercentage issues table.TotalWidth = pdf.PageSize.Width - pdf.LeftMargin - pdf.RightMargin; table.HorizontalAlignment = Element.ALIGN_LEFT; IEnumerator enumerator = list.GetEnumerator(); //draw headers foreach(IElementWriter writer in Columns) { PdfPCell headerCell = new PdfPCell(); headerCell.AddElement(new Phrase(writer.Label, LabelFont)); headerCell.HorizontalAlignment = PdfPCell.ALIGN_LEFT; headerCell.VerticalAlignment = PdfPCell.ALIGN_TOP; headerCell.BorderColor = new BaseColor(System.Drawing.Color.LightGray); headerCell.BorderWidth = BorderWidth.Value; table.AddCell(headerCell); } while (enumerator.MoveNext()) { object candidateObject = enumerator.Current; LOG.DebugFormat("Validating: {0}", candidateObject); ObjectWrapper row = new ObjectWrapper(candidateObject); foreach (IElementWriter writer in Columns) { //PdfPCell cell = new PdfPCell(new Phrase(element.Make(row), Font)); PdfPCell cell = new PdfPCell(); cell.AddElement(writer.GetElement(new PdfPCell(), row)); cell.HorizontalAlignment = PdfPCell.ALIGN_LEFT; cell.VerticalAlignment = PdfPCell.ALIGN_TOP; cell.BorderColor = new BaseColor(System.Drawing.Color.LightGray); cell.BorderWidth = BorderWidth.Value; table.AddCell(cell); } } //each row item pdf.Add(table); } } public override IElement GetElement(IElement element, ObjectWrapper context) { LOG.Debug("Building sub table"); //Write label Paragraph para = new Paragraph(Label, LabelFont); if (SpaceBefore.HasValue) { para.SpacingBefore = SpaceBefore.Value; } if (SpaceAfter.HasValue) { para.SpacingAfter = SpaceAfter.Value; } //Table object selectionValue = null; try { selectionValue = context.GetPropertyValue(Select); } catch(Exception err) { LOG.Debug(String.Format("Error getting accessing {0} : {1}", Select, err.Message)); }//swallowed IEnumerable list = selectionValue as IEnumerable; //Write the table only if there is a content if (list != null) { PdfPTable table = new PdfPTable(Columns.Count); table.KeepTogether = true; //workaround for WidthPercentage issues table.WidthPercentage = 95; table.HorizontalAlignment = Element.ALIGN_LEFT; IEnumerator enumerator = list.GetEnumerator(); while (enumerator.MoveNext()) { object candidateObject = enumerator.Current; LOG.DebugFormat("Validating: {0}", candidateObject); ObjectWrapper row = new ObjectWrapper(candidateObject); foreach (IElementWriter writer in Columns) { //PdfPCell cell = new PdfPCell(new Phrase(element.Make(row), Font)); PdfPCell cell = new PdfPCell(); cell.AddElement(writer.GetElement(new PdfPCell(), row)); cell.HorizontalAlignment = PdfPCell.ALIGN_LEFT; cell.VerticalAlignment = PdfPCell.ALIGN_TOP; cell.BorderColor = new BaseColor(System.Drawing.Color.LightGray); cell.BorderWidth = BorderWidth.Value; table.AddCell(cell); } } //each row item para.Add(table); //return para; going to return table. maybe issue adding table to para then to table cell? return table; } return null; } } }