/** * @param e * @param t * @param ctx * @return the element with CSS applied onto */ public IElement Apply(IElement e, Tag t, IMarginMemory mm, IPageSizeContainable psc, IImageProvider ip) { // warning, mapping is done by instance of, make sure to add things in the right order when adding more. if (e is Chunk) // covers TabbedChunk & Chunk { e = chunk.Apply((Chunk)e, t); } else if (e is Paragraph) { e = paragraph.Apply((Paragraph)e, t, mm); } else if (e is NoNewLineParagraph) { e = nonewlineparagraph.Apply((NoNewLineParagraph)e, t, mm); } else if (e is HtmlCell) { e = htmlcell.Apply((HtmlCell)e, t, mm, psc); } else if (e is List) { e = list.Apply((List)e, t, ip); } else if (e is LineSeparator) { e = lineseparator.Apply((LineSeparator)e, t, psc); } else if (e is iTextSharp.text.Image) { e = image.Apply((iTextSharp.text.Image)e, t); } return(e); }
IElement ICssApplier.Apply(IElement e, Tag t, IMarginMemory mm, IPageSizeContainable psc, HtmlPipelineContext ctx) { if (!(e is T)) { throw new ArgumentException(string.Format("Expected element of type {0}, but was {1}", typeof(T).Name, e.GetType().Name)); } return(Apply((T)e, t, mm, psc, ctx)); }
/** * Calculates the margin top or spacingBefore based on the given value and the last margin bottom. * <br /><br /> * In HTML the margin-bottom of a tag overlaps with the margin-top of a following tag. * This method simulates this behavior by subtracting the margin-top value of the given tag from the margin-bottom of the previous tag. The remaining value is returned or if the margin-bottom value is the largest, 0 is returned * @param value float containing the margin-top value. * @param configuration XmlWorkerConfig containing the last margin bottom. * @return an offset */ public float CalculateMarginTop(float value, IMarginMemory configuration) { float marginTop = value; try { float marginBottom = configuration.LastMarginBottom; marginTop = (marginTop > marginBottom)?marginTop - marginBottom:0; } catch (NoDataException) { } return(marginTop); }
/* (non-Javadoc) * @see com.itextpdf.tool.xml.html.CssAppliers#apply(com.itextpdf.text.Element, com.itextpdf.tool.xml.Tag, com.itextpdf.tool.xml.css.apply.MarginMemory, com.itextpdf.tool.xml.css.apply.PageSizeContainable, com.itextpdf.tool.xml.pipeline.html.ImageProvider) */ public virtual IElement Apply(IElement e, Tag t, IMarginMemory mm, IPageSizeContainable psc, HtmlPipelineContext ctx) { ICssApplier c = null; foreach (KeyValuePair <Type, ICssApplier> entry in map) { if (entry.Key.IsInstanceOfType(e)) { c = entry.Value; break; } } if (c == null) { throw new Exception(); } e = c.Apply(e, t, mm, psc, ctx); return(e); }
/** * @param e * @param t * @param ctx * @return the element with CSS applied onto */ public IElement Apply(IElement e, Tag t, IMarginMemory mm, IPageSizeContainable psc, IImageProvider ip) { // warning, mapping is done by instance of, make sure to add things in the right order when adding more. if (e is Chunk) { // covers TabbedChunk & Chunk e = chunk.Apply((Chunk) e, t); } else if (e is Paragraph) { e = paragraph.Apply((Paragraph) e, t, mm); } else if (e is NoNewLineParagraph) { e = nonewlineparagraph.Apply((NoNewLineParagraph) e, t, mm); } else if (e is HtmlCell) { e = htmlcell.Apply((HtmlCell) e, t, mm, psc); } else if (e is List) { e = list.Apply((List) e, t, ip); } else if (e is LineSeparator) { e = lineseparator.Apply((LineSeparator) e, t, psc); } else if (e is iTextSharp.text.Image) { e = image.Apply((iTextSharp.text.Image) e, t); } return e; }
/* * (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); }
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); }
/** * Calculates the margin top or spacingBefore based on the given value and the last margin bottom. * <br /><br /> * In HTML the margin-bottom of a tag overlaps with the margin-top of a following tag. * This method simulates this behavior by subtracting the margin-top value of the given tag from the margin-bottom of the previous tag. The remaining value is returned or if the margin-bottom value is the largest, 0 is returned * @param value the margin-top value of the given tag. * @param largestFont used if a relative value was given to calculate margin. * @param configuration XmlWorkerConfig containing the last margin bottom. * @return an offset */ public float CalculateMarginTop(String value, float largestFont, IMarginMemory configuration) { return(CalculateMarginTop(ParseValueToPt(value, largestFont), configuration)); }
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; }
/** * 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); }
/** * * @param c the Chunk to apply CSS to. * @param t the tag containing the chunk data * @return the styled chunk */ public override Chunk Apply(Chunk c, Tag t, IMarginMemory mm, IPageSizeContainable psc, HtmlPipelineContext ctx) { 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("%", ""), CultureInfo.InvariantCulture) / 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("%", ""), CultureInfo.InvariantCulture) / 100; c.SetHorizontalScaling(100 / float.Parse(xfaVertScale.Replace("%", ""), CultureInfo.InvariantCulture)); } } 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(null, 0.75f, 0, 0, -0.125f, PdfContentByte.LINE_CAP_BUTT); } if (Util.EqualsIgnoreCase(CSS.Value.LINE_THROUGH, curValue)) { c.SetUnderline(null, 0.75f, 0, 0, 0.25f, PdfContentByte.LINE_CAP_BUTT); } } } 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, CultureInfo.InvariantCulture) * 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); }
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); }
public abstract T Apply(T e, Tag t, IMarginMemory mm, IPageSizeContainable psc, HtmlPipelineContext ctx);
/* (non-Javadoc) * @see com.itextpdf.tool.xml.css.CssApplier#apply(com.itextpdf.text.Element, com.itextpdf.tool.xml.Tag) */ virtual public NoNewLineParagraph Apply(NoNewLineParagraph 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); } } // setDefaultMargin to largestFont if no margin-top 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; } return p; }
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); }
public override LineSeparator Apply(LineSeparator ls, Tag t, IMarginMemory mm, IPageSizeContainable psc, HtmlPipelineContext ctx) { float lineWidth = 1; IDictionary <String, String> css = t.CSS; if (t.Attributes.ContainsKey(HTML.Attribute.SIZE)) { lineWidth = CssUtils.GetInstance().ParsePxInCmMmPcToPt(t.Attributes[HTML.Attribute.SIZE]); } else if (css.ContainsKey(CSS.Property.HEIGHT)) { lineWidth = CssUtils.GetInstance().ParsePxInCmMmPcToPt(css[CSS.Property.HEIGHT]); } ls.LineWidth = lineWidth; BaseColor lineColor = BaseColor.BLACK; if (t.Attributes.ContainsKey(CSS.Property.COLOR)) { lineColor = HtmlUtilities.DecodeColor(t.Attributes[CSS.Property.COLOR]); } else if (css.ContainsKey(CSS.Property.COLOR)) { lineColor = HtmlUtilities.DecodeColor(css[CSS.Property.COLOR]); } else if (css.ContainsKey(CSS.Property.BACKGROUND_COLOR)) { lineColor = HtmlUtilities.DecodeColor(css[CSS.Property.BACKGROUND_COLOR]); } ls.LineColor = lineColor; float percentage = 100; String widthStr; css.TryGetValue(CSS.Property.WIDTH, out widthStr); if (widthStr == null) { t.Attributes.TryGetValue(CSS.Property.WIDTH, out widthStr); } if (widthStr != null) { if (widthStr.Contains("%")) { percentage = float.Parse(widthStr.Replace("%", ""), CultureInfo.InvariantCulture); } else { percentage = (CssUtils.GetInstance().ParsePxInCmMmPcToPt(widthStr) / psc.PageSize.Width) * 100; } } ls.Percentage = percentage; String align; t.Attributes.TryGetValue(HTML.Attribute.ALIGN, out align); if (CSS.Value.RIGHT.Equals(align)) { ls.Alignment = Element.ALIGN_RIGHT; } else if (CSS.Value.LEFT.Equals(align)) { ls.Alignment = Element.ALIGN_LEFT; } else if (CSS.Value.CENTER.Equals(align)) { ls.Alignment = Element.ALIGN_CENTER; } return(ls); }
/* (non-Javadoc) * @see com.itextpdf.tool.xml.css.CssApplier#apply(com.itextpdf.text.Element, com.itextpdf.tool.xml.Tag) */ virtual public NoNewLineParagraph Apply(NoNewLineParagraph 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); } } // setDefaultMargin to largestFont if no margin-top 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; } return(p); }
virtual public PdfDiv Apply(PdfDiv div, Tag t, IMarginMemory memory, IPageSizeContainable psc, HtmlPipelineContext context) { if (t.Attributes.ContainsKey("id")) div.Tag = t.Attributes["id"]; 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)) { div.SpacingBefore = div.SpacingBefore + utils.CalculateMarginTop(value, fontSize, memory); marginTop = utils.CalculateMarginTop(value, fontSize, memory); } else if (Util.EqualsIgnoreCase(key, CSS.Property.MARGIN_BOTTOM)) { div.SpacingAfter = div.SpacingAfter + utils.CalculateMarginTop(value, fontSize, memory); 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) || Util.EqualsIgnoreCase(CSS.Property.BORDER_TOP, key)) { if (value.Contains(CSS.Value.DOTTED)) { div.BorderStyleTop = PdfDiv.BorderStyle.DOTTED; } else if (value.Contains(CSS.Value.DASHED)) { div.BorderStyleTop = PdfDiv.BorderStyle.DASHED; } else if (value.Contains(CSS.Value.SOLID)) { div.BorderStyleTop = PdfDiv.BorderStyle.SOLID; } else if (value.Contains(CSS.Value.DOUBLE)) { div.BorderStyleTop = PdfDiv.BorderStyle.DOUBLE; } else if (value.Contains(CSS.Value.GROOVE)) { div.BorderStyleTop = PdfDiv.BorderStyle.GROOVE; } else if (value.Contains(CSS.Value.RIDGE)) { div.BorderStyleTop = PdfDiv.BorderStyle.RIDGE; } else if (value.Contains(CSS.Value.INSET)) { div.BorderStyleTop = PdfDiv.BorderStyle.INSET; } else if (value.Contains(CSS.Value.OUTSET)) { div.BorderStyleTop = PdfDiv.BorderStyle.OUTSET; } } else if (Util.EqualsIgnoreCase(CSS.Property.BORDER_BOTTOM_STYLE, key) || Util.EqualsIgnoreCase(CSS.Property.BORDER_BOTTOM, key)) { if (value.Contains(CSS.Value.DOTTED)) { div.BorderStyleBottom = PdfDiv.BorderStyle.DOTTED; } else if (value.Contains(CSS.Value.DASHED)) { div.BorderStyleBottom = PdfDiv.BorderStyle.DASHED; } else if (value.Contains(CSS.Value.SOLID)) { div.BorderStyleBottom = PdfDiv.BorderStyle.SOLID; } else if (value.Contains(CSS.Value.DOUBLE)) { div.BorderStyleBottom = PdfDiv.BorderStyle.DOUBLE; } else if (value.Contains(CSS.Value.GROOVE)) { div.BorderStyleBottom = PdfDiv.BorderStyle.GROOVE; } else if (value.Contains(CSS.Value.RIDGE)) { div.BorderStyleBottom = PdfDiv.BorderStyle.RIDGE; } else if (value.Contains(CSS.Value.INSET)) { div.BorderStyleBottom = PdfDiv.BorderStyle.INSET; } else if (value.Contains(CSS.Value.OUTSET)) { div.BorderStyleBottom = PdfDiv.BorderStyle.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; }
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); }
/** * Styles a paragraph * * @param p the paragraph * @param t the tag * @param configuration the MarginMemory * @return a styled {@link Paragraph} */ public virtual Paragraph Apply(Paragraph p, Tag t, IMarginMemory configuration) { return((Paragraph)Apply(p, t, configuration, null, null)); }
/* * (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) { 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)) { 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; } } } 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 virtual HtmlCell Apply(HtmlCell cell, Tag t, IMarginMemory memory, IPageSizeContainable psc) { return(Apply(cell, t, memory, psc, null)); }
public override List Apply(List lst, Tag t, IMarginMemory configuration, IPageSizeContainable psc, HtmlPipelineContext context) { float fontSize = FontSizeTranslator.GetInstance().GetFontSize(t); IDictionary <String, String> css = t.CSS; String styleType; css.TryGetValue(CSS.Property.LIST_STYLE_TYPE, out styleType); BaseColor color = HtmlUtilities.DecodeColor(css.ContainsKey(CSS.Property.COLOR) ? css[CSS.Property.COLOR] : null); if (null == color) { color = BaseColor.BLACK; } if (null != styleType) { if (Util.EqualsIgnoreCase(styleType, CSS.Value.NONE)) { lst.Lettered = false; lst.Numbered = false; lst.SetListSymbol(""); } else if (Util.EqualsIgnoreCase(CSS.Value.DECIMAL, styleType)) { lst = new List(List.ORDERED); } else if (Util.EqualsIgnoreCase(CSS.Value.DISC, styleType)) { lst = new ZapfDingbatsList(108); lst.Autoindent = false; lst.SymbolIndent = 7.75f; Chunk symbol = lst.Symbol; symbol.SetTextRise(1.5f); Font font = symbol.Font; font.Size = 4.5f; font.Color = color; } else if (Util.EqualsIgnoreCase(CSS.Value.SQUARE, styleType)) { lst = new ZapfDingbatsList(110); ShrinkSymbol(lst, fontSize, color); } else if (Util.EqualsIgnoreCase(CSS.Value.CIRCLE, styleType)) { lst = new ZapfDingbatsList(109); lst.Autoindent = false; lst.SymbolIndent = 7.75f; Chunk symbol = lst.Symbol; symbol.SetTextRise(1.5f); Font font = symbol.Font; font.Size = 4.5f; font.Color = color; } else if (CSS.Value.LOWER_ROMAN.Equals(styleType)) { lst = new RomanList(true, 0); lst.Autoindent = true; SynchronizeSymbol(fontSize, lst, color); } else if (CSS.Value.UPPER_ROMAN.Equals(styleType)) { lst = new RomanList(false, 0); SynchronizeSymbol(fontSize, lst, color); lst.Autoindent = true; } else if (CSS.Value.LOWER_GREEK.Equals(styleType)) { lst = new GreekList(true, 0); SynchronizeSymbol(fontSize, lst, color); lst.Autoindent = true; } else if (CSS.Value.UPPER_GREEK.Equals(styleType)) { lst = new GreekList(false, 0); SynchronizeSymbol(fontSize, lst, color); lst.Autoindent = true; } else if (CSS.Value.LOWER_ALPHA.Equals(styleType) || CSS.Value.LOWER_LATIN.Equals(styleType)) { lst = new List(List.ORDERED, List.ALPHABETICAL); SynchronizeSymbol(fontSize, lst, color); lst.Lowercase = true; lst.Autoindent = true; } else if (CSS.Value.UPPER_ALPHA.Equals(styleType) || CSS.Value.UPPER_LATIN.Equals(styleType)) { lst = new List(List.ORDERED, List.ALPHABETICAL); SynchronizeSymbol(fontSize, lst, color); lst.Lowercase = false; lst.Autoindent = true; } } else if (Util.EqualsIgnoreCase(t.Name, HTML.Tag.OL)) { lst = new List(List.ORDERED); String type = null; t.Attributes.TryGetValue("type", out type); if (type != null) { if (type.Equals("A")) { lst.Lettered = true; } else if (type.Equals("a")) { lst.Lettered = true; lst.Lowercase = true; } } SynchronizeSymbol(fontSize, lst, color); lst.Autoindent = true; } else if (Util.EqualsIgnoreCase(t.Name, HTML.Tag.UL)) { lst = new List(List.UNORDERED); ShrinkSymbol(lst, fontSize, color); } if (css.ContainsKey(CSS.Property.LIST_STYLE_IMAGE) && !Util.EqualsIgnoreCase(css[CSS.Property.LIST_STYLE_IMAGE], CSS.Value.NONE)) { lst = new List(); String url = utils.ExtractUrl(css[CSS.Property.LIST_STYLE_IMAGE]); try { Image img = new ImageRetrieve(context.ResourcePath, context.GetImageProvider()).RetrieveImage(url); lst.ListSymbol = new Chunk(img, 0, 0, false); lst.SymbolIndent = img.Width; if (LOG.IsLogging(Level.TRACE)) { LOG.Trace(String.Format(LocaleMessages.GetInstance().GetMessage("html.tag.list"), url)); } } catch (NoImageException e) { if (LOG.IsLogging(Level.ERROR)) { LOG.Error(String.Format(LocaleMessages.GetInstance().GetMessage("html.tag.img.failed"), url), e); } lst = new List(List.UNORDERED); } lst.Autoindent = false; } lst.Alignindent = false; float leftIndent = 0; if (css.ContainsKey(CSS.Property.LIST_STYLE_POSITION) && Util.EqualsIgnoreCase(css[CSS.Property.LIST_STYLE_POSITION], CSS.Value.INSIDE)) { leftIndent += 30; } else { leftIndent += 15; } leftIndent += css.ContainsKey(CSS.Property.MARGIN_LEFT)?utils.ParseValueToPt(css[CSS.Property.MARGIN_LEFT], fontSize):0; leftIndent += css.ContainsKey(CSS.Property.PADDING_LEFT)?utils.ParseValueToPt(css[CSS.Property.PADDING_LEFT], fontSize):0; lst.IndentationLeft = leftIndent; String startAtr = null; t.Attributes.TryGetValue(HTML.Attribute.START, out startAtr); if (startAtr != null) { try { int start = int.Parse(startAtr); lst.First = start; } catch (FormatException exc) { } } return(lst); }
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; }
/** * 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 == Font.UNDEFINED) { 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); p.PaddingTop = 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)) { p.Alignment = CSS.GetElementAlignment(value); } 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); } } } if (t.Attributes.ContainsKey(HTML.Attribute.ALIGN)) { String value = t.Attributes[HTML.Attribute.ALIGN]; if (value != null) { p.Alignment = CSS.GetElementAlignment(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; }
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); }
/** * Calculates the margin top or spacingBefore based on the given value and the last margin bottom. * <br /><br /> * In HTML the margin-bottom of a tag overlaps with the margin-top of a following tag. * This method simulates this behavior by subtracting the margin-top value of the given tag from the margin-bottom of the previous tag. The remaining value is returned or if the margin-bottom value is the largest, 0 is returned * @param value the margin-top value of the given tag. * @param largestFont used if a relative value was given to calculate margin. * @param configuration XmlWorkerConfig containing the last margin bottom. * @return an offset */ virtual public float CalculateMarginTop(String value, float largestFont, IMarginMemory configuration) { return CalculateMarginTop(ParseValueToPt(value, largestFont), configuration); }
/** * Calculates the margin top or spacingBefore based on the given value and the last margin bottom. * <br /><br /> * In HTML the margin-bottom of a tag overlaps with the margin-top of a following tag. * This method simulates this behavior by subtracting the margin-top value of the given tag from the margin-bottom of the previous tag. The remaining value is returned or if the margin-bottom value is the largest, 0 is returned * @param value float containing the margin-top value. * @param configuration XmlWorkerConfig containing the last margin bottom. * @return an offset */ virtual public float CalculateMarginTop(float value, IMarginMemory configuration) { float marginTop = value; try { float marginBottom = configuration.LastMarginBottom; marginTop = (marginTop>marginBottom)?marginTop-marginBottom:0; } catch (NoDataException) { } return marginTop; }
/* * (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; }