/** Set the table style values in a {@link TableStyleValues} object based on attributes and css of the given tag. * @param tag containing attributes and css. * @return a {@link TableStyleValues} object containing the table's style values. */ private TableStyleValues SetStyleValues(Tag tag) { TableStyleValues styleValues = new TableStyleValues(); IDictionary <String, String> css = tag.CSS; IDictionary <String, String> attributes = tag.Attributes; string v; if (attributes.ContainsKey(CSS.Property.BORDER)) { styleValues.BorderColor = BaseColor.BLACK; styleValues.BorderWidth = utils.ParsePxInCmMmPcToPt(attributes[CSS.Property.BORDER]); } else { css.TryGetValue(CSS.Property.BORDER_BOTTOM_COLOR, out v); styleValues.BorderColorBottom = HtmlUtilities.DecodeColor(v); css.TryGetValue(CSS.Property.BORDER_TOP_COLOR, out v); styleValues.BorderColorTop = HtmlUtilities.DecodeColor(v); css.TryGetValue(CSS.Property.BORDER_LEFT_COLOR, out v); styleValues.BorderColorLeft = HtmlUtilities.DecodeColor(v); css.TryGetValue(CSS.Property.BORDER_RIGHT_COLOR, out v); styleValues.BorderColorRight = HtmlUtilities.DecodeColor(v); styleValues.BorderWidthBottom = utils.CheckMetricStyle(css, CSS.Property.BORDER_BOTTOM_WIDTH); styleValues.BorderWidthTop = utils.CheckMetricStyle(css, CSS.Property.BORDER_TOP_WIDTH); styleValues.BorderWidthLeft = utils.CheckMetricStyle(css, CSS.Property.BORDER_LEFT_WIDTH); styleValues.BorderWidthRight = utils.CheckMetricStyle(css, CSS.Property.BORDER_RIGHT_WIDTH); } css.TryGetValue(CSS.Property.BACKGROUND_COLOR, out v); styleValues.Background = HtmlUtilities.DecodeColor(v); styleValues.HorBorderSpacing = GetBorderOrCellSpacing(true, css, attributes); styleValues.VerBorderSpacing = GetBorderOrCellSpacing(false, css, attributes); return(styleValues); }
/** * Calculates the leading of the given tag. * <br /> * First checks which line-height string is present in the css of the tag, if any. Following strings are allowed; * <ul> * <li>a constant (containing px, in, cm, mm, pc, em, ex or pt),</li> * <li>percentage (containing %),</li> * <li>multiplier (only digits),</li> * </ul> * Then this methods calculates the leading based on the font-size and the line-height.<br /><br /> * If no line-height was given or if the line-height:normal was given, leading = font-size * 1.5f. * @param t tag of which the leading has to be calculated. * @return float containing the leading of the tag. */ public float CalculateLeading(Tag t) { float leading = 0; IDictionary <String, String> css = t.CSS; float fontSize = fontSizeTranslator.GetFontSize(t); if (css.ContainsKey(CSS.Property.LINE_HEIGHT)) { String value = css[CSS.Property.LINE_HEIGHT]; if (utils.IsNumericValue(value)) { leading = float.Parse(value, CultureInfo.InvariantCulture) * fontSize; } else if (utils.IsRelativeValue(value)) { leading = utils.ParseRelativeValue(value, fontSize); } else if (utils.IsMetricValue(value)) { leading = utils.ParsePxInCmMmPcToPt(value); } // a faulty value was entered for line-height. if (leading == 0) { leading = fontSize * 1.5f; } } else { leading = fontSize * 1.5f; } return(leading); }
public void ResolveIndentations() { CssUtils cssUtils = CssUtils.GetInstance(); Assert.AreEqual(cssUtils.ParseRelativeValue("1em", 12), ((Paragraph)elementList[0]).SpacingBefore, 0); Assert.AreEqual(cssUtils.ParsePxInCmMmPcToPt("1.5in"), ((Paragraph)elementList[1]).IndentationLeft, 0); Assert.AreEqual(cssUtils.ParsePxInCmMmPcToPt("3cm"), ((Paragraph)elementList[2]).IndentationRight, 0); Assert.AreEqual(cssUtils.ParsePxInCmMmPcToPt("5pc") - 12, ((Paragraph)elementList[3]).SpacingBefore, 0); Assert.AreEqual(cssUtils.ParsePxInCmMmPcToPt("4pc"), ((Paragraph)elementList[3]).SpacingAfter, 0); Assert.AreEqual(cssUtils.ParsePxInCmMmPcToPt("80px") - cssUtils.ParsePxInCmMmPcToPt("4pc"), ((Paragraph)elementList[4]).SpacingBefore, 0); Assert.AreEqual(cssUtils.ParsePxInCmMmPcToPt("80px"), ((Paragraph)elementList[4]).SpacingAfter, 0); }
public override Image Apply(Image img, Tag tag, IMarginMemory mm, IPageSizeContainable psc, HtmlPipelineContext ctx) { IDictionary <String, String> cssMap = tag.CSS; String widthValue = null; cssMap.TryGetValue(HTML.Attribute.WIDTH, out widthValue); if (widthValue == null) { tag.Attributes.TryGetValue(HTML.Attribute.WIDTH, out widthValue); } String heightValue = null; cssMap.TryGetValue(HTML.Attribute.HEIGHT, out heightValue); if (heightValue == null) { tag.Attributes.TryGetValue(HTML.Attribute.HEIGHT, out heightValue); } if (widthValue == null) { img.ScaleToFitLineWhenOverflow = true; } else { img.ScaleToFitLineWhenOverflow = false; } img.ScaleToFitHeight = false; CssUtils utils = CssUtils.GetInstance(); float widthInPoints = utils.ParsePxInCmMmPcToPt(widthValue); float heightInPoints = utils.ParsePxInCmMmPcToPt(heightValue); if (widthInPoints > 0 && heightInPoints > 0) { img.ScaleAbsolute(widthInPoints, heightInPoints); } else if (widthInPoints > 0) { heightInPoints = img.Height * widthInPoints / img.Width; img.ScaleAbsolute(widthInPoints, heightInPoints); } else if (heightInPoints > 0) { widthInPoints = img.Width * heightInPoints / img.Height; img.ScaleAbsolute(widthInPoints, heightInPoints); } // apply border CSS String borderTopColor = null; cssMap.TryGetValue(CSS.Property.BORDER_TOP_COLOR, out borderTopColor); if (borderTopColor != null) { img.BorderColorTop = HtmlUtilities.DecodeColor(borderTopColor); } String borderTopWidth = null; cssMap.TryGetValue(CSS.Property.BORDER_TOP_WIDTH, out borderTopWidth); if (borderTopWidth != null) { img.BorderWidthTop = utils.ParseValueToPt(borderTopWidth, 1f); } String borderRightColor = null; cssMap.TryGetValue(CSS.Property.BORDER_RIGHT_COLOR, out borderRightColor); if (borderRightColor != null) { img.BorderColorRight = HtmlUtilities.DecodeColor(borderRightColor); } String borderRightWidth = null; cssMap.TryGetValue(CSS.Property.BORDER_RIGHT_WIDTH, out borderRightWidth); if (borderRightWidth != null) { img.BorderWidthRight = utils.ParseValueToPt(borderRightWidth, 1f); } String borderBottomColor = null; cssMap.TryGetValue(CSS.Property.BORDER_BOTTOM_COLOR, out borderBottomColor); if (borderBottomColor != null) { img.BorderColorBottom = HtmlUtilities.DecodeColor(borderBottomColor); } String borderBottomWidth = null; cssMap.TryGetValue(CSS.Property.BORDER_BOTTOM_WIDTH, out borderBottomWidth); if (borderBottomWidth != null) { img.BorderWidthBottom = utils.ParseValueToPt(borderBottomWidth, 1f); } String borderLeftColor = null; cssMap.TryGetValue(CSS.Property.BORDER_LEFT_COLOR, out borderLeftColor); if (borderLeftColor != null) { img.BorderColorLeft = HtmlUtilities.DecodeColor(borderLeftColor); } String borderLeftWidth = null; cssMap.TryGetValue(CSS.Property.BORDER_LEFT_WIDTH, out borderLeftWidth); if (borderLeftWidth != null) { img.BorderWidthLeft = utils.ParseValueToPt(borderLeftWidth, 1f); } // end of border CSS String before = null; cssMap.TryGetValue(CSS.Property.BEFORE, out before); if (before != null) { img.SpacingBefore = float.Parse(before, CultureInfo.InvariantCulture); } String after = null; cssMap.TryGetValue(CSS.Property.AFTER, out after); if (after != null) { img.SpacingAfter = float.Parse(after, CultureInfo.InvariantCulture); } img.WidthPercentage = 0; return(img); }
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); }
/** * * @param c the Chunk to apply CSS to. * @param t the tag containing the chunk data * @return the styled chunk */ virtual public Chunk Apply(Chunk c, Tag t) { Font f = ApplyFontStyles(t); float size = f.Size; String value = null; IDictionary <String, String> rules = t.CSS; foreach (KeyValuePair <String, String> entry in rules) { String key = entry.Key; value = entry.Value; if (Util.EqualsIgnoreCase(CSS.Property.FONT_STYLE, key)) { if (Util.EqualsIgnoreCase(CSS.Value.OBLIQUE, value)) { c.SetSkew(0, 12); } } else if (Util.EqualsIgnoreCase(CSS.Property.LETTER_SPACING, key)) { String letterSpacing = entry.Value; float letterSpacingValue = 0f; if (utils.IsRelativeValue(value)) { letterSpacingValue = utils.ParseRelativeValue(letterSpacing, f.Size); } else if (utils.IsMetricValue(value)) { letterSpacingValue = utils.ParsePxInCmMmPcToPt(letterSpacing); } c.SetCharacterSpacing(letterSpacingValue); } else if (Util.EqualsIgnoreCase(CSS.Property.XFA_FONT_HORIZONTAL_SCALE, key)) { // only % allowed; need a catch block NumberFormatExc? c.SetHorizontalScaling( float.Parse(value.Replace("%", "")) / 100); } } // following styles are separate from the for each loop, because they are based on font settings like size. if (rules.TryGetValue(CSS.Property.VERTICAL_ALIGN, out value)) { if (Util.EqualsIgnoreCase(CSS.Value.SUPER, value) || Util.EqualsIgnoreCase(CSS.Value.TOP, value) || Util.EqualsIgnoreCase(CSS.Value.TEXT_TOP, value)) { c.SetTextRise((float)(size / 2 + 0.5)); } else if (Util.EqualsIgnoreCase(CSS.Value.SUB, value) || Util.EqualsIgnoreCase(CSS.Value.BOTTOM, value) || Util.EqualsIgnoreCase(CSS.Value.TEXT_BOTTOM, value)) { c.SetTextRise(-size / 2); } else { c.SetTextRise(utils.ParsePxInCmMmPcToPt(value)); } } String xfaVertScale; if (rules.TryGetValue(CSS.Property.XFA_FONT_VERTICAL_SCALE, out xfaVertScale)) { if (xfaVertScale.Contains("%")) { size *= float.Parse(xfaVertScale.Replace("%", "")) / 100; c.SetHorizontalScaling(100 / float.Parse(xfaVertScale.Replace("%", ""))); } } if (rules.TryGetValue(CSS.Property.TEXT_DECORATION, out value)) { String[] splitValues = new Regex(@"\s+").Split(value); foreach (String curValue in splitValues) { if (Util.EqualsIgnoreCase(CSS.Value.UNDERLINE, curValue)) { c.SetUnderline(0.75f, -size / 8f); } if (Util.EqualsIgnoreCase(CSS.Value.LINE_THROUGH, curValue)) { c.SetUnderline(0.75f, size / 4f); } } } if (rules.TryGetValue(CSS.Property.BACKGROUND_COLOR, out value)) { c.SetBackground(HtmlUtilities.DecodeColor(value)); } f.Size = size; c.Font = f; float?leading = null; value = null; if (rules.TryGetValue(CSS.Property.LINE_HEIGHT, out value)) { if (utils.IsNumericValue(value)) { leading = float.Parse(value) * c.Font.Size; } else if (utils.IsRelativeValue(value)) { leading = utils.ParseRelativeValue(value, c.Font.Size); } else if (utils.IsMetricValue(value)) { leading = utils.ParsePxInCmMmPcToPt(value); } } if (leading != null) { c.setLineHeight((float)leading); } return(c); }
/** * Styles a paragraph * * @param p the paragraph * @param t the tag * @param configuration the MarginMemory * @return a styled {@link Paragraph} */ virtual public Paragraph Apply(Paragraph p, Tag t, IMarginMemory configuration) { /*MaxLeadingAndSize m = new MaxLeadingAndSize(); * if (configuration.GetRootTags().Contains(t.GetName())) { * m.SetLeading(t); * } else { * m.SetVariablesBasedOnChildren(t); * }*/ CssUtils utils = CssUtils.GetInstance(); float fontSize = FontSizeTranslator.GetInstance().GetFontSize(t); if (fontSize < 0) { fontSize = 0; } float lmb = 0; bool hasLMB = false; IDictionary <String, String> css = t.CSS; foreach (KeyValuePair <String, String> entry in css) { String key = entry.Key; String value = entry.Value; if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_TOP, key)) { p.SpacingBefore = p.SpacingBefore + utils.CalculateMarginTop(value, fontSize, configuration); } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_TOP, key)) { p.SpacingBefore = p.SpacingBefore + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_BOTTOM, key)) { float after = utils.ParseValueToPt(value, fontSize); p.SpacingAfter = p.SpacingAfter + after; lmb = after; hasLMB = true; } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_BOTTOM, key)) { p.SpacingAfter = p.SpacingAfter + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_LEFT, key)) { p.IndentationLeft = p.IndentationLeft + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_RIGHT, key)) { p.IndentationRight = p.IndentationRight + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_LEFT, key)) { p.IndentationLeft = p.IndentationLeft + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_RIGHT, key)) { p.IndentationRight = p.IndentationRight + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.TEXT_ALIGN, key)) { if (Util.EqualsIgnoreCase(CSS.Value.RIGHT, value)) { p.Alignment = Element.ALIGN_RIGHT; } else if (Util.EqualsIgnoreCase(CSS.Value.CENTER, value)) { p.Alignment = Element.ALIGN_CENTER; } else if (Util.EqualsIgnoreCase(CSS.Value.LEFT, value)) { p.Alignment = Element.ALIGN_LEFT; } else if (Util.EqualsIgnoreCase(CSS.Value.JUSTIFY, value)) { p.Alignment = Element.ALIGN_JUSTIFIED; } } else if (Util.EqualsIgnoreCase(CSS.Property.TEXT_INDENT, key)) { p.FirstLineIndent = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.LINE_HEIGHT, key)) { if (utils.IsNumericValue(value)) { p.Leading = float.Parse(value) * fontSize; } else if (utils.IsRelativeValue(value)) { p.Leading = utils.ParseRelativeValue(value, fontSize); } else if (utils.IsMetricValue(value)) { p.Leading = utils.ParsePxInCmMmPcToPt(value); } } } // setDefaultMargin to largestFont if no margin-bottom is set and p-tag is child of the root tag. /*if (null != t.GetParent()) { * String parent = t.GetParent().GetName(); * if (css[CSS.Property.MARGIN_TOP] == null && configuration.GetRootTags().Contains(parent)) { * p.SetSpacingBefore(p.GetSpacingBefore() + utils.CalculateMarginTop(fontSize + "pt", 0, configuration)); * } * if (css[CSS.Property.MARGIN_BOTTOM] == null && configuration.GetRootTags().Contains(parent)) { * p.SetSpacingAfter(p.GetSpacingAfter() + fontSize); * css.Put(CSS.Property.MARGIN_BOTTOM, fontSize + "pt"); * lmb = fontSize; * hasLMB = true; * } * //p.SetLeading(m.GetLargestLeading()); We need possibility to detect that line-height undefined; * if (p.GetAlignment() == -1) { * p.SetAlignment(Element.ALIGN_LEFT); * } * }*/ if (hasLMB) { configuration.LastMarginBottom = lmb; } Font font = appliers.ChunkCssAplier.ApplyFontStyles(t); p.Font = font; // TODO reactive for positioning and implement more return(p); }
virtual public void ParseLength() { Assert.AreEqual(9, css.ParsePxInCmMmPcToPt("12"), 0); Assert.AreEqual(576, css.ParsePxInCmMmPcToPt("8inch"), 0); Assert.AreEqual(576, css.ParsePxInCmMmPcToPt("8", CSS.Value.IN), 0); }
public PdfDiv apply(PdfDiv div, Tag t, IMarginMemory memory, IPageSizeContainable psc) { IDictionary <String, String> css = t.CSS; float fontSize = FontSizeTranslator.GetInstance().TranslateFontSize(t); if (fontSize == Font.UNDEFINED) { fontSize = FontSizeTranslator.DEFAULT_FONT_SIZE; } 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(align, CSS.Value.CENTER)) { div.TextAlignment = Element.ALIGN_CENTER; } else if (Util.EqualsIgnoreCase(align, CSS.Value.RIGHT)) { div.TextAlignment = Element.ALIGN_RIGHT; } else if (Util.EqualsIgnoreCase(align, CSS.Value.JUSTIFY)) { div.TextAlignment = Element.ALIGN_JUSTIFIED; } } String widthValue; if (!t.CSS.TryGetValue(HTML.Attribute.WIDTH, out widthValue)) { t.Attributes.TryGetValue(HTML.Attribute.WIDTH, out widthValue); } if (widthValue != null) { if (utils.IsNumericValue(widthValue) || utils.IsMetricValue(widthValue)) { div.Width = utils.ParsePxInCmMmPcToPt(widthValue); } else if (utils.IsRelativeValue(widthValue)) { if (widthValue.Contains(CSS.Value.PERCENTAGE)) { div.PercentageWidth = utils.ParseRelativeValue(widthValue, 1f); } else { div.Width = utils.ParseRelativeValue(widthValue, fontSize); } } } String heightValue; if (!t.CSS.TryGetValue(HTML.Attribute.HEIGHT, out heightValue)) { t.Attributes.TryGetValue(HTML.Attribute.HEIGHT, out heightValue); } if (heightValue != null) { if (utils.IsNumericValue(heightValue) || utils.IsMetricValue(heightValue)) { div.Height = utils.ParsePxInCmMmPcToPt(heightValue); } else if (utils.IsRelativeValue(heightValue)) { if (heightValue.Contains(CSS.Value.PERCENTAGE)) { div.PercentageHeight = utils.ParseRelativeValue(heightValue, 1f); } else { div.Height = utils.ParseRelativeValue(heightValue, fontSize); } } } float?marginTop = null; float?marginBottom = null; foreach (KeyValuePair <String, String> entry in css) { String key = entry.Key; String value = entry.Value; if (Util.EqualsIgnoreCase(key, CSS.Property.LEFT)) { div.Left = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.RIGHT)) { if (div.Width == null || div.Left == null) { div.Right = utils.ParseValueToPt(value, fontSize); } } else if (Util.EqualsIgnoreCase(key, CSS.Property.TOP)) { div.Top = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.BOTTOM)) { if (div.Height == null || div.Top == null) { div.Bottom = utils.ParseValueToPt(value, fontSize); } } else if (Util.EqualsIgnoreCase(key, CSS.Property.BACKGROUND_COLOR)) { div.BackgroundColor = HtmlUtilities.DecodeColor(value); } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_LEFT)) { div.PaddingLeft = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_RIGHT)) { div.PaddingRight = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_TOP)) { div.PaddingTop = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_BOTTOM)) { div.PaddingBottom = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_TOP)) { marginTop = utils.CalculateMarginTop(value, fontSize, memory); } else if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_BOTTOM)) { marginBottom = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.FLOAT)) { if (Util.EqualsIgnoreCase(value, CSS.Value.LEFT)) { div.Float = PdfDiv.FloatType.LEFT; } else if (Util.EqualsIgnoreCase(value, CSS.Value.RIGHT)) { div.Float = PdfDiv.FloatType.RIGHT; } } else if (Util.EqualsIgnoreCase(key, CSS.Property.POSITION)) { if (Util.EqualsIgnoreCase(value, CSS.Value.ABSOLUTE)) { div.Position = PdfDiv.PositionType.ABSOLUTE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.FIXED)) { div.Position = PdfDiv.PositionType.FIXED; } else if (Util.EqualsIgnoreCase(value, CSS.Value.RELATIVE)) { div.Position = PdfDiv.PositionType.RELATIVE; } } //TODO: border, background properties. } return(div); }
/* * (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); }
/* * (non-Javadoc) * * @see * com.itextpdf.tool.xml.ITagProcessor#endElement(com.itextpdf.tool.xml.Tag, * java.util.List, com.itextpdf.text.Document) */ public override IList <IElement> End(IWorkerContext ctx, Tag tag, IList <IElement> currentContent) { IDictionary <String, String> attributes = tag.Attributes; String src; attributes.TryGetValue(HTML.Attribute.SRC, out src); iTextSharp.text.Image img = null; IList <IElement> l = new List <IElement>(1); if (!string.IsNullOrEmpty(src)) { src = XMLUtil.UnescapeXML(src); src = src.Trim(); // check if the image was already added once try { if (logger.IsLogging(Level.TRACE)) { logger.Trace(String.Format(LocaleMessages.GetInstance().GetMessage(LocaleMessages.HTML_IMG_USE), src)); } try { img = new ImageRetrieve(GetHtmlPipelineContext(ctx).GetImageProvider()).RetrieveImage(src); } catch (NoImageProviderException) { img = new ImageRetrieve().RetrieveImage(src); } } catch (IOException e) { if (logger.IsLogging(Level.ERROR)) { logger.Error(String.Format(LocaleMessages.GetInstance().GetMessage(LocaleMessages.HTML_IMG_RETRIEVE_FAIL), src), e); } } catch (NoImageException e) { if (logger.IsLogging(Level.ERROR)) { logger.Error("", e); } } catch (NoCustomContextException e) { throw new RuntimeWorkerException(LocaleMessages.GetInstance().GetMessage(LocaleMessages.NO_CUSTOM_CONTEXT), e); } if (null != img) { img.ScaleToFitLineWhenOverflow = true; String width; attributes.TryGetValue(HTML.Attribute.WIDTH, out width); float widthInPoints = utils.ParsePxInCmMmPcToPt(width); String height; attributes.TryGetValue(HTML.Attribute.HEIGHT, out height); float heightInPoints = utils.ParsePxInCmMmPcToPt(height); if (widthInPoints > 0 && heightInPoints > 0) { img.ScaleAbsolute(widthInPoints, heightInPoints); } else if (widthInPoints > 0) { heightInPoints = img.Height * widthInPoints / img.Width; img.ScaleAbsolute(widthInPoints, heightInPoints); } else if (heightInPoints > 0) { widthInPoints = img.Width * heightInPoints / img.Height; img.ScaleAbsolute(widthInPoints, heightInPoints); } try { HtmlPipelineContext htmlPipelineContext = GetHtmlPipelineContext(ctx); l.Add(GetCssAppliers().Apply(new Chunk((iTextSharp.text.Image)GetCssAppliers().Apply(img, tag, htmlPipelineContext), 0, 0, true), tag, htmlPipelineContext)); } catch (NoCustomContextException e) { throw new RuntimeWorkerException(e); } } } return(l); }
/* (non-Javadoc) * @see com.itextpdf.tool.xml.css.CssApplier#apply(com.itextpdf.text.Element, com.itextpdf.tool.xml.Tag) */ virtual public Image Apply(Image img, Tag tag) { String widthValue; tag.CSS.TryGetValue(HTML.Attribute.WIDTH, out widthValue); if (widthValue == null) { tag.Attributes.TryGetValue(HTML.Attribute.WIDTH, out widthValue); } String heightValue; tag.CSS.TryGetValue(HTML.Attribute.HEIGHT, out heightValue); if (heightValue == null) { tag.Attributes.TryGetValue(HTML.Attribute.HEIGHT, out heightValue); } if (widthValue == null) { img.ScaleToFitLineWhenOverflow = true; } else { img.ScaleToFitLineWhenOverflow = false; } if (heightValue == null) { img.ScaleToFitHeight = true; } else { img.ScaleToFitHeight = false; } CssUtils utils = CssUtils.GetInstance(); float widthInPoints = utils.ParsePxInCmMmPcToPt(widthValue); float heightInPoints = utils.ParsePxInCmMmPcToPt(heightValue); if (widthInPoints > 0 && heightInPoints > 0) { img.ScaleAbsolute(widthInPoints, heightInPoints); } else if (widthInPoints > 0) { heightInPoints = img.Height * widthInPoints / img.Width; img.ScaleAbsolute(widthInPoints, heightInPoints); } else if (heightInPoints > 0) { widthInPoints = img.Width * heightInPoints / img.Height; img.ScaleAbsolute(widthInPoints, heightInPoints); } String before; tag.CSS.TryGetValue(CSS.Property.BEFORE, out before); if (before != null) { img.SpacingBefore = float.Parse(before, CultureInfo.InvariantCulture); } String after; tag.CSS.TryGetValue(CSS.Property.AFTER, out after); if (after != null) { img.SpacingAfter = float.Parse(after, CultureInfo.InvariantCulture); } img.WidthPercentage = 0; return(img); }
/* * (non-Javadoc) * * @see * com.itextpdf.tool.xml.css.CssApplier#apply(com.itextpdf.text.Element, * com.itextpdf.tool.xml.Tag) */ public Chunk Apply(Chunk c, Tag t) { String fontName = null; String encoding = BaseFont.CP1252; float size = new FontSizeTranslator().GetFontSize(t); int style = Font.UNDEFINED; BaseColor color = null; IDictionary <String, String> rules = t.CSS; foreach (KeyValuePair <String, String> entry in rules) { String key = entry.Key; String value = entry.Value; if (Util.EqualsIgnoreCase(CSS.Property.FONT_WEIGHT, key)) { if (CSS.Value.BOLD.Contains(value)) { if (style == Font.ITALIC) { style = Font.BOLDITALIC; } else { style = Font.BOLD; } } else { if (style == Font.BOLDITALIC) { style = Font.ITALIC; } else { style = Font.NORMAL; } } } else if (Util.EqualsIgnoreCase(CSS.Property.FONT_STYLE, key)) { if (Util.EqualsIgnoreCase(value, CSS.Value.ITALIC)) { if (style == Font.BOLD) { style = Font.BOLDITALIC; } else { style = Font.ITALIC; } } if (Util.EqualsIgnoreCase(value, CSS.Value.OBLIQUE)) { c.SetSkew(0, 12); } } else if (Util.EqualsIgnoreCase(CSS.Property.FONT_FAMILY, key)) { if (value.Contains(",")) { String[] fonts = value.Split(','); foreach (String s in fonts) { string s2 = s.Trim(); if (!Util.EqualsIgnoreCase(FontFactory.GetFont(s2).Familyname, "unknown")) { fontName = s2; break; } } } else { fontName = value; } } else if (Util.EqualsIgnoreCase(CSS.Property.COLOR, key)) { color = HtmlUtilities.DecodeColor(value); } else if (Util.EqualsIgnoreCase(CSS.Property.LETTER_SPACING, key)) { c.SetCharacterSpacing(utils.ParsePxInCmMmPcToPt(value)); } else if (rules.ContainsKey(CSS.Property.XFA_FONT_HORIZONTAL_SCALE)) // only % allowed; need a catch block NumberFormatExc? { c.SetHorizontalScaling(float.Parse(rules[CSS.Property.XFA_FONT_HORIZONTAL_SCALE].Replace("%", ""), CultureInfo.InvariantCulture) / 100f); } } // following styles are separate from the for each loop, because they are based on font settings like size. if (rules.ContainsKey(CSS.Property.VERTICAL_ALIGN)) { String value = rules[CSS.Property.VERTICAL_ALIGN]; if (Util.EqualsIgnoreCase(value, CSS.Value.SUPER) || Util.EqualsIgnoreCase(value, CSS.Value.TOP) || Util.EqualsIgnoreCase(value, CSS.Value.TEXT_TOP)) { c.SetTextRise((float)(size / 2 + 0.5)); } else if (Util.EqualsIgnoreCase(value, CSS.Value.SUB) || Util.EqualsIgnoreCase(value, CSS.Value.BOTTOM) || Util.EqualsIgnoreCase(value, CSS.Value.TEXT_BOTTOM)) { c.SetTextRise(-size / 2); } else { c.SetTextRise(utils.ParsePxInCmMmPcToPt(value)); } } String xfaVertScale; rules.TryGetValue(CSS.Property.XFA_FONT_VERTICAL_SCALE, out xfaVertScale); if (null != xfaVertScale) // only % allowed; need a catch block NumberFormatExc? { if (xfaVertScale.Contains("%")) { size *= float.Parse(xfaVertScale.Replace("%", ""), CultureInfo.InvariantCulture) / 100; c.SetHorizontalScaling(100 / float.Parse(xfaVertScale.Replace("%", ""), CultureInfo.InvariantCulture)); } } if (rules.ContainsKey(CSS.Property.TEXT_DECORATION)) // Restriction? In html a underline and a line-through is possible on one piece of text. A Chunk can set an underline only once. { String value = rules[CSS.Property.TEXT_DECORATION]; if (Util.EqualsIgnoreCase(CSS.Value.UNDERLINE, value)) { c.SetUnderline(0.75f, -size / 8f); } if (Util.EqualsIgnoreCase(CSS.Value.LINE_THROUGH, value)) { c.SetUnderline(0.75f, size / 4f); } } if (rules.ContainsKey(CSS.Property.BACKGROUND_COLOR)) { c.SetBackground(HtmlUtilities.DecodeColor(rules[CSS.Property.BACKGROUND_COLOR])); } Font f = FontFactory.GetFont(fontName, encoding, BaseFont.EMBEDDED, size, style, color); c.Font = f; return(c); }
public override PdfDiv Apply(PdfDiv div, Tag t, IMarginMemory memory, IPageSizeContainable psc, HtmlPipelineContext context) { IDictionary<String, String> css = t.CSS; float fontSize = FontSizeTranslator.GetInstance().TranslateFontSize(t); if (fontSize == Font.UNDEFINED) { fontSize = FontSizeTranslator.DEFAULT_FONT_SIZE; } 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) { div.TextAlignment = CSS.GetElementAlignment(align); } String widthValue; if (!css.TryGetValue(HTML.Attribute.WIDTH, out widthValue)) { t.Attributes.TryGetValue(HTML.Attribute.WIDTH, out widthValue); } if (widthValue != null) { float pageWidth = psc.PageSize.Width; if (utils.IsNumericValue(widthValue) || utils.IsMetricValue(widthValue)) { div.Width = Math.Min(pageWidth, utils.ParsePxInCmMmPcToPt(widthValue)); } else if (utils.IsRelativeValue(widthValue)) { if (widthValue.Contains(CSS.Value.PERCENTAGE)) { div.PercentageWidth = utils.ParseRelativeValue(widthValue, 1f); } else { div.Width = Math.Min(pageWidth, utils.ParseRelativeValue(widthValue, fontSize)); } } } String heightValue; if (!css.TryGetValue(HTML.Attribute.HEIGHT, out heightValue)) { t.Attributes.TryGetValue(HTML.Attribute.HEIGHT, out heightValue); } if (heightValue != null) { if (utils.IsNumericValue(heightValue) || utils.IsMetricValue(heightValue)) { div.Height = utils.ParsePxInCmMmPcToPt(heightValue); } else if (utils.IsRelativeValue(heightValue)) { if (heightValue.Contains(CSS.Value.PERCENTAGE)) { div.PercentageHeight = utils.ParseRelativeValue(heightValue, 1f); } else { div.Height = utils.ParseRelativeValue(heightValue, fontSize); } } } float? marginTop = null; float? marginBottom = null; foreach (KeyValuePair<String, String> entry in css) { String key = entry.Key; String value = entry.Value; if (Util.EqualsIgnoreCase(key, CSS.Property.LEFT)) { div.Left = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.RIGHT)) { if (div.Width == null || div.Left == null) { div.Right = utils.ParseValueToPt(value, fontSize); } } else if (Util.EqualsIgnoreCase(key, CSS.Property.TOP)) { div.Top = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.BOTTOM)) { if (div.Height == null || div.Top == null) { div.Bottom = utils.ParseValueToPt(value, fontSize); } } else if (Util.EqualsIgnoreCase(key, CSS.Property.BACKGROUND_COLOR)) { div.BackgroundColor = HtmlUtilities.DecodeColor(value); } else if (Util.EqualsIgnoreCase(key, CSS.Property.BACKGROUND_IMAGE)) { string url = utils.ExtractUrl(value); try { Image img = new ImageRetrieve(context.ResourcePath, context.GetImageProvider()).RetrieveImage(url); div.BackgroundImage = img; } catch (NoImageException e) { if (LOG.IsLogging(Level.ERROR)) { LOG.Error(string.Format(LocaleMessages.GetInstance().GetMessage("html.tag.img.failed"), url), e); } } } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_LEFT)) { div.PaddingLeft = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_RIGHT)) { div.PaddingRight = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_TOP)) { div.PaddingTop = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_BOTTOM)) { div.PaddingBottom = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_TOP)) { marginTop = utils.CalculateMarginTop(value, fontSize, memory); } else if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_BOTTOM)) { marginBottom = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.FLOAT)) { if (Util.EqualsIgnoreCase(value, CSS.Value.LEFT)) { div.Float = PdfDiv.FloatType.LEFT; } else if (Util.EqualsIgnoreCase(value, CSS.Value.RIGHT)) { div.Float = PdfDiv.FloatType.RIGHT; } } else if (Util.EqualsIgnoreCase(key, CSS.Property.POSITION)) { if (Util.EqualsIgnoreCase(value, CSS.Value.ABSOLUTE)) { div.Position = PdfDiv.PositionType.ABSOLUTE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.FIXED)) { div.Position = PdfDiv.PositionType.FIXED; } else if (Util.EqualsIgnoreCase(value, CSS.Value.RELATIVE)) { div.Position = PdfDiv.PositionType.RELATIVE; } } else if (Util.EqualsIgnoreCase(key, CSS.Property.DISPLAY)) { if (Util.EqualsIgnoreCase(value, CSS.Value.BLOCK)) { div.Display = PdfDiv.DisplayType.BLOCK; } else if (Util.EqualsIgnoreCase(value, CSS.Value.INLINE)) { div.Display = PdfDiv.DisplayType.INLINE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.INLINE_BLOCK)) { div.Display = PdfDiv.DisplayType.INLINE_BLOCK; } else if (Util.EqualsIgnoreCase(value, CSS.Value.INLINE_TABLE)) { div.Display = PdfDiv.DisplayType.INLINE_TABLE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.LIST_ITEM)) { div.Display = PdfDiv.DisplayType.LIST_ITEM; } else if (Util.EqualsIgnoreCase(value, CSS.Value.NONE)) { div.Display = PdfDiv.DisplayType.NONE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.RUN_IN)) { div.Display = PdfDiv.DisplayType.RUN_IN; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE)) { div.Display = PdfDiv.DisplayType.TABLE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_CAPTION)) { div.Display = PdfDiv.DisplayType.TABLE_CAPTION; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_CELL)) { div.Display = PdfDiv.DisplayType.TABLE_CELL; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_COLUMN_GROUP)) { div.Display = PdfDiv.DisplayType.TABLE_COLUMN_GROUP; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_COLUMN)) { div.Display = PdfDiv.DisplayType.TABLE_COLUMN; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_FOOTER_GROUP)) { div.Display = PdfDiv.DisplayType.TABLE_FOOTER_GROUP; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_HEADER_GROUP)) { div.Display = PdfDiv.DisplayType.TABLE_HEADER_GROUP; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_ROW)) { div.Display = PdfDiv.DisplayType.TABLE_ROW; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_ROW_GROUP)) { div.Display = PdfDiv.DisplayType.TABLE_ROW_GROUP; } } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_TOP_STYLE, key)) { if (Util.EqualsIgnoreCase(CSS.Value.DOTTED, value)) { div.BorderStyle = PdfDiv.BorderTopStyle.DOTTED; } else if (Util.EqualsIgnoreCase(CSS.Value.DASHED, value)) { div.BorderStyle = PdfDiv.BorderTopStyle.DASHED; } else if (Util.EqualsIgnoreCase(CSS.Value.SOLID, value)) { div.BorderStyle = PdfDiv.BorderTopStyle.SOLID; } else if (Util.EqualsIgnoreCase(CSS.Value.DOUBLE, value)) { div.BorderStyle = PdfDiv.BorderTopStyle.DOUBLE; } else if (Util.EqualsIgnoreCase(CSS.Value.GROOVE, value)) { div.BorderStyle = PdfDiv.BorderTopStyle.GROOVE; } else if (Util.EqualsIgnoreCase(CSS.Value.RIDGE, value)) { div.BorderStyle = PdfDiv.BorderTopStyle.RIDGE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.INSET)) { div.BorderStyle = PdfDiv.BorderTopStyle.INSET; } else if (Util.EqualsIgnoreCase(value, CSS.Value.OUTSET)) { div.BorderStyle = PdfDiv.BorderTopStyle.OUTSET; } } else if (Util.EqualsIgnoreCase(key, CSS.Property.PAGE_BREAK_INSIDE)) { if (Util.EqualsIgnoreCase(value, CSS.Value.AVOID)) { div.KeepTogether = true; } } //TODO: border, background properties. } return div; }
/* (non-Javadoc) * @see com.itextpdf.tool.xml.css.CssApplier#apply(com.itextpdf.text.Element, com.itextpdf.tool.xml.Tag) */ public Paragraph Apply(Paragraph p, Tag t, IMarginMemory configuration) { /*if (this.configuration.GetRootTags().Contains(t.Name)) { * m.SetLeading(t); * } else { * m.SetVariablesBasedOnChildren(t); * }*/ float fontSize = FontSizeTranslator.GetInstance().GetFontSize(t); float lmb = 0; bool hasLMB = false; IDictionary <String, String> css = t.CSS; foreach (KeyValuePair <String, String> entry in css) { String key = entry.Key; String value = entry.Value; if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_TOP, key)) { p.SpacingBefore = p.SpacingBefore + utils.CalculateMarginTop(value, fontSize, configuration); } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_TOP, key)) { p.SpacingBefore = p.SpacingBefore + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_BOTTOM, key)) { float after = utils.ParseValueToPt(value, fontSize); p.SpacingAfter = p.SpacingAfter + after; lmb = after; hasLMB = true; } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_BOTTOM, key)) { p.SpacingAfter = p.SpacingAfter + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_LEFT, key)) { p.IndentationLeft = p.IndentationLeft + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_RIGHT, key)) { p.IndentationRight = p.IndentationRight + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_LEFT, key)) { p.IndentationLeft = p.IndentationLeft + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_RIGHT, key)) { p.IndentationRight = p.IndentationRight + utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.TEXT_ALIGN, key)) { if (Util.EqualsIgnoreCase(CSS.Value.RIGHT, value)) { p.Alignment = Element.ALIGN_RIGHT; } else if (Util.EqualsIgnoreCase(CSS.Value.CENTER, value)) { p.Alignment = Element.ALIGN_CENTER; } else if (Util.EqualsIgnoreCase(CSS.Value.LEFT, value)) { p.Alignment = Element.ALIGN_LEFT; } else if (Util.EqualsIgnoreCase(CSS.Value.JUSTIFY, value)) { p.Alignment = Element.ALIGN_JUSTIFIED; } } else if (Util.EqualsIgnoreCase(CSS.Property.TEXT_INDENT, key)) { p.FirstLineIndent = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(CSS.Property.LINE_HEIGHT, key)) { if (utils.IsNumericValue(value)) { p.Leading = float.Parse(value, CultureInfo.InvariantCulture) * fontSize; } else if (utils.IsRelativeValue(value)) { p.Leading = utils.ParseRelativeValue(value, fontSize); } else if (utils.IsMetricValue(value)) { p.Leading = utils.ParsePxInCmMmPcToPt(value); } } } // setDefaultMargin to largestFont if no margin-bottom is set and p-tag is child of the root tag. /*if (null != t.Parent) { * String parent = t.Parent.Name; * if (!css.ContainsKey(CSS.Property.MARGIN_TOP) && configuration.GetRootTags().Contains(parent)) { * p.SpacingBefore = p.SpacingBefore+utils.CalculateMarginTop(fontSize.ToString(CultureInfo.InvariantCulture)+"pt", 0, configuration); * } * if (!css.ContainsKey(CSS.Property.MARGIN_BOTTOM) && configuration.GetRootTags().Contains(parent)) { * p.SpacingAfter = p.SpacingAfter+fontSize; * css[CSS.Property.MARGIN_BOTTOM] = fontSize.ToString(CultureInfo.InvariantCulture)+"pt"; * lmb = fontSize; * hasLMB = true; * } * p.Leading = m.GetLargestLeading(); * if (p.Alignment == -1) { * p.Alignment = Element.ALIGN_LEFT; * } * }*/ if (hasLMB) { configuration.LastMarginBottom = lmb; } //TODO this only work around for applaying of font properties to paragraph Chunk dummy = new ChunkCssApplier().Apply(new Chunk("dummy"), t); p.Font = dummy.Font; return(p); }
/** * * @param c the Chunk to apply CSS to. * @param t the tag containing the chunk data * @return the styled chunk */ public Chunk Apply(Chunk c, Tag t) { Font f = ApplyFontStyles(t); float size = f.Size; String value = null; IDictionary <String, String> rules = t.CSS; foreach (KeyValuePair <String, String> entry in rules) { String key = entry.Key; value = entry.Value; if (Util.EqualsIgnoreCase(CSS.Property.FONT_STYLE, key)) { if (Util.EqualsIgnoreCase(CSS.Value.OBLIQUE, value)) { c.SetSkew(0, 12); } } else if (Util.EqualsIgnoreCase(CSS.Property.LETTER_SPACING, key)) { c.SetCharacterSpacing(utils.ParsePxInCmMmPcToPt(value)); } else if (Util.EqualsIgnoreCase(CSS.Property.XFA_FONT_HORIZONTAL_SCALE, key)) { // only % allowed; need a catch block NumberFormatExc? c.SetHorizontalScaling( float.Parse(value.Replace("%", "")) / 100); } } // following styles are separate from the for each loop, because they are based on font settings like size. if (rules.TryGetValue(CSS.Property.VERTICAL_ALIGN, out value)) { if (Util.EqualsIgnoreCase(CSS.Value.SUPER, value) || Util.EqualsIgnoreCase(CSS.Value.TOP, value) || Util.EqualsIgnoreCase(CSS.Value.TEXT_TOP, value)) { c.SetTextRise((float)(size / 2 + 0.5)); } else if (Util.EqualsIgnoreCase(CSS.Value.SUB, value) || Util.EqualsIgnoreCase(CSS.Value.BOTTOM, value) || Util.EqualsIgnoreCase(CSS.Value.TEXT_BOTTOM, value)) { c.SetTextRise(-size / 2); } else { c.SetTextRise(utils.ParsePxInCmMmPcToPt(value)); } } String xfaVertScale; if (rules.TryGetValue(CSS.Property.XFA_FONT_VERTICAL_SCALE, out xfaVertScale)) { if (xfaVertScale.Contains("%")) { size *= float.Parse(xfaVertScale.Replace("%", "")) / 100; c.SetHorizontalScaling(100 / float.Parse(xfaVertScale.Replace("%", ""))); } } if (rules.TryGetValue(CSS.Property.TEXT_DECORATION, out value)) { // Restriction? In html a underline and a line-through is possible on one piece of text. A Chunk can set an underline only once. if (Util.EqualsIgnoreCase(CSS.Value.UNDERLINE, value)) { c.SetUnderline(0.75f, -size / 8f); } if (Util.EqualsIgnoreCase(CSS.Value.LINE_THROUGH, value)) { c.SetUnderline(0.75f, size / 4f); } } if (rules.TryGetValue(CSS.Property.BACKGROUND_COLOR, out value)) { c.SetBackground(HtmlUtilities.DecodeColor(value)); } f.Size = size; c.Font = f; return(c); }
/* * (non-Javadoc) * * @see * com.itextpdf.tool.xml.ITagProcessor#endElement(com.itextpdf.tool.xml.Tag, * java.util.List, com.itextpdf.text.Document) */ public override IList <IElement> End(IWorkerContext ctx, Tag tag, IList <IElement> currentContent) { try { bool percentage = false; String widthValue = null; tag.CSS.TryGetValue(HTML.Attribute.WIDTH, out widthValue); if (!tag.CSS.TryGetValue(HTML.Attribute.WIDTH, out widthValue) && !tag.Attributes.TryGetValue(HTML.Attribute.WIDTH, out widthValue)) { widthValue = null; } if (widthValue != null && widthValue.Trim().EndsWith("%")) { percentage = true; } int numberOfColumns = 0; List <TableRowElement> tableRows = new List <TableRowElement>(currentContent.Count); IList <IElement> invalidRowElements = new List <IElement>(1); String repeatHeader; tag.CSS.TryGetValue(CSS.Property.REPEAT_HEADER, out repeatHeader); String repeatFooter; tag.CSS.TryGetValue(CSS.Property.REPEAT_FOOTER, out repeatFooter); int headerRows = 0; int footerRows = 0; foreach (IElement e in currentContent) { int localNumCols = 0; if (e is TableRowElement) { TableRowElement tableRowElement = (TableRowElement)e; foreach (HtmlCell cell in tableRowElement.Content) { localNumCols += cell.Colspan; } if (localNumCols > numberOfColumns) { numberOfColumns = localNumCols; } tableRows.Add(tableRowElement); if (repeatHeader != null && Util.EqualsIgnoreCase(repeatHeader, "yes") && tableRowElement.RowPlace.Equals(TableRowElement.Place.HEADER)) { headerRows++; } if (repeatFooter != null && Util.EqualsIgnoreCase(repeatFooter, "yes") && tableRowElement.RowPlace.Equals(TableRowElement.Place.FOOTER)) { footerRows++; } } else { invalidRowElements.Add(e); } } if (repeatFooter == null || !Util.EqualsIgnoreCase(repeatFooter, "yes")) { InsertionSort <TableRowElement>(tableRows, delegate(TableRowElement o1, TableRowElement o2) { return(o1.RowPlace.Normal.CompareTo(o2.RowPlace.Normal)); }); } else { InsertionSort <TableRowElement>(tableRows, delegate(TableRowElement o1, TableRowElement o2) { return(o1.RowPlace.Repeated.CompareTo(o2.RowPlace.Repeated)); }); } PdfPTable table = new PdfPTable(numberOfColumns); table.HeaderRows = headerRows + footerRows; table.FooterRows = footerRows; table.HorizontalAlignment = Element.ALIGN_LEFT; TableStyleValues styleValues = SetStyleValues(tag); table.TableEvent = new TableBorderEvent(styleValues); SetVerticalMargin(table, tag, styleValues, ctx); WidenLastCell(tableRows, styleValues.HorBorderSpacing); float[] columnWidths = new float[numberOfColumns]; float[] widestWords = new float[numberOfColumns]; float[] fixedWidths = new float[numberOfColumns]; float[] colspanWidestWords = new float[numberOfColumns]; int[] rowspanValue = new int[numberOfColumns]; float largestColumn = 0; float largestColspanColumn = 0; int indexOfLargestColumn = -1; int indexOfLargestColspanColumn = -1; // Initial fill of the widths arrays foreach (TableRowElement row in tableRows) { int column = 0; foreach (HtmlCell cell in row.Content) { // check whether the current column should be skipped due to a // rowspan value of higher cell in this column. while (rowspanValue[column] > 1) { rowspanValue[column] = rowspanValue[column] - 1; ++column; } // sets a rowspan counter for current column (counter not // needed for last column). if (cell.Rowspan > 1 && column != numberOfColumns - 1) { rowspanValue[column] = cell.Rowspan - 1; } int colspan = cell.Colspan; if (cell.FixedWidth != 0) { float fixedWidth = cell.FixedWidth + GetCellStartWidth(cell); fixedWidth /= colspan; for (int i = 0; i < colspan; i++) { int c = column + i; if (fixedWidth > fixedWidths[c]) { fixedWidths[c] = fixedWidth; columnWidths[c] = fixedWidth; } } } if (cell.CompositeElements != null) { float[] widthValues = SetCellWidthAndWidestWord(cell); float cellWidth = widthValues[0] / colspan; float widestWordOfCell = widthValues[1] / colspan; for (int i = 0; i < colspan; i++) { int c = column + i; if (fixedWidths[c] == 0 && cellWidth > columnWidths[c]) { columnWidths[c] = cellWidth; if (colspan == 1) { if (cellWidth > largestColumn) { largestColumn = cellWidth; indexOfLargestColumn = c; } } else { if (cellWidth > largestColspanColumn) { largestColspanColumn = cellWidth; indexOfLargestColspanColumn = c; } } } if (colspan == 1) { if (widestWordOfCell > widestWords[c]) { widestWords[c] = widestWordOfCell; } } else { if (widestWordOfCell > colspanWidestWords[c]) { colspanWidestWords[c] = widestWordOfCell; } } } } if (colspan > 1) { if (LOG.IsLogging(Level.TRACE)) { LOG.Trace(String.Format(LocaleMessages.GetInstance().GetMessage(LocaleMessages.COLSPAN), colspan)); } column += colspan - 1; } column++; } } if (indexOfLargestColumn == -1) { indexOfLargestColumn = indexOfLargestColspanColumn; if (indexOfLargestColumn == -1) { indexOfLargestColumn = 0; } for (int column = 0; column < numberOfColumns; column++) { widestWords[column] = colspanWidestWords[column]; } } float outerWidth = GetTableOuterWidth(tag, styleValues.HorBorderSpacing, ctx); float initialTotalWidth = GetTableWidth(columnWidths, 0); // float targetWidth = calculateTargetWidth(tag, columnWidths, outerWidth, ctx); float targetWidth = 0; HtmlPipelineContext htmlPipelineContext = GetHtmlPipelineContext(ctx); float max = htmlPipelineContext.PageSize.Width - outerWidth; bool tableWidthFixed = false; if (tag.Attributes.ContainsKey(CSS.Property.WIDTH) || tag.CSS.ContainsKey(CSS.Property.WIDTH)) { targetWidth = new WidthCalculator().GetWidth(tag, htmlPipelineContext.GetRootTags(), htmlPipelineContext.PageSize.Width); if (targetWidth > max) { targetWidth = max; } tableWidthFixed = true; } else if (initialTotalWidth <= max) { targetWidth = initialTotalWidth; } else if (null == tag.Parent || (null != tag.Parent && htmlPipelineContext.GetRootTags().Contains(tag.Parent.Name))) { targetWidth = max; } else /* this table is an inner table and width adjustment is done in outer table */ { targetWidth = GetTableWidth(columnWidths, outerWidth); } float totalFixedColumnWidth = GetTableWidth(fixedWidths, 0); float targetPercentage = 0; if (totalFixedColumnWidth == initialTotalWidth) // all column widths are fixed { targetPercentage = targetWidth / initialTotalWidth; if (initialTotalWidth > targetWidth) { for (int column = 0; column < columnWidths.Length; column++) { columnWidths[column] *= targetPercentage; } } else if (tableWidthFixed && targetPercentage != 1) { for (int column = 0; column < columnWidths.Length; column++) { columnWidths[column] *= targetPercentage; } } } else { targetPercentage = (targetWidth - totalFixedColumnWidth) / (initialTotalWidth - totalFixedColumnWidth); // Reduce width of columns if the columnWidth array + borders + // paddings // is too large for the given targetWidth. if (initialTotalWidth > targetWidth) { float leftToReduce = 0; for (int column = 0; column < columnWidths.Length; column++) { if (fixedWidths[column] == 0) { // Reduce width of the column to its targetWidth, if // widestWord of column still fits in the targetWidth of // the // column. if (widestWords[column] <= columnWidths[column] * targetPercentage) { columnWidths[column] *= targetPercentage; // else take the widest word and calculate space // left to // reduce. } else { columnWidths[column] = widestWords[column]; leftToReduce += widestWords[column] - columnWidths[column] * targetPercentage; } // if widestWord of a column does not fit in the // fixedWidth, // set the column width to the widestWord. } else if (fixedWidths[column] < widestWords[column]) { columnWidths[column] = widestWords[column]; leftToReduce += widestWords[column] - fixedWidths[column]; } } if (leftToReduce != 0) { // Reduce width of the column with the most text, if its // widestWord still fits in the reduced column. if (widestWords[indexOfLargestColumn] <= columnWidths[indexOfLargestColumn] - leftToReduce) { columnWidths[indexOfLargestColumn] -= leftToReduce; } else // set all columns to their minimum, with the // widestWord array. { for (int column = 0; leftToReduce != 0 && column < columnWidths.Length; column++) { if (fixedWidths[column] == 0 && columnWidths[column] > widestWords[column]) { float difference = columnWidths[column] - widestWords[column]; if (difference <= leftToReduce) { leftToReduce -= difference; columnWidths[column] = widestWords[column]; } else { columnWidths[column] -= leftToReduce; leftToReduce = 0; } } } if (leftToReduce != 0) { // If the table has an insufficient fixed width // by // an // attribute or style, try to enlarge the table // to // its // minimum width (= widestWords array). float pageWidth = GetHtmlPipelineContext(ctx).PageSize.Width; if (GetTableWidth(widestWords, outerWidth) < pageWidth) { targetWidth = GetTableWidth(widestWords, outerWidth); leftToReduce = 0; } else { // If all columnWidths are set to the // widestWordWidths and the table is still // to // wide // content will fall off the edge of a page, // which // is similar to HTML. targetWidth = pageWidth - outerWidth; leftToReduce = 0; } } } } // Enlarge width of columns to fit the targetWidth. } else if (initialTotalWidth < targetWidth) { for (int column = 0; column < columnWidths.Length; column++) { if (fixedWidths[column] == 0) { columnWidths[column] *= targetPercentage; } } } } try { table.SetTotalWidth(columnWidths); table.LockedWidth = true; table.DefaultCell.Border = Rectangle.NO_BORDER; } catch (DocumentException e) { throw new RuntimeWorkerException(LocaleMessages.GetInstance().GetMessage(LocaleMessages.NO_CUSTOM_CONTEXT), e); } float?tableHeight = new HeightCalculator().GetHeight(tag, GetHtmlPipelineContext(ctx).PageSize.Height); float?tableRowHeight = null; if (tableHeight != null && tableHeight > 0) { tableRowHeight = tableHeight / tableRows.Count; } int rowNumber = 0; foreach (TableRowElement row in tableRows) { int columnNumber = -1; float?computedRowHeight = null; /*if ( tableHeight != null && tableRows.IndexOf(row) == tableRows.Count - 1) { * float computedTableHeigt = table.CalculateHeights(); * computedRowHeight = tableHeight - computedTableHeigt; * }*/ foreach (HtmlCell cell in row.Content) { IList <IElement> compositeElements = cell.CompositeElements; if (compositeElements != null) { foreach (IElement baseLevel in compositeElements) { if (baseLevel is PdfPTable) { TableStyleValues cellValues = cell.CellValues; float totalBordersWidth = cellValues.IsLastInRow ? styleValues.HorBorderSpacing * 2 : styleValues.HorBorderSpacing; totalBordersWidth += cellValues.BorderWidthLeft + cellValues.BorderWidthRight; float columnWidth = 0; for (int currentColumnNumber = columnNumber + 1; currentColumnNumber <= columnNumber + cell.Colspan; currentColumnNumber++) { columnWidth += columnWidths[currentColumnNumber]; } IPdfPTableEvent tableEvent = ((PdfPTable)baseLevel).TableEvent; TableStyleValues innerStyleValues = ((TableBorderEvent)tableEvent).TableStyleValues; totalBordersWidth += innerStyleValues.BorderWidthLeft; totalBordersWidth += innerStyleValues.BorderWidthRight; ((PdfPTable)baseLevel).TotalWidth = columnWidth - totalBordersWidth; } } } columnNumber += cell.Colspan; table.AddCell(cell); } table.CompleteRow(); if ((computedRowHeight == null || computedRowHeight <= 0) && tableRowHeight != null) { computedRowHeight = tableRowHeight; } if (computedRowHeight != null && computedRowHeight > 0) { float rowHeight = table.GetRow(rowNumber).MaxHeights; if (rowHeight < computedRowHeight) { table.GetRow(rowNumber).MaxHeights = computedRowHeight.Value; } else if (tableRowHeight != null && tableRowHeight < rowHeight) { tableRowHeight = (tableHeight - rowHeight - rowNumber * tableRowHeight) / (tableRows.Count - rowNumber - 1); } } rowNumber++; } if (percentage) { table.WidthPercentage = utils.ParsePxInCmMmPcToPt(widthValue); table.LockedWidth = false; } List <IElement> elems = new List <IElement>(); if (invalidRowElements.Count > 0) { // all invalid row elements taken as caption int i = 0; Tag captionTag = tag.Children[i++]; while (!Util.EqualsIgnoreCase(captionTag.Name, HTML.Tag.CAPTION) && i < tag.Children.Count) { captionTag = tag.Children[i]; i++; } String captionSideValue; captionTag.CSS.TryGetValue(CSS.Property.CAPTION_SIDE, out captionSideValue); if (captionSideValue != null && Util.EqualsIgnoreCase(captionSideValue, CSS.Value.BOTTOM)) { elems.Add(table); elems.AddRange(invalidRowElements); } else { elems.AddRange(invalidRowElements); elems.Add(table); } } else { elems.Add(table); } return(elems); } catch (NoCustomContextException e) { throw new RuntimeWorkerException(LocaleMessages.GetInstance().GetMessage(LocaleMessages.NO_CUSTOM_CONTEXT), e); } }
virtual public PdfDiv apply(PdfDiv div, Tag t, IMarginMemory memory, IPageSizeContainable psc) { IDictionary <String, String> css = t.CSS; float fontSize = FontSizeTranslator.GetInstance().TranslateFontSize(t); if (fontSize == Font.UNDEFINED) { fontSize = FontSizeTranslator.DEFAULT_FONT_SIZE; } 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(align, CSS.Value.CENTER)) { div.TextAlignment = Element.ALIGN_CENTER; } else if (Util.EqualsIgnoreCase(align, CSS.Value.LEFT)) { div.TextAlignment = Element.ALIGN_LEFT; } else if (Util.EqualsIgnoreCase(align, CSS.Value.RIGHT)) { div.TextAlignment = Element.ALIGN_RIGHT; } else if (Util.EqualsIgnoreCase(align, CSS.Value.JUSTIFY)) { div.TextAlignment = Element.ALIGN_JUSTIFIED; } } String widthValue; if (!t.CSS.TryGetValue(HTML.Attribute.WIDTH, out widthValue)) { t.Attributes.TryGetValue(HTML.Attribute.WIDTH, out widthValue); } if (widthValue != null) { float pageWidth = psc.PageSize.Width; if (utils.IsNumericValue(widthValue) || utils.IsMetricValue(widthValue)) { div.Width = Math.Min(pageWidth, utils.ParsePxInCmMmPcToPt(widthValue)); } else if (utils.IsRelativeValue(widthValue)) { if (widthValue.Contains(CSS.Value.PERCENTAGE)) { div.PercentageWidth = utils.ParseRelativeValue(widthValue, 1f); } else { div.Width = Math.Min(pageWidth, utils.ParseRelativeValue(widthValue, fontSize)); } } } String heightValue; if (!t.CSS.TryGetValue(HTML.Attribute.HEIGHT, out heightValue)) { t.Attributes.TryGetValue(HTML.Attribute.HEIGHT, out heightValue); } if (heightValue != null) { if (utils.IsNumericValue(heightValue) || utils.IsMetricValue(heightValue)) { div.Height = utils.ParsePxInCmMmPcToPt(heightValue); } else if (utils.IsRelativeValue(heightValue)) { if (heightValue.Contains(CSS.Value.PERCENTAGE)) { div.PercentageHeight = utils.ParseRelativeValue(heightValue, 1f); } else { div.Height = utils.ParseRelativeValue(heightValue, fontSize); } } } float?marginTop = null; float?marginBottom = null; foreach (KeyValuePair <String, String> entry in css) { String key = entry.Key; String value = entry.Value; if (Util.EqualsIgnoreCase(key, CSS.Property.LEFT)) { div.Left = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.RIGHT)) { if (div.Width == null || div.Left == null) { div.Right = utils.ParseValueToPt(value, fontSize); } } else if (Util.EqualsIgnoreCase(key, CSS.Property.TOP)) { div.Top = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.BOTTOM)) { if (div.Height == null || div.Top == null) { div.Bottom = utils.ParseValueToPt(value, fontSize); } } else if (Util.EqualsIgnoreCase(key, CSS.Property.BACKGROUND_COLOR)) { div.BackgroundColor = HtmlUtilities.DecodeColor(value); } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_LEFT)) { div.PaddingLeft = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_RIGHT)) { div.PaddingRight = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_TOP)) { div.PaddingTop = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.PADDING_BOTTOM)) { div.PaddingBottom = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_TOP)) { marginTop = utils.CalculateMarginTop(value, fontSize, memory); } else if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_BOTTOM)) { marginBottom = utils.ParseValueToPt(value, fontSize); } else if (Util.EqualsIgnoreCase(key, CSS.Property.FLOAT)) { if (Util.EqualsIgnoreCase(value, CSS.Value.LEFT)) { div.Float = PdfDiv.FloatType.LEFT; } else if (Util.EqualsIgnoreCase(value, CSS.Value.RIGHT)) { div.Float = PdfDiv.FloatType.RIGHT; } } else if (Util.EqualsIgnoreCase(key, CSS.Property.POSITION)) { if (Util.EqualsIgnoreCase(value, CSS.Value.ABSOLUTE)) { div.Position = PdfDiv.PositionType.ABSOLUTE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.FIXED)) { div.Position = PdfDiv.PositionType.FIXED; } else if (Util.EqualsIgnoreCase(value, CSS.Value.RELATIVE)) { div.Position = PdfDiv.PositionType.RELATIVE; } } else if (Util.EqualsIgnoreCase(key, CSS.Property.DISPLAY)) { if (Util.EqualsIgnoreCase(value, CSS.Value.BLOCK)) { div.Display = PdfDiv.DisplayType.BLOCK; } else if (Util.EqualsIgnoreCase(value, CSS.Value.INLINE)) { div.Display = PdfDiv.DisplayType.INLINE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.INLINE_BLOCK)) { div.Display = PdfDiv.DisplayType.INLINE_BLOCK; } else if (Util.EqualsIgnoreCase(value, CSS.Value.INLINE_TABLE)) { div.Display = PdfDiv.DisplayType.INLINE_TABLE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.LIST_ITEM)) { div.Display = PdfDiv.DisplayType.LIST_ITEM; } else if (Util.EqualsIgnoreCase(value, CSS.Value.NONE)) { div.Display = PdfDiv.DisplayType.NONE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.RUN_IN)) { div.Display = PdfDiv.DisplayType.RUN_IN; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE)) { div.Display = PdfDiv.DisplayType.TABLE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_CAPTION)) { div.Display = PdfDiv.DisplayType.TABLE_CAPTION; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_CELL)) { div.Display = PdfDiv.DisplayType.TABLE_CELL; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_COLUMN_GROUP)) { div.Display = PdfDiv.DisplayType.TABLE_COLUMN_GROUP; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_COLUMN)) { div.Display = PdfDiv.DisplayType.TABLE_COLUMN; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_FOOTER_GROUP)) { div.Display = PdfDiv.DisplayType.TABLE_FOOTER_GROUP; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_HEADER_GROUP)) { div.Display = PdfDiv.DisplayType.TABLE_HEADER_GROUP; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_ROW)) { div.Display = PdfDiv.DisplayType.TABLE_ROW; } else if (Util.EqualsIgnoreCase(value, CSS.Value.TABLE_ROW_GROUP)) { div.Display = PdfDiv.DisplayType.TABLE_ROW_GROUP; } } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_TOP_STYLE, key)) { if (Util.EqualsIgnoreCase(CSS.Value.DOTTED, value)) { div.BorderStyle = PdfDiv.BorderTopStyle.DOTTED; } else if (Util.EqualsIgnoreCase(CSS.Value.DASHED, value)) { div.BorderStyle = PdfDiv.BorderTopStyle.DASHED; } else if (Util.EqualsIgnoreCase(CSS.Value.SOLID, value)) { div.BorderStyle = PdfDiv.BorderTopStyle.SOLID; } else if (Util.EqualsIgnoreCase(CSS.Value.DOUBLE, value)) { div.BorderStyle = PdfDiv.BorderTopStyle.DOUBLE; } else if (Util.EqualsIgnoreCase(CSS.Value.GROOVE, value)) { div.BorderStyle = PdfDiv.BorderTopStyle.GROOVE; } else if (Util.EqualsIgnoreCase(CSS.Value.RIDGE, value)) { div.BorderStyle = PdfDiv.BorderTopStyle.RIDGE; } else if (Util.EqualsIgnoreCase(value, CSS.Value.INSET)) { div.BorderStyle = PdfDiv.BorderTopStyle.INSET; } else if (Util.EqualsIgnoreCase(value, CSS.Value.OUTSET)) { div.BorderStyle = PdfDiv.BorderTopStyle.OUTSET; } } //TODO: border, background properties. } return(div); }