/** * Sets the top and bottom margin of the given table. * * @param table PdfPTable on which the margins need to be set. * @param t Tag containing the margin styles and font size if needed. * @param values {@link TableStyleValues} containing border widths and border spacing values. * @throws NoCustomContextException */ private void SetVerticalMargin(PdfPTable table, Tag t, TableStyleValues values, IWorkerContext ctx) { float spacingBefore = values.BorderWidthTop; IDictionary <String, Object> memory = GetHtmlPipelineContext(ctx).GetMemory(); Object mb; memory.TryGetValue(HtmlPipelineContext.LAST_MARGIN_BOTTOM, out mb); if (mb != null) { spacingBefore += (float)mb; } float spacingAfter = values.VerBorderSpacing + values.BorderWidthBottom; foreach (KeyValuePair <String, String> css in t.CSS) { String key = css.Key; String value = css.Value; if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_TOP, key)) { spacingBefore += utils.ParseValueToPt(value, fst.GetFontSize(t)); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_BOTTOM, key)) { float marginBottom = utils.ParseValueToPt(value, fst.GetFontSize(t)); spacingAfter += marginBottom; GetHtmlPipelineContext(ctx).GetMemory()[HtmlPipelineContext.LAST_MARGIN_BOTTOM] = marginBottom; } } table.SpacingBefore = spacingBefore; table.SpacingAfter = spacingAfter; }
/** * Sets the top and bottom margin of the given table. * * @param table PdfPTable on which the margins need to be set. * @param t Tag containing the margin styles and font size if needed. * @param values {@link TableStyleValues} containing border widths and border spacing values. * @throws NoCustomContextException */ private void SetVerticalMargin(PdfPTable table, Tag t, TableStyleValues values, IWorkerContext ctx) { float spacingBefore = values.BorderWidthTop; float spacingAfter = values.VerBorderSpacing + values.BorderWidthBottom; foreach (KeyValuePair <String, String> css in t.CSS) { String key = css.Key; String value = css.Value; if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_TOP, key)) { CssUtils utils = CssUtils.GetInstance(); spacingBefore += utils.CalculateMarginTop(value, fst.GetFontSize(t), GetHtmlPipelineContext(ctx)); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_BOTTOM, key)) { float marginBottom = utils.ParseValueToPt(value, fst.GetFontSize(t)); spacingAfter += marginBottom; GetHtmlPipelineContext(ctx).GetMemory()[HtmlPipelineContext.LAST_MARGIN_BOTTOM] = marginBottom; } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_TOP, key)) { table.PaddingTop = utils.ParseValueToPt(value, fst.GetFontSize(t)); } } table.SpacingBefore = spacingBefore; table.SpacingAfter = spacingAfter; }
/** Set the table style values in a {@link TableStyleValues} object based on attributes and css of the given tag. * @param tag containing attributes and css. * @return a {@link TableStyleValues} object containing the table's style values. */ private TableStyleValues SetStyleValues(Tag tag) { TableStyleValues styleValues = new TableStyleValues(); IDictionary <String, String> css = tag.CSS; IDictionary <String, String> attributes = tag.Attributes; string v; if (attributes.ContainsKey(CSS.Property.BORDER)) { styleValues.BorderColor = BaseColor.BLACK; styleValues.BorderWidth = utils.ParsePxInCmMmPcToPt(attributes[CSS.Property.BORDER]); } else { css.TryGetValue(CSS.Property.BORDER_BOTTOM_COLOR, out v); styleValues.BorderColorBottom = HtmlUtilities.DecodeColor(v); css.TryGetValue(CSS.Property.BORDER_TOP_COLOR, out v); styleValues.BorderColorTop = HtmlUtilities.DecodeColor(v); css.TryGetValue(CSS.Property.BORDER_LEFT_COLOR, out v); styleValues.BorderColorLeft = HtmlUtilities.DecodeColor(v); css.TryGetValue(CSS.Property.BORDER_RIGHT_COLOR, out v); styleValues.BorderColorRight = HtmlUtilities.DecodeColor(v); styleValues.BorderWidthBottom = utils.CheckMetricStyle(css, CSS.Property.BORDER_BOTTOM_WIDTH); styleValues.BorderWidthTop = utils.CheckMetricStyle(css, CSS.Property.BORDER_TOP_WIDTH); styleValues.BorderWidthLeft = utils.CheckMetricStyle(css, CSS.Property.BORDER_LEFT_WIDTH); styleValues.BorderWidthRight = utils.CheckMetricStyle(css, CSS.Property.BORDER_RIGHT_WIDTH); } css.TryGetValue(CSS.Property.BACKGROUND_COLOR, out v); styleValues.Background = HtmlUtilities.DecodeColor(v); styleValues.HorBorderSpacing = GetBorderOrCellSpacing(true, css, attributes); styleValues.VerBorderSpacing = GetBorderOrCellSpacing(false, css, attributes); return(styleValues); }
/** * Calculates the start width of a cell. Following values are added up: * <ul> * <li>padding left, this includes left border width and a horizontal border spacing.</li> * <li>padding right, this includes right border width.</li> * <li>the (colspan - 1) * horizontal border spacing.</li> * </ul> * @param cell HtmlCell of which the start width is needed. * @return float containing the start width. */ private float GetCellStartWidth(HtmlCell cell) { TableStyleValues cellStyleValues = cell.CellValues; // colspan - 1, because one horBorderSpacing has been added to paddingLeft for all cells. int spacingMultiplier = cell.Colspan - 1; float spacing = spacingMultiplier * cellStyleValues.HorBorderSpacing; return(spacing + cell.PaddingLeft + cell.PaddingRight + 1); }
public static TableStyleValues setBorderAttributeForCell(Tag tag) { TableStyleValues styleValues = new TableStyleValues(); IDictionary <String, String> attributes = tag.Attributes; IDictionary <String, String> css = tag.CSS; String border = null; if (attributes.TryGetValue(CSS.Property.BORDER, out border) && ("".Equals(border) || utils.ParsePxInCmMmPcToPt(border) > 0)) { styleValues.BorderColor = BaseColor.BLACK; styleValues.BorderWidth = DEFAULT_CELL_BORDER_WIDTH; } styleValues.HorBorderSpacing = GetBorderOrCellSpacing(true, css, attributes); styleValues.VerBorderSpacing = GetBorderOrCellSpacing(false, css, attributes); return(styleValues); }
/** * Sets the default cell width and widest word of a cell. * <ul> * <li>cell width = {@link Table#getCellStartWidth(HtmlCell)} + the width of the widest line of text.</li> * <li>widest word = {@link Table#getCellStartWidth(HtmlCell)} + the widest word of the cell.</li> * </ul> * These 2 widths are used as the starting point when determining the width of the table in * @param cell HtmlCell of which the widths are needed. * @return float array containing the default cell width and the widest word. * <ul> * <li>float[0] = cell width.</li> * <li>float[1] = widest word.</li> * </ul> */ private float[] SetCellWidthAndWidestWord(HtmlCell cell) { IList <float> rulesWidth = new List <float>(); float widestWordOfCell = 0f; float startWidth = GetCellStartWidth(cell); float cellWidth = startWidth; IList <IElement> compositeElements = cell.CompositeElements; if (compositeElements != null) { foreach (IElement baseLevel in compositeElements) { if (baseLevel is Phrase) { for (int i = 0; i < ((Phrase)baseLevel).Count; i++) { IElement inner = ((Phrase)baseLevel)[i]; if (inner is Chunk) { cellWidth += ((Chunk)inner).GetWidthPoint(); float widestWord = startWidth + new ChunkCssApplier().GetWidestWord((Chunk)inner); if (widestWord > widestWordOfCell) { widestWordOfCell = widestWord; } } } rulesWidth.Add(cellWidth); cellWidth = startWidth; } else if (baseLevel is List) { foreach (IElement li in ((List)baseLevel).Items) { rulesWidth.Add(cellWidth); cellWidth = startWidth + ((ListItem)li).IndentationLeft; foreach (Chunk c in ((ListItem)li).Chunks) { cellWidth += c.GetWidthPoint(); float widestWord = new ChunkCssApplier().GetWidestWord(c); if (startWidth + widestWord > widestWordOfCell) { widestWordOfCell = startWidth + widestWord; } } } rulesWidth.Add(cellWidth); cellWidth = startWidth; } else if (baseLevel is PdfPTable) { rulesWidth.Add(cellWidth); cellWidth = startWidth + ((PdfPTable)baseLevel).TotalWidth; foreach (PdfPRow innerRow in ((PdfPTable)baseLevel).Rows) { int size = innerRow.GetCells().Length; TableBorderEvent evente = (TableBorderEvent)((PdfPTable)baseLevel).TableEvent; TableStyleValues values = evente.TableStyleValues; float minRowWidth = values.BorderWidthLeft + (size + 1) * values.HorBorderSpacing + values.BorderWidthRight; int celnr = 0; foreach (PdfPCell innerCell in innerRow.GetCells()) { celnr++; if (innerCell != null) { float innerWidestWordOfCell = SetCellWidthAndWidestWord(new HtmlCell(innerCell, celnr == size))[1]; minRowWidth += innerWidestWordOfCell; } } if (minRowWidth > widestWordOfCell) { widestWordOfCell = minRowWidth; } } rulesWidth.Add(cellWidth); cellWidth = startWidth; } } } foreach (float width in rulesWidth) { if (width > cellWidth) { cellWidth = width; } } return(new float[] { cellWidth, widestWordOfCell }); }
/* * (non-Javadoc) * * @see * com.itextpdf.tool.xml.ITagProcessor#endElement(com.itextpdf.tool.xml.Tag, * java.util.List, com.itextpdf.text.Document) */ public override IList <IElement> End(IWorkerContext ctx, Tag tag, IList <IElement> currentContent) { try { bool percentage = false; String widthValue = null; tag.CSS.TryGetValue(HTML.Attribute.WIDTH, out widthValue); if (!tag.CSS.TryGetValue(HTML.Attribute.WIDTH, out widthValue) && !tag.Attributes.TryGetValue(HTML.Attribute.WIDTH, out widthValue)) { widthValue = null; } if (widthValue != null && widthValue.Trim().EndsWith("%")) { percentage = true; } int numberOfColumns = 0; List <TableRowElement> tableRows = new List <TableRowElement>(currentContent.Count); IList <IElement> invalidRowElements = new List <IElement>(1); String repeatHeader; tag.CSS.TryGetValue(CSS.Property.REPEAT_HEADER, out repeatHeader); String repeatFooter; tag.CSS.TryGetValue(CSS.Property.REPEAT_FOOTER, out repeatFooter); int headerRows = 0; int footerRows = 0; foreach (IElement e in currentContent) { int localNumCols = 0; if (e is TableRowElement) { TableRowElement tableRowElement = (TableRowElement)e; foreach (HtmlCell cell in tableRowElement.Content) { localNumCols += cell.Colspan; } if (localNumCols > numberOfColumns) { numberOfColumns = localNumCols; } tableRows.Add(tableRowElement); if (repeatHeader != null && Util.EqualsIgnoreCase(repeatHeader, "yes") && tableRowElement.RowPlace.Equals(TableRowElement.Place.HEADER)) { headerRows++; } if (repeatFooter != null && Util.EqualsIgnoreCase(repeatFooter, "yes") && tableRowElement.RowPlace.Equals(TableRowElement.Place.FOOTER)) { footerRows++; } } else { invalidRowElements.Add(e); } } if (repeatFooter == null || !Util.EqualsIgnoreCase(repeatFooter, "yes")) { InsertionSort <TableRowElement>(tableRows, delegate(TableRowElement o1, TableRowElement o2) { return(o1.RowPlace.Normal.CompareTo(o2.RowPlace.Normal)); }); } else { InsertionSort <TableRowElement>(tableRows, delegate(TableRowElement o1, TableRowElement o2) { return(o1.RowPlace.Repeated.CompareTo(o2.RowPlace.Repeated)); }); } PdfPTable table = new PdfPTable(numberOfColumns); table.HeaderRows = headerRows + footerRows; table.FooterRows = footerRows; table.HorizontalAlignment = Element.ALIGN_LEFT; TableStyleValues styleValues = SetStyleValues(tag); table.TableEvent = new TableBorderEvent(styleValues); SetVerticalMargin(table, tag, styleValues, ctx); WidenLastCell(tableRows, styleValues.HorBorderSpacing); float[] columnWidths = new float[numberOfColumns]; float[] widestWords = new float[numberOfColumns]; float[] fixedWidths = new float[numberOfColumns]; float[] colspanWidestWords = new float[numberOfColumns]; int[] rowspanValue = new int[numberOfColumns]; float largestColumn = 0; float largestColspanColumn = 0; int indexOfLargestColumn = -1; int indexOfLargestColspanColumn = -1; // Initial fill of the widths arrays foreach (TableRowElement row in tableRows) { int column = 0; foreach (HtmlCell cell in row.Content) { // check whether the current column should be skipped due to a // rowspan value of higher cell in this column. while (rowspanValue[column] > 1) { rowspanValue[column] = rowspanValue[column] - 1; ++column; } // sets a rowspan counter for current column (counter not // needed for last column). if (cell.Rowspan > 1 && column != numberOfColumns - 1) { rowspanValue[column] = cell.Rowspan - 1; } int colspan = cell.Colspan; if (cell.FixedWidth != 0) { float fixedWidth = cell.FixedWidth + GetCellStartWidth(cell); fixedWidth /= colspan; for (int i = 0; i < colspan; i++) { int c = column + i; if (fixedWidth > fixedWidths[c]) { fixedWidths[c] = fixedWidth; columnWidths[c] = fixedWidth; } } } if (cell.CompositeElements != null) { float[] widthValues = SetCellWidthAndWidestWord(cell); float cellWidth = widthValues[0] / colspan; float widestWordOfCell = widthValues[1] / colspan; for (int i = 0; i < colspan; i++) { int c = column + i; if (fixedWidths[c] == 0 && cellWidth > columnWidths[c]) { columnWidths[c] = cellWidth; if (colspan == 1) { if (cellWidth > largestColumn) { largestColumn = cellWidth; indexOfLargestColumn = c; } } else { if (cellWidth > largestColspanColumn) { largestColspanColumn = cellWidth; indexOfLargestColspanColumn = c; } } } if (colspan == 1) { if (widestWordOfCell > widestWords[c]) { widestWords[c] = widestWordOfCell; } } else { if (widestWordOfCell > colspanWidestWords[c]) { colspanWidestWords[c] = widestWordOfCell; } } } } if (colspan > 1) { if (LOG.IsLogging(Level.TRACE)) { LOG.Trace(String.Format(LocaleMessages.GetInstance().GetMessage(LocaleMessages.COLSPAN), colspan)); } column += colspan - 1; } column++; } } if (indexOfLargestColumn == -1) { indexOfLargestColumn = indexOfLargestColspanColumn; if (indexOfLargestColumn == -1) { indexOfLargestColumn = 0; } for (int column = 0; column < numberOfColumns; column++) { widestWords[column] = colspanWidestWords[column]; } } float outerWidth = GetTableOuterWidth(tag, styleValues.HorBorderSpacing, ctx); float initialTotalWidth = GetTableWidth(columnWidths, 0); // float targetWidth = calculateTargetWidth(tag, columnWidths, outerWidth, ctx); float targetWidth = 0; HtmlPipelineContext htmlPipelineContext = GetHtmlPipelineContext(ctx); float max = htmlPipelineContext.PageSize.Width - outerWidth; bool tableWidthFixed = false; if (tag.Attributes.ContainsKey(CSS.Property.WIDTH) || tag.CSS.ContainsKey(CSS.Property.WIDTH)) { targetWidth = new WidthCalculator().GetWidth(tag, htmlPipelineContext.GetRootTags(), htmlPipelineContext.PageSize.Width); if (targetWidth > max) { targetWidth = max; } tableWidthFixed = true; } else if (initialTotalWidth <= max) { targetWidth = initialTotalWidth; } else if (null == tag.Parent || (null != tag.Parent && htmlPipelineContext.GetRootTags().Contains(tag.Parent.Name))) { targetWidth = max; } else /* this table is an inner table and width adjustment is done in outer table */ { targetWidth = GetTableWidth(columnWidths, outerWidth); } float totalFixedColumnWidth = GetTableWidth(fixedWidths, 0); float targetPercentage = 0; if (totalFixedColumnWidth == initialTotalWidth) // all column widths are fixed { targetPercentage = targetWidth / initialTotalWidth; if (initialTotalWidth > targetWidth) { for (int column = 0; column < columnWidths.Length; column++) { columnWidths[column] *= targetPercentage; } } else if (tableWidthFixed && targetPercentage != 1) { for (int column = 0; column < columnWidths.Length; column++) { columnWidths[column] *= targetPercentage; } } } else { targetPercentage = (targetWidth - totalFixedColumnWidth) / (initialTotalWidth - totalFixedColumnWidth); // Reduce width of columns if the columnWidth array + borders + // paddings // is too large for the given targetWidth. if (initialTotalWidth > targetWidth) { float leftToReduce = 0; for (int column = 0; column < columnWidths.Length; column++) { if (fixedWidths[column] == 0) { // Reduce width of the column to its targetWidth, if // widestWord of column still fits in the targetWidth of // the // column. if (widestWords[column] <= columnWidths[column] * targetPercentage) { columnWidths[column] *= targetPercentage; // else take the widest word and calculate space // left to // reduce. } else { columnWidths[column] = widestWords[column]; leftToReduce += widestWords[column] - columnWidths[column] * targetPercentage; } // if widestWord of a column does not fit in the // fixedWidth, // set the column width to the widestWord. } else if (fixedWidths[column] < widestWords[column]) { columnWidths[column] = widestWords[column]; leftToReduce += widestWords[column] - fixedWidths[column]; } } if (leftToReduce != 0) { // Reduce width of the column with the most text, if its // widestWord still fits in the reduced column. if (widestWords[indexOfLargestColumn] <= columnWidths[indexOfLargestColumn] - leftToReduce) { columnWidths[indexOfLargestColumn] -= leftToReduce; } else // set all columns to their minimum, with the // widestWord array. { for (int column = 0; leftToReduce != 0 && column < columnWidths.Length; column++) { if (fixedWidths[column] == 0 && columnWidths[column] > widestWords[column]) { float difference = columnWidths[column] - widestWords[column]; if (difference <= leftToReduce) { leftToReduce -= difference; columnWidths[column] = widestWords[column]; } else { columnWidths[column] -= leftToReduce; leftToReduce = 0; } } } if (leftToReduce != 0) { // If the table has an insufficient fixed width // by // an // attribute or style, try to enlarge the table // to // its // minimum width (= widestWords array). float pageWidth = GetHtmlPipelineContext(ctx).PageSize.Width; if (GetTableWidth(widestWords, outerWidth) < pageWidth) { targetWidth = GetTableWidth(widestWords, outerWidth); leftToReduce = 0; } else { // If all columnWidths are set to the // widestWordWidths and the table is still // to // wide // content will fall off the edge of a page, // which // is similar to HTML. targetWidth = pageWidth - outerWidth; leftToReduce = 0; } } } } // Enlarge width of columns to fit the targetWidth. } else if (initialTotalWidth < targetWidth) { for (int column = 0; column < columnWidths.Length; column++) { if (fixedWidths[column] == 0) { columnWidths[column] *= targetPercentage; } } } } try { table.SetTotalWidth(columnWidths); table.LockedWidth = true; table.DefaultCell.Border = Rectangle.NO_BORDER; } catch (DocumentException e) { throw new RuntimeWorkerException(LocaleMessages.GetInstance().GetMessage(LocaleMessages.NO_CUSTOM_CONTEXT), e); } float?tableHeight = new HeightCalculator().GetHeight(tag, GetHtmlPipelineContext(ctx).PageSize.Height); float?tableRowHeight = null; if (tableHeight != null && tableHeight > 0) { tableRowHeight = tableHeight / tableRows.Count; } int rowNumber = 0; foreach (TableRowElement row in tableRows) { int columnNumber = -1; float?computedRowHeight = null; /*if ( tableHeight != null && tableRows.IndexOf(row) == tableRows.Count - 1) { * float computedTableHeigt = table.CalculateHeights(); * computedRowHeight = tableHeight - computedTableHeigt; * }*/ foreach (HtmlCell cell in row.Content) { IList <IElement> compositeElements = cell.CompositeElements; if (compositeElements != null) { foreach (IElement baseLevel in compositeElements) { if (baseLevel is PdfPTable) { TableStyleValues cellValues = cell.CellValues; float totalBordersWidth = cellValues.IsLastInRow ? styleValues.HorBorderSpacing * 2 : styleValues.HorBorderSpacing; totalBordersWidth += cellValues.BorderWidthLeft + cellValues.BorderWidthRight; float columnWidth = 0; for (int currentColumnNumber = columnNumber + 1; currentColumnNumber <= columnNumber + cell.Colspan; currentColumnNumber++) { columnWidth += columnWidths[currentColumnNumber]; } IPdfPTableEvent tableEvent = ((PdfPTable)baseLevel).TableEvent; TableStyleValues innerStyleValues = ((TableBorderEvent)tableEvent).TableStyleValues; totalBordersWidth += innerStyleValues.BorderWidthLeft; totalBordersWidth += innerStyleValues.BorderWidthRight; ((PdfPTable)baseLevel).TotalWidth = columnWidth - totalBordersWidth; } } } columnNumber += cell.Colspan; table.AddCell(cell); } table.CompleteRow(); if ((computedRowHeight == null || computedRowHeight <= 0) && tableRowHeight != null) { computedRowHeight = tableRowHeight; } if (computedRowHeight != null && computedRowHeight > 0) { float rowHeight = table.GetRow(rowNumber).MaxHeights; if (rowHeight < computedRowHeight) { table.GetRow(rowNumber).MaxHeights = computedRowHeight.Value; } else if (tableRowHeight != null && tableRowHeight < rowHeight) { tableRowHeight = (tableHeight - rowHeight - rowNumber * tableRowHeight) / (tableRows.Count - rowNumber - 1); } } rowNumber++; } if (percentage) { table.WidthPercentage = utils.ParsePxInCmMmPcToPt(widthValue); table.LockedWidth = false; } List <IElement> elems = new List <IElement>(); if (invalidRowElements.Count > 0) { // all invalid row elements taken as caption int i = 0; Tag captionTag = tag.Children[i++]; while (!Util.EqualsIgnoreCase(captionTag.Name, HTML.Tag.CAPTION) && i < tag.Children.Count) { captionTag = tag.Children[i]; i++; } String captionSideValue; captionTag.CSS.TryGetValue(CSS.Property.CAPTION_SIDE, out captionSideValue); if (captionSideValue != null && Util.EqualsIgnoreCase(captionSideValue, CSS.Value.BOTTOM)) { elems.Add(table); elems.AddRange(invalidRowElements); } else { elems.AddRange(invalidRowElements); elems.Add(table); } } else { elems.Add(table); } return(elems); } catch (NoCustomContextException e) { throw new RuntimeWorkerException(LocaleMessages.GetInstance().GetMessage(LocaleMessages.NO_CUSTOM_CONTEXT), e); } }
/** Set the table style values in a {@link TableStyleValues} object based on attributes and css of the given tag. * @param tag containing attributes and css. * @return a {@link TableStyleValues} object containing the table's style values. */ public static TableStyleValues SetStyleValues(Tag tag) { TableStyleValues styleValues = new TableStyleValues(); IDictionary <String, String> css = tag.CSS; IDictionary <String, String> attributes = tag.Attributes; string v; if (attributes.ContainsKey(CSS.Property.BORDER)) { styleValues.BorderColor = BaseColor.BLACK; String borderValue = null; if (attributes.TryGetValue(CSS.Property.BORDER, out borderValue) && "".Equals(borderValue)) { styleValues.BorderWidth = DEFAULT_CELL_BORDER_WIDTH; } else { styleValues.BorderWidth = utils.ParsePxInCmMmPcToPt(borderValue); } } else { foreach (KeyValuePair <String, String> entry in css) { String key = entry.Key; String value = entry.Value; if (Util.EqualsIgnoreCase(key, CSS.Property.BORDER_LEFT_STYLE) && Util.EqualsIgnoreCase(CSS.Value.SOLID, value)) { styleValues.BorderColorLeft = BaseColor.BLACK; styleValues.BorderWidthLeft = DEFAULT_CELL_BORDER_WIDTH; } else if (Util.EqualsIgnoreCase(key, CSS.Property.BORDER_RIGHT_STYLE) && Util.EqualsIgnoreCase(CSS.Value.SOLID, value)) { styleValues.BorderColorRight = BaseColor.BLACK; styleValues.BorderWidthRight = DEFAULT_CELL_BORDER_WIDTH; } else if (Util.EqualsIgnoreCase(key, CSS.Property.BORDER_TOP_STYLE) && Util.EqualsIgnoreCase(CSS.Value.SOLID, value)) { styleValues.BorderColorTop = BaseColor.BLACK; styleValues.BorderWidthTop = DEFAULT_CELL_BORDER_WIDTH; } else if (Util.EqualsIgnoreCase(key, CSS.Property.BORDER_BOTTOM_STYLE) && Util.EqualsIgnoreCase(CSS.Value.SOLID, value)) { styleValues.BorderColorBottom = BaseColor.BLACK; styleValues.BorderWidthBottom = DEFAULT_CELL_BORDER_WIDTH; } } String color = null; if (css.TryGetValue(CSS.Property.BORDER_BOTTOM_COLOR, out color)) { styleValues.BorderColorBottom = HtmlUtilities.DecodeColor(color); } if (css.TryGetValue(CSS.Property.BORDER_TOP_COLOR, out color)) { styleValues.BorderColorTop = HtmlUtilities.DecodeColor(color); } if (css.TryGetValue(CSS.Property.BORDER_LEFT_COLOR, out color)) { styleValues.BorderColorLeft = HtmlUtilities.DecodeColor(color); } if (css.TryGetValue(CSS.Property.BORDER_RIGHT_COLOR, out color)) { styleValues.BorderColorRight = HtmlUtilities.DecodeColor(color); } float?width = utils.CheckMetricStyle(css, CSS.Property.BORDER_BOTTOM_WIDTH); if (width != null) { styleValues.BorderWidthBottom = (float)width; } width = utils.CheckMetricStyle(css, CSS.Property.BORDER_TOP_WIDTH); if (width != null) { styleValues.BorderWidthTop = (float)width; } width = utils.CheckMetricStyle(css, CSS.Property.BORDER_RIGHT_WIDTH); if (width != null) { styleValues.BorderWidthRight = (float)width; } width = utils.CheckMetricStyle(css, CSS.Property.BORDER_LEFT_WIDTH); if (width != null) { styleValues.BorderWidthLeft = (float)width; } } css.TryGetValue(CSS.Property.BACKGROUND_COLOR, out v); styleValues.Background = HtmlUtilities.DecodeColor(v); styleValues.HorBorderSpacing = GetBorderOrCellSpacing(true, css, attributes); styleValues.VerBorderSpacing = GetBorderOrCellSpacing(false, css, attributes); return(styleValues); }
private void SetRightOfBorder(HtmlCell cell, String key, String value, TableStyleValues values) { if (key.Contains(CSS.Property.WIDTH)) { values.BorderWidthRight = utils.ParsePxInCmMmPcToPt(value); } if (key.Contains(CSS.Property.COLOR)) { values.BorderColorRight = HtmlUtilities.DecodeColor(value); } else if (values.BorderColorRight == null){ values.BorderColorRight = BaseColor.BLACK; } if (key.Contains("style")) { // If any, which are the border styles in iText? simulate in the borderevent? if (values.BorderWidthRight == 0){ values.BorderWidthRight = 2.25f; } } }
/** * Construct a new TableBorderEvent with the given TableStyleValues. * * @param styleValues the TableStyleValues * @see com.itextpdf.text.pdf.PdfPTableEvent#tableLayout(com.itextpdf.text.pdf.PdfPTable, * float[][], float[], int, int, * com.itextpdf.text.pdf.PdfContentByte[]) */ public TableBorderEvent(TableStyleValues styleValues) { this.styleValues = styleValues; }
/** * Sets the top and bottom margin of the given table. * * @param table PdfPTable on which the margins need to be set. * @param t Tag containing the margin styles and font size if needed. * @param values {@link TableStyleValues} containing border widths and border spacing values. * @throws NoCustomContextException */ private void SetVerticalMargin(PdfPTable table, Tag t, TableStyleValues values, IWorkerContext ctx) { float spacingBefore = values.BorderWidthTop; float spacingAfter = values.VerBorderSpacing + values.BorderWidthBottom; foreach (KeyValuePair<String, String> css in t.CSS) { String key = css.Key; String value = css.Value; if(Util.EqualsIgnoreCase(CSS.Property.MARGIN_TOP, key)) { CssUtils utils = CssUtils.GetInstance(); spacingBefore += utils.CalculateMarginTop(value, fst.GetFontSize(t), GetHtmlPipelineContext(ctx)); } else if (Util.EqualsIgnoreCase(CSS.Property.MARGIN_BOTTOM, key)) { float marginBottom = utils.ParseValueToPt(value, fst.GetFontSize(t)); spacingAfter += marginBottom; GetHtmlPipelineContext(ctx).GetMemory()[HtmlPipelineContext.LAST_MARGIN_BOTTOM] = marginBottom; } else if (Util.EqualsIgnoreCase(CSS.Property.PADDING_TOP, key)) { table.PaddingTop = utils.ParseValueToPt(value, fst.GetFontSize(t)); } } table.SpacingBefore = spacingBefore; table.SpacingAfter = spacingAfter; }
public static TableStyleValues SetBorderAttributeForCell(Tag tag) { TableStyleValues styleValues = new TableStyleValues(); IDictionary<String, String> attributes = tag.Attributes; IDictionary<String, String> css = tag.CSS; String border = null; if (attributes.TryGetValue(CSS.Property.BORDER, out border) && ("".Equals(border) || utils.ParsePxInCmMmPcToPt(border) > 0)) { styleValues.BorderColor = BaseColor.BLACK; styleValues.BorderWidth = DEFAULT_CELL_BORDER_WIDTH; } styleValues.HorBorderSpacing = GetBorderOrCellSpacing(true, css, attributes); styleValues.VerBorderSpacing = GetBorderOrCellSpacing(false, css, attributes); return styleValues; }
/** Set the table style values in a {@link TableStyleValues} object based on attributes and css of the given tag. * @param tag containing attributes and css. * @return a {@link TableStyleValues} object containing the table's style values. */ public static TableStyleValues SetStyleValues(Tag tag) { TableStyleValues styleValues = new TableStyleValues(); IDictionary<String, String> css = tag.CSS; IDictionary<String, String> attributes = tag.Attributes; string v; if (attributes.ContainsKey(CSS.Property.BORDER)) { styleValues.BorderColor = BaseColor.BLACK; String borderValue = null; if (attributes.TryGetValue(CSS.Property.BORDER, out borderValue) && "".Equals(borderValue)) styleValues.BorderWidth = DEFAULT_CELL_BORDER_WIDTH; else styleValues.BorderWidth = utils.ParsePxInCmMmPcToPt(borderValue); } else { foreach (KeyValuePair<String, String> entry in css) { String key = entry.Key; String value = entry.Value; if (Util.EqualsIgnoreCase(key, CSS.Property.BORDER_LEFT_STYLE) && Util.EqualsIgnoreCase(CSS.Value.SOLID, value)) { styleValues.BorderColorLeft = BaseColor.BLACK; styleValues.BorderWidthLeft = DEFAULT_CELL_BORDER_WIDTH; } else if (Util.EqualsIgnoreCase(key, CSS.Property.BORDER_RIGHT_STYLE) && Util.EqualsIgnoreCase(CSS.Value.SOLID, value)) { styleValues.BorderColorRight = BaseColor.BLACK; styleValues.BorderWidthRight = DEFAULT_CELL_BORDER_WIDTH; } else if (Util.EqualsIgnoreCase(key,CSS.Property.BORDER_TOP_STYLE) && Util.EqualsIgnoreCase(CSS.Value.SOLID, value)) { styleValues.BorderColorTop = BaseColor.BLACK; styleValues.BorderWidthTop = DEFAULT_CELL_BORDER_WIDTH; } else if (Util.EqualsIgnoreCase(key, CSS.Property.BORDER_BOTTOM_STYLE) && Util.EqualsIgnoreCase(CSS.Value.SOLID, value)) { styleValues.BorderColorBottom = BaseColor.BLACK; styleValues.BorderWidthBottom = DEFAULT_CELL_BORDER_WIDTH; } } String color = null; if (css.TryGetValue(CSS.Property.BORDER_BOTTOM_COLOR, out color)) { styleValues.BorderColorBottom = HtmlUtilities.DecodeColor(color); } if (css.TryGetValue(CSS.Property.BORDER_TOP_COLOR, out color)) { styleValues.BorderColorTop = HtmlUtilities.DecodeColor(color); } if (css.TryGetValue(CSS.Property.BORDER_LEFT_COLOR, out color)) { styleValues.BorderColorLeft = HtmlUtilities.DecodeColor(color); } if (css.TryGetValue(CSS.Property.BORDER_RIGHT_COLOR, out color)) { styleValues.BorderColorRight = HtmlUtilities.DecodeColor(color); } float? width = utils.CheckMetricStyle(css, CSS.Property.BORDER_BOTTOM_WIDTH); if (width != null) { styleValues.BorderWidthBottom = (float)width; } width = utils.CheckMetricStyle(css, CSS.Property.BORDER_TOP_WIDTH); if (width != null) { styleValues.BorderWidthTop = (float)width; } width = utils.CheckMetricStyle(css, CSS.Property.BORDER_RIGHT_WIDTH); if (width != null) { styleValues.BorderWidthRight = (float) width; } width = utils.CheckMetricStyle(css, CSS.Property.BORDER_LEFT_WIDTH); if (width != null) { styleValues.BorderWidthLeft = (float)width; } } css.TryGetValue(CSS.Property.BACKGROUND_COLOR, out v); styleValues.Background = HtmlUtilities.DecodeColor(v); styleValues.HorBorderSpacing = GetBorderOrCellSpacing(true, css, attributes); styleValues.VerBorderSpacing = GetBorderOrCellSpacing(false, css, attributes); return styleValues; }
/** * Construct a new CellSpacingEvent with the given TableStyleValues * @param styleValues the TableStyleValues */ public CellSpacingEvent(TableStyleValues styleValues) { this.styleValues = styleValues; }
/* * (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; Tag row = t.Parent; while(row != null && !row.Name.Equals(HTML.Tag.TR)){ row = row.Parent; } while(table!= null && !table.Name.Equals(HTML.Tag.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. 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; } } } float horSpacing = Table.GetBorderOrCellSpacing(true, table.CSS, table.Attributes); float verSpacing = 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; }