Inheritance: iTextSharp.text.pdf.PdfPCell
Beispiel #1
0
	    /*
	     * (non-Javadoc)
	     * 
	     * @see
	     * com.itextpdf.tool.xml.TagProcessor#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) {
		    HtmlCell cell = new HtmlCell();
            try {
                HtmlPipelineContext htmlPipelineContext = GetHtmlPipelineContext(ctx);
                cell = new HtmlCellCssApplier().Apply(cell, tag, htmlPipelineContext, htmlPipelineContext);
            } catch (NoCustomContextException e1) {
                throw new RuntimeWorkerException(LocaleMessages.GetInstance().GetMessage(LocaleMessages.NO_CUSTOM_CONTEXT), e1);
            }
		    IList<IElement> l = new List<IElement>(1);
            IList<IElement> chunks = new List<IElement>();
		    foreach (IElement e in currentContent) {
                if (e is Chunk || e is NoNewLineParagraph || e is LineSeparator) {
                    if (e == Chunk.NEWLINE) {
                        int index = currentContent.IndexOf(e);
                        if (index == currentContent.Count - 1) {
                            continue;
                        } else {
                            IElement nextElement = currentContent[index + 1];
                            if (nextElement is Paragraph) {
                                continue;
                            }
                            if (chunks.Count == 0) {
                                continue;
                            }

                        }
                    } else if (e is LineSeparator) {
                        chunks.Add(Chunk.NEWLINE);
                    }
                    chunks.Add(e);
                    continue;
                } else if (chunks.Count > 0) {
                    Paragraph p = new Paragraph();
                    p.MultipliedLeading = 1.2f;
                    p.AddAll(chunks);
                    p.Alignment = cell.HorizontalAlignment;
                    cell.AddElement(p);
                    chunks.Clear();
                }

                if (e is Paragraph) {
                    ((Paragraph)e).Alignment = cell.HorizontalAlignment;
                }

			    cell.AddElement(e);
		    }
            if ( chunks.Count > 0 ) {
                Paragraph p = new Paragraph();
                p.MultipliedLeading = 1.2f;
                p.AddAll(chunks);
                p.Alignment = cell.HorizontalAlignment;
                cell.AddElement(p);
            }
    	    l.Add(cell);
		    return l;
	    }
        virtual public void SetUp() {
            cells = new List<Element>();
            tag = new Tag("td", new Dictionary<String, String>());
            basicPara = new NoNewLineParagraph();
            basic = new Chunk("content");

            cell = new HtmlCell();
            applier = new HtmlCellCssApplier();


            LoggerFactory.GetInstance().SetLogger(new SysoLogger(3));
            Tag parent = new Tag("tr");
            parent.Parent = new Tag("table");
            tag.Parent = parent;
            basicPara.Add(basic);
            cell.AddElement(basicPara);
            cells.Add(cell);
            config = new HtmlPipelineContext(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;
    }
     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;
             }
         }
     }
Beispiel #5
0
	    /*
	     * (non-Javadoc)
	     * 
	     * @see
	     * com.itextpdf.tool.xml.TagProcessor#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) {
		    HtmlCell cell = new HtmlCell();
            int direction = GetRunDirection(tag);

            if (direction != PdfWriter.RUN_DIRECTION_DEFAULT) {
                cell.RunDirection = direction;
            }

            if (HTML.Tag.TH.Equals(tag.Name, StringComparison.OrdinalIgnoreCase)) {
                cell.Role = PdfName.TH;
            }
            try {
                HtmlPipelineContext htmlPipelineContext = GetHtmlPipelineContext(ctx);
                cell = (HtmlCell) GetCssAppliers().Apply(cell, tag, htmlPipelineContext);
            } catch (NoCustomContextException e1) {
                throw new RuntimeWorkerException(LocaleMessages.GetInstance().GetMessage(LocaleMessages.NO_CUSTOM_CONTEXT), e1);
            }
		    IList<IElement> l = new List<IElement>(1);
            IList<IElement> chunks = new List<IElement>();
            IList<ListItem> listItems = new List<ListItem>();
	        int index = -1;
		    foreach (IElement e in currentContent) {
		        index++;
                if (e is Chunk || e is NoNewLineParagraph || e is LineSeparator) {
                    if (listItems.Count > 0) {
                        ProcessListItems(ctx, tag, listItems, cell);
                    }
                    if (e is Chunk && Chunk.NEWLINE.Content.Equals(((Chunk)e).Content)) {
                        if (index == currentContent.Count - 1) {
                            continue;
                        } else {
                            IElement nextElement = currentContent[index + 1];
                            if (chunks.Count > 0 && !(nextElement is Chunk) && !(nextElement is NoNewLineParagraph)) {
                                continue;
                            }
                        }
                    } else if (e is LineSeparator) {
                        try {
                            HtmlPipelineContext htmlPipelineContext = GetHtmlPipelineContext(ctx);
                            Chunk newLine = (Chunk)GetCssAppliers().Apply(new Chunk(Chunk.NEWLINE), tag, htmlPipelineContext);
                            chunks.Add(newLine);
                        } catch (NoCustomContextException exc) {
                            throw new RuntimeWorkerException(LocaleMessages.GetInstance().GetMessage(LocaleMessages.NO_CUSTOM_CONTEXT), exc);
                        }
                    }
                    chunks.Add(e);
                    continue;
                } else if (e is ListItem) {
                    if (chunks.Count >0 ) {
                        ProcessChunkItems(chunks, cell);
                    }
                    listItems.Add((ListItem)e);
                    continue;
                } else {
		            if (chunks.Count > 0) {
                       ProcessChunkItems(chunks, cell); 
                    }
                    if (listItems.Count > 0) {
                        ProcessListItems(ctx, tag, listItems, cell);
                    }
                }

                if (e is Paragraph) {
                    if (((Paragraph) e).Alignment == Element.ALIGN_UNDEFINED) {
                        ((Paragraph) e).Alignment = cell.HorizontalAlignment;
                    }
                }

			    cell.AddElement(e);
		    }
            if ( chunks.Count > 0 ) {
                ProcessChunkItems(chunks, cell);
            }
    	    l.Add(cell);
		    return l;
	    }
Beispiel #6
0
        protected void ProcessListItems(IWorkerContext ctx, Tag tag, IList<ListItem> listItems, HtmlCell cell) {
            try {
                List list = new List();
                list.Autoindent = false;
                list = (List) GetCssAppliers().Apply(list, tag, GetHtmlPipelineContext(ctx));
                list.IndentationLeft = 0;
                foreach (ListItem li in listItems) {
                    ListItem listItem = (ListItem) GetCssAppliers().Apply(li, tag, GetHtmlPipelineContext(ctx));
                    listItem.SpacingAfter = 0;
                    listItem.SpacingBefore = 0;

                    listItem.MultipliedLeading = 1.2f;
                    list.Add(listItem);
                }
                cell.AddElement(list);
                listItems.Clear();
            } catch (NoCustomContextException e) {
                throw new RuntimeWorkerException(LocaleMessages.GetInstance().GetMessage(LocaleMessages.NO_CUSTOM_CONTEXT), e);
            }
        }
Beispiel #7
0
 protected void ProcessChunkItems(IList<IElement> chunks, HtmlCell cell) {
     Paragraph p = new Paragraph();
     p.MultipliedLeading = 1.2f;
     p.AddAll(chunks);
     p.Alignment = cell.HorizontalAlignment;
     if (p.Trim()) {
         cell.AddElement(p);
     }
     chunks.Clear();    
 }
Beispiel #8
0
 /*
  * (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)
 {
     HtmlCell cell = new HtmlCell();
     IList<IElement> l = new List<IElement>(1);
     foreach (IElement e in currentContent) {
         cell.AddElement(e);
     }
     try {
         l.Add(new HtmlCellCssApplier(GetHtmlPipelineContext(ctx)).Apply(cell, tag));
     } catch (NoCustomContextException e1) {
         throw new RuntimeWorkerException(LocaleMessages.GetInstance().GetMessage(LocaleMessages.NO_CUSTOM_CONTEXT), e1);
     }
     return l;
 }
 virtual public void ResolveFixedWidth() {
     HtmlCell fixedWidthCell = new HtmlCell();
     tag.Attributes["width"] = "90pt";
     fixedWidthCell = applier.Apply(fixedWidthCell, tag, config, config);
     Assert.AreEqual(90, (fixedWidthCell).FixedWidth, 0);
 }
Beispiel #10
0
	    /*
	     * (non-Javadoc)
	     * 
	     * @see
	     * com.itextpdf.tool.xml.TagProcessor#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) {
		    HtmlCell cell = new HtmlCell();
            try {
                HtmlPipelineContext htmlPipelineContext = GetHtmlPipelineContext(ctx);
                cell = (HtmlCell) GetCssAppliers().Apply(cell, tag, htmlPipelineContext);
            } catch (NoCustomContextException e1) {
                throw new RuntimeWorkerException(LocaleMessages.GetInstance().GetMessage(LocaleMessages.NO_CUSTOM_CONTEXT), e1);
            }
		    IList<IElement> l = new List<IElement>(1);
            IList<IElement> chunks = new List<IElement>();
            IList<ListItem> listItems = new List<ListItem>();
	        int index = -1;
		    foreach (IElement e in currentContent) {
		        index++;
                if (e is Chunk || e is NoNewLineParagraph || e is LineSeparator) {
                    if (listItems.Count > 0) {
                        ProcessListItems(ctx, tag, listItems, cell);
                    }
                    if (e is Chunk && Chunk.NEWLINE.Content.Equals(((Chunk)e).Content)) {
                        if (index == currentContent.Count - 1) {
                            continue;
                        } else {
                            IElement nextElement = currentContent[index + 1];
                            if (chunks.Count > 0 && !(nextElement is Chunk) && !(nextElement is NoNewLineParagraph)) {
                                continue;
                            }
                        }
                    } else if (e is LineSeparator) {
                        try {
                            HtmlPipelineContext htmlPipelineContext = GetHtmlPipelineContext(ctx);
                            Chunk newLine = (Chunk)GetCssAppliers().Apply(new Chunk(Chunk.NEWLINE), tag, htmlPipelineContext);
                            chunks.Add(newLine);
                        } catch (NoCustomContextException exc) {
                            throw new RuntimeWorkerException(LocaleMessages.GetInstance().GetMessage(LocaleMessages.NO_CUSTOM_CONTEXT), exc);
                        }
                    }
                    chunks.Add(e);
                    continue;
                } else if (e is ListItem) {
                    if (chunks.Count >0 ) {
                        ProcessChunkItems(chunks, cell);
                    }
                    listItems.Add((ListItem)e);
                    continue;
                } else {
		            if (chunks.Count > 0) {
                       ProcessChunkItems(chunks, cell); 
                    }
                    if (listItems.Count > 0) {
                        ProcessListItems(ctx, tag, listItems, cell);
                    }
                }

                if (e is Paragraph) {
                    ((Paragraph)e).Alignment = cell.HorizontalAlignment;
                }

			    cell.AddElement(e);
		    }
            if ( chunks.Count > 0 ) {
                ProcessChunkItems(chunks, cell);
            }
    	    l.Add(cell);
		    return l;
	    }
Beispiel #11
0
 /**
  * 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;
 }
Beispiel #12
0
        /**
         * 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;
            float widthDeviation = 0.001f;
            IList<IElement> compositeElements = cell.CompositeElements;
            if (compositeElements != null) {
                foreach (IElement baseLevel in compositeElements) {
                    cellWidth = float.NaN;
                    if (baseLevel is Phrase) {
                        for(int i = 0; i < ((Phrase)baseLevel).Count; i++) {
                            IElement inner = ((Phrase)baseLevel)[i];
                            if (inner is Chunk) {
                                if(float.IsNaN(cellWidth))
                                    cellWidth = startWidth + widthDeviation;
                                cellWidth += ((Chunk)inner).GetWidthPoint();
                                float widestWord = startWidth + widthDeviation + GetCssAppliers().ChunkCssAplier.GetWidestWord((Chunk)inner);
                                if(widestWord > widestWordOfCell) {
                                    widestWordOfCell = widestWord;
                                }
                            }
                        }
                        if(!float.IsNaN(cellWidth))
                            rulesWidth.Add(cellWidth);
                    } else if (baseLevel is List) {
                        foreach (IElement li in ((List)baseLevel).Items) {
                            cellWidth = startWidth + widthDeviation + ((ListItem)li).IndentationLeft;
                            foreach (Chunk c in li.Chunks) {
                                cellWidth += c.GetWidthPoint();
                                float widestWord = new ChunkCssApplier().GetWidestWord(c);
                                if(startWidth + widthDeviation + widestWord > widestWordOfCell) {
                                    widestWordOfCell = startWidth + widthDeviation + widestWord;
                                }
                            }
                            rulesWidth.Add(cellWidth);
                        }
                    } else if (baseLevel is PdfPTable) {
                        cellWidth = startWidth + widthDeviation + ((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);
				    }
                    else if (baseLevel is PdfDiv) {
                        PdfDiv div = (PdfDiv) baseLevel;

                        float? divActualWidth = div.Width;

                        if (divActualWidth == null) {
                            divActualWidth = CalculateDivWidestElementWidth(div.Content);
                        }

                        cellWidth = startWidth + widthDeviation + (float)divActualWidth;
                        rulesWidth.Add(cellWidth);
                    }
                }
            }
            cellWidth = startWidth;
            foreach (float width in rulesWidth) {
                if(width > cellWidth) {
                    cellWidth = width;
                }
            }
            return new float[]{cellWidth, widestWordOfCell};
        }
Beispiel #13
0
 /*
  * (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;
 }