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 ObjectColumnWriter : ElementWriter { public int BoderWidth { get; set; } public string HeaderLeft { get; set; } public string HeaderRight { get; set; } public int LabelColumnWidth { get; set; } public int ValueColumnWidth { get; set; } public ElementWriter NewLeftColumn { get; set; } public ElementWriter NewRightColumn { get; set; } public ObjectValueLabelParser LeftColumn { get; set; } public ObjectValueLabelParser RightColumn { get; set; } public override void Init() { ArgumentValidationUtility.ThrowOnNull(LeftColumn, "LeftColumn"); if (LabelColumnWidth < 1) { LabelColumnWidth = 15; } if (ValueColumnWidth < 1) { ValueColumnWidth = 35; } } public override IElement GetElement(IElement parent, ObjectWrapper data) { LOG.Debug("Init Write"); //BeforeWrite(pdf, context); PdfPTable table = new PdfPTable(4); if (parent == null) { parent = table; } table.WidthPercentage = 100; table.SetWidths(new int[] { LabelColumnWidth, ValueColumnWidth, LabelColumnWidth, ValueColumnWidth }); table.HorizontalAlignment = Element.ALIGN_LEFT; table.SpacingAfter = (SpaceAfter.HasValue) ? SpaceAfter.Value : 2; table.SpacingBefore = (SpaceBefore.HasValue) ? SpaceBefore.Value : 0; //exit and don't write anything if no values to write. //if ((LeftColumn == null && RightColumn == null) || // (LeftColumn != null && RightColumn != null && String.IsNullOrEmpty(LeftColumn.ParseValue(data)) && String.IsNullOrEmpty(RightColumn.ParseValue(data)))) return null; if (!String.IsNullOrEmpty(HeaderLeft)) { //LEFT LABEL PdfPCell cell0Label = new PdfPCell(new Phrase(HeaderLeft, LabelFont)); cell0Label.HorizontalAlignment = PdfPCell.ALIGN_LEFT; cell0Label.VerticalAlignment = PdfPCell.ALIGN_TOP; cell0Label.BorderWidth = BoderWidth; cell0Label.Colspan = 2; table.AddCell(cell0Label); //LEFT LABEL PdfPCell cell00Label = null; if (!String.IsNullOrEmpty(HeaderRight)) { cell00Label = new PdfPCell(new Phrase(HeaderRight, LabelFont)); } else { cell00Label = new PdfPCell(new Phrase(String.Empty, LabelFont)); } cell00Label.HorizontalAlignment = PdfPCell.ALIGN_LEFT; cell00Label.VerticalAlignment = PdfPCell.ALIGN_TOP; cell00Label.BorderWidth = BoderWidth; cell00Label.Colspan = 2; table.AddCell(cell00Label); } //LEFT int leftColSpan = 1; //if (!String.IsNullOrEmpty(LeftColumn.Label)) if(LeftColumn!=null) { PdfPCell cell1Label = new PdfPCell(new Phrase(LeftColumn.Label, Font)); cell1Label.HorizontalAlignment = PdfPCell.ALIGN_LEFT; cell1Label.VerticalAlignment = PdfPCell.ALIGN_TOP; cell1Label.BorderWidth = BoderWidth; table.AddCell(cell1Label); } else { leftColSpan = 2; } //PdfPCell cell1Value = new PdfPCell(new Phrase(LeftColumn.ParseValue(context), Font)); PdfPCell cell1Value = new PdfPCell(new Phrase(LeftColumn.ParseValue(data), Font)); cell1Value.HorizontalAlignment = PdfPCell.ALIGN_LEFT; cell1Value.VerticalAlignment = PdfPCell.ALIGN_TOP; cell1Value.BorderWidth = BoderWidth; cell1Value.Colspan = leftColSpan; //RIGHT int rightColSpan = 1; if (RightColumn != null) { table.AddCell(cell1Value); if (!String.IsNullOrEmpty(LeftColumn.Label)) { PdfPCell cell2Label = new PdfPCell(new Phrase(RightColumn.Label, Font)); cell2Label.HorizontalAlignment = PdfPCell.ALIGN_LEFT; cell2Label.VerticalAlignment = PdfPCell.ALIGN_TOP; cell2Label.BorderWidth = BoderWidth; table.AddCell(cell2Label); } else { rightColSpan = 2; } PdfPCell cell2Value = new PdfPCell(new Phrase(RightColumn.ParseValue(data), Font)); cell2Value.HorizontalAlignment = PdfPCell.ALIGN_LEFT; cell2Value.VerticalAlignment = PdfPCell.ALIGN_TOP; cell2Value.BorderWidth = BoderWidth; cell2Value.Colspan = rightColSpan; table.AddCell(cell2Value); } else { cell1Value.Colspan = 3; table.AddCell(cell1Value); } return table; } public override void Write(Document pdf, ObjectWrapper context) { LOG.Debug("Init Write"); BeforeWrite(pdf, context); PdfPTable table = new PdfPTable(4); table.WidthPercentage = 100; table.SetWidths(new int[] { LabelColumnWidth, ValueColumnWidth, LabelColumnWidth, ValueColumnWidth }); table.HorizontalAlignment = Element.ALIGN_LEFT; table.SpacingAfter = (SpaceAfter.HasValue) ? SpaceAfter.Value : 2; table.SpacingBefore = (SpaceBefore.HasValue) ? SpaceBefore.Value : 0; //exit and don't write anything if no values to write. // if ((LeftColumn == null && RightColumn == null) || // (LeftColumn!=null && RightColumn != null && String.IsNullOrEmpty(LeftColumn.ParseValue(context)) && String.IsNullOrEmpty(RightColumn.ParseValue(context)))) return; if (!String.IsNullOrEmpty(HeaderLeft)) { //LEFT LABEL PdfPCell cell0Label = new PdfPCell(new Phrase(HeaderLeft, LabelFont)); cell0Label.HorizontalAlignment = PdfPCell.ALIGN_LEFT; cell0Label.VerticalAlignment = PdfPCell.ALIGN_TOP; cell0Label.BorderWidth = BoderWidth; cell0Label.Colspan = 2; table.AddCell(cell0Label); //LEFT LABEL PdfPCell cell00Label = null; if (!String.IsNullOrEmpty(HeaderRight)) { cell00Label = new PdfPCell(new Phrase(HeaderRight, LabelFont)); } else { cell00Label = new PdfPCell(new Phrase(String.Empty, LabelFont)); } cell00Label.HorizontalAlignment = PdfPCell.ALIGN_LEFT; cell00Label.VerticalAlignment = PdfPCell.ALIGN_TOP; cell00Label.BorderWidth = BoderWidth; cell00Label.Colspan = 2; table.AddCell(cell00Label); } //LEFT int leftColSpan = 1; //if (!String.IsNullOrEmpty(LeftColumn.Label)) if(LeftColumn!=null) { PdfPCell cell1Label = new PdfPCell(new Phrase(LeftColumn.Label, Font)); //PdfPCell cell1Label = new PdfPCell(new Phrase(LeftColumnHeader.Write(pdf, context), Font)); cell1Label.HorizontalAlignment = PdfPCell.ALIGN_LEFT; cell1Label.VerticalAlignment = PdfPCell.ALIGN_TOP; cell1Label.BorderWidth = BoderWidth; table.AddCell(cell1Label); } else { leftColSpan = 2; } //PdfPCell cell1Value = new PdfPCell(new Phrase(LeftColumn.ParseValue(context), Font)); PdfPCell cell1Value = new PdfPCell(new Phrase(LeftColumn.ParseValue(context), Font)); cell1Value.HorizontalAlignment = PdfPCell.ALIGN_LEFT; cell1Value.VerticalAlignment = PdfPCell.ALIGN_TOP; cell1Value.BorderWidth = BoderWidth; cell1Value.Colspan = leftColSpan; //RIGHT int rightColSpan = 1; if (RightColumn != null) { table.AddCell(cell1Value); if (!String.IsNullOrEmpty(LeftColumn.Label)) { PdfPCell cell2Label = new PdfPCell(new Phrase(RightColumn.Label, Font)); cell2Label.HorizontalAlignment = PdfPCell.ALIGN_LEFT; cell2Label.VerticalAlignment = PdfPCell.ALIGN_TOP; cell2Label.BorderWidth = BoderWidth; table.AddCell(cell2Label); } else { rightColSpan = 2; } PdfPCell cell2Value = new PdfPCell(new Phrase(RightColumn.ParseValue(context), Font)); cell2Value.HorizontalAlignment = PdfPCell.ALIGN_LEFT; cell2Value.VerticalAlignment = PdfPCell.ALIGN_TOP; cell2Value.BorderWidth = BoderWidth; cell2Value.Colspan = rightColSpan; table.AddCell(cell2Value); } else { cell1Value.Colspan = 3; table.AddCell(cell1Value); } pdf.Add(table); } } }