private void SetRightOfBorder(HtmlCell cell, String key, String value, TableStyleValues values) { if (key.Contains(CSS.Property.WIDTH)) { values.BorderWidthRight = utils.ParsePxInCmMmPcToPt(value); } if (key.Contains(CSS.Property.COLOR)) { values.BorderColorRight = HtmlUtilities.DecodeColor(value); } else if (values.BorderColorRight == null) { values.BorderColorRight = BaseColor.BLACK; } if (key.Contains("style")) { // If any, which are the border styles in iText? simulate in the borderevent? if (!values.GetBorderWidthRight(false).HasValue) { values.BorderWidthRight = 2.25f; } } }
public override HtmlCell Apply(HtmlCell cell, Tag t, IMarginMemory memory, IPageSizeContainable psc, HtmlPipelineContext ctx) { Tag row = t.Parent; while (row != null && !row.Name.Equals(HTML.Tag.TR)) { row = row.Parent; } Tag table = t.Parent; while (table != null && !table.Name.Equals(HTML.Tag.TABLE)) { table = table.Parent; } TableStyleValues values = Table.SetBorderAttributeForCell(table); IDictionary <String, String> css = t.CSS; String emptyCells; css.TryGetValue(CSS.Property.EMPTY_CELLS, out emptyCells); if (null != emptyCells && Util.EqualsIgnoreCase(CSS.Value.HIDE, emptyCells) && cell.CompositeElements == null) { cell.Border = Rectangle.NO_BORDER; } else { cell.VerticalAlignment = Element.ALIGN_MIDDLE; // Default css behavior. Implementation of "vertical-align" style further along. String vAlign = null; if (t.Attributes.ContainsKey(HTML.Attribute.VALIGN)) { vAlign = t.Attributes[HTML.Attribute.VALIGN]; } else if (css.ContainsKey(HTML.Attribute.VALIGN)) { vAlign = css[HTML.Attribute.VALIGN]; } else if (row != null) { if (row.Attributes.ContainsKey(HTML.Attribute.VALIGN)) { vAlign = row.Attributes[HTML.Attribute.VALIGN]; } else if (row.CSS.ContainsKey(HTML.Attribute.VALIGN)) { vAlign = row.CSS[HTML.Attribute.VALIGN]; } } if (vAlign != null) { if (Util.EqualsIgnoreCase(CSS.Value.TOP, vAlign)) { cell.VerticalAlignment = Element.ALIGN_TOP; } else if (Util.EqualsIgnoreCase(CSS.Value.BOTTOM, vAlign)) { cell.VerticalAlignment = Element.ALIGN_BOTTOM; } } String align = null; if (t.Attributes.ContainsKey(HTML.Attribute.ALIGN)) { align = t.Attributes[HTML.Attribute.ALIGN]; } else if (css.ContainsKey(CSS.Property.TEXT_ALIGN)) { align = css[CSS.Property.TEXT_ALIGN]; } if (align != null) { if (Util.EqualsIgnoreCase(CSS.Value.CENTER, align)) { cell.HorizontalAlignment = Element.ALIGN_CENTER; } else if (Util.EqualsIgnoreCase(CSS.Value.RIGHT, align)) { cell.HorizontalAlignment = Element.ALIGN_RIGHT; } else if (Util.EqualsIgnoreCase(CSS.Value.JUSTIFY, align)) { cell.HorizontalAlignment = Element.ALIGN_JUSTIFIED; } } if (t.Attributes.ContainsKey(HTML.Attribute.WIDTH) || css.ContainsKey(HTML.Attribute.WIDTH)) { cell.FixedWidth = new WidthCalculator().GetWidth(t, memory.GetRootTags(), psc.PageSize.Width); } HeightCalculator heightCalc = new HeightCalculator(); float? height = heightCalc.GetHeight(t, psc.PageSize.Height); if (height == null && row != null) { height = heightCalc.GetHeight(row, psc.PageSize.Height); } if (height != null) { cell.MinimumHeight = height.Value; } String colspan; if (t.Attributes.TryGetValue(HTML.Attribute.COLSPAN, out colspan)) { cell.Colspan = int.Parse(colspan); } String rowspan; t.Attributes.TryGetValue(HTML.Attribute.ROWSPAN, out rowspan); if (null != rowspan) { cell.Rowspan = int.Parse(rowspan); } foreach (KeyValuePair <String, String> entry in css) { String key = entry.Key; String value = entry.Value; cell.UseBorderPadding = true; if (Util.EqualsIgnoreCase(key, CSS.Property.BACKGROUND_COLOR)) { values.Background = HtmlUtilities.DecodeColor(value); } else if (Util.EqualsIgnoreCase(key, CSS.Property.VERTICAL_ALIGN)) { if (Util.EqualsIgnoreCase(value, CSS.Value.TOP)) { cell.VerticalAlignment = Element.ALIGN_TOP; cell.PaddingTop = cell.PaddingTop + 6; } else if (Util.EqualsIgnoreCase(value, CSS.Value.BOTTOM)) { cell.VerticalAlignment = Element.ALIGN_BOTTOM; cell.PaddingBottom = cell.PaddingBottom + 6; } } else if (key.Contains(CSS.Property.BORDER)) { if (key.Contains(CSS.Value.TOP)) { SetTopOfBorder(cell, key, value, values); } else if (key.Contains(CSS.Value.BOTTOM)) { SetBottomOfBorder(cell, key, value, values); } else if (key.Contains(CSS.Value.LEFT)) { SetLeftOfBorder(cell, key, value, values); } else if (key.Contains(CSS.Value.RIGHT)) { SetRightOfBorder(cell, key, value, values); } } else if (key.Contains(CSS.Property.CELLPADDING) || key.Contains(CSS.Property.PADDING)) { if (key.Contains(CSS.Value.TOP)) { cell.PaddingTop = cell.PaddingTop + utils.ParsePxInCmMmPcToPt(value); } else if (key.Contains(CSS.Value.BOTTOM)) { cell.PaddingBottom = cell.PaddingBottom + utils.ParsePxInCmMmPcToPt(value); } else if (key.Contains(CSS.Value.LEFT)) { cell.PaddingLeft = cell.PaddingLeft + utils.ParsePxInCmMmPcToPt(value); } else if (key.Contains(CSS.Value.RIGHT)) { cell.PaddingRight = cell.PaddingRight + utils.ParsePxInCmMmPcToPt(value); } } else if (key.Contains(CSS.Property.TEXT_ALIGN)) { cell.HorizontalAlignment = CSS.GetElementAlignment(value); } } cell.PaddingLeft = cell.PaddingLeft + values.HorBorderSpacing + values.BorderWidthLeft; cell.PaddingRight = cell.PaddingRight + values.BorderWidthRight; cell.PaddingTop = cell.PaddingTop + values.VerBorderSpacing + values.BorderWidthTop; cell.PaddingBottom = cell.PaddingBottom + values.BorderWidthBottom; } cell.Border = Rectangle.NO_BORDER; cell.CellEvent = new CellSpacingEvent(values); cell.CellValues = values; return(cell); }
/* * (non-Javadoc) * * @see * com.itextpdf.tool.xml.css.CssApplier#apply(com.itextpdf.text.Element, * com.itextpdf.tool.xml.Tag) */ public HtmlCell Apply(HtmlCell cell, Tag t, IMarginMemory memory, IPageSizeContainable psc) { TableStyleValues values = new TableStyleValues(); Tag table = t.Parent; while (!table.Name.Equals("table")) { table = table.Parent; } String border; table.Attributes.TryGetValue(CSS.Property.BORDER, out border); if (border != null && !border.Equals("0")) { values.BorderColor = BaseColor.BLACK; values.BorderWidth = 0.75f; } IDictionary <String, String> css = t.CSS; String emptyCells; css.TryGetValue(CSS.Property.EMPTY_CELLS, out emptyCells); if (null != emptyCells && Util.EqualsIgnoreCase(CSS.Value.HIDE, emptyCells) && cell.CompositeElements == null) { cell.Border = Rectangle.NO_BORDER; } else { cell.VerticalAlignment = Element.ALIGN_MIDDLE; // Default css behavior. Implementation of "vertical-align" style further along. if (t.Attributes.ContainsKey(HTML.Attribute.WIDTH) || css.ContainsKey(HTML.Attribute.WIDTH)) { cell.FixedWidth = new WidthCalculator().GetWidth(t, memory.GetRootTags(), psc.PageSize.Width); } String colspan; t.Attributes.TryGetValue(HTML.Attribute.COLSPAN, out colspan); if (null != colspan) { cell.Colspan = int.Parse(colspan); } String rowspan; t.Attributes.TryGetValue(HTML.Attribute.ROWSPAN, out rowspan); if (null != rowspan) { cell.Rowspan = int.Parse(rowspan); } foreach (KeyValuePair <String, String> entry in css) { String key = entry.Key; String value = entry.Value; cell.UseBorderPadding = true; if (Util.EqualsIgnoreCase(key, CSS.Property.HEIGHT)) { cell.MinimumHeight = utils.ParsePxInCmMmPcToPt(value); } else if (Util.EqualsIgnoreCase(key, CSS.Property.BACKGROUND_COLOR)) { values.Background = HtmlUtilities.DecodeColor(value); } else if (Util.EqualsIgnoreCase(key, CSS.Property.VERTICAL_ALIGN)) { if (Util.EqualsIgnoreCase(value, CSS.Value.TOP)) { cell.VerticalAlignment = Element.ALIGN_TOP; cell.PaddingTop = cell.PaddingTop + 6; } else if (Util.EqualsIgnoreCase(value, CSS.Value.BOTTOM)) { cell.VerticalAlignment = Element.ALIGN_BOTTOM; cell.PaddingBottom = cell.PaddingBottom + 6; } } else if (key.Contains(CSS.Property.BORDER)) { if (key.Contains(CSS.Value.TOP)) { SetTopOfBorder(cell, key, value, values); } else if (key.Contains(CSS.Value.BOTTOM)) { SetBottomOfBorder(cell, key, value, values); } else if (key.Contains(CSS.Value.LEFT)) { SetLeftOfBorder(cell, key, value, values); } else if (key.Contains(CSS.Value.RIGHT)) { SetRightOfBorder(cell, key, value, values); } } else if (key.Contains(CSS.Property.CELLPADDING) || key.Contains(CSS.Property.PADDING)) { if (key.Contains(CSS.Value.TOP)) { cell.PaddingTop = cell.PaddingTop + utils.ParsePxInCmMmPcToPt(value); } else if (key.Contains(CSS.Value.BOTTOM)) { cell.PaddingBottom = cell.PaddingBottom + utils.ParsePxInCmMmPcToPt(value); } else if (key.Contains(CSS.Value.LEFT)) { cell.PaddingLeft = cell.PaddingLeft + utils.ParsePxInCmMmPcToPt(value); } else if (key.Contains(CSS.Value.RIGHT)) { cell.PaddingRight = cell.PaddingRight + utils.ParsePxInCmMmPcToPt(value); } } else if (key.Contains(CSS.Property.TEXT_ALIGN)) { if (Util.EqualsIgnoreCase(value, CSS.Value.LEFT)) { cell.HorizontalAlignment = Element.ALIGN_LEFT; } else if (Util.EqualsIgnoreCase(value, CSS.Value.CENTER)) { cell.HorizontalAlignment = Element.ALIGN_CENTER; } else if (Util.EqualsIgnoreCase(value, CSS.Value.RIGHT)) { cell.HorizontalAlignment = Element.ALIGN_RIGHT; } } } float horSpacing = new Table().GetBorderOrCellSpacing(true, table.CSS, table.Attributes); float verSpacing = new Table().GetBorderOrCellSpacing(false, table.CSS, table.Attributes); values.HorBorderSpacing = horSpacing; values.VerBorderSpacing = verSpacing; cell.PaddingLeft = cell.PaddingLeft + horSpacing + values.BorderWidthLeft; cell.PaddingRight = cell.PaddingRight + values.BorderWidthRight; cell.PaddingTop = cell.PaddingTop + verSpacing + values.BorderWidthTop; cell.PaddingBottom = cell.PaddingBottom + values.BorderWidthBottom + 1; } cell.Border = Rectangle.NO_BORDER; cell.CellEvent = new CellSpacingEvent(values); cell.CellValues = values; return(cell); }