diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/html/Utils/BackgroundUtil.java b/fine-itext/src/com/fr/third/v2/lowagie/text/html/Utils/BackgroundUtil.java new file mode 100644 index 000000000..61c0ce452 --- /dev/null +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/html/Utils/BackgroundUtil.java @@ -0,0 +1,48 @@ +package com.fr.third.v2.lowagie.text.html.Utils; + +import com.fr.third.v2.lowagie.text.html.CSSUtils; +import com.fr.third.v2.lowagie.text.html.simpleparser.ChainedProperties; +import java.util.HashMap; +import java.util.Map; + +/** + * @author Hugh.C + * @version 1.0 + * Created by Hugh.C on 2020/2/26 + */ +public class BackgroundUtil { + + public static Map parse2RulesMap(ChainedProperties props) { + Map backgroundRules = new HashMap(); + String value = props.getProperty("bgcolor"); + if (value != null) { + backgroundRules.put("background-color", value); + } + value = props.getLastChainProperty("background-size"); + if (value != null) { + backgroundRules.put("background-size", value); + } + value = props.getLastChainProperty("background"); + if (value != null) { + Map backgroundStyles = CSSUtils.processBackground(value); + backgroundRules.putAll(backgroundStyles); + } + value = props.getLastChainProperty("background-color"); + if (value != null) { + backgroundRules.put("background-color", value); + } + value = props.getLastChainProperty("background-position"); + if (value != null) { + backgroundRules.put("background-position", value); + } + value = props.getLastChainProperty("background-repeat"); + if (value != null) { + backgroundRules.put("background-repeat", value); + } + value = props.getLastChainProperty("background-image"); + if (value != null) { + backgroundRules.put("background-image", value); + } + return backgroundRules; + } +} diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/IncCell.java b/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/IncCell.java index 6e0597921..8b8318bb1 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/IncCell.java +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/IncCell.java @@ -47,6 +47,7 @@ package com.fr.third.v2.lowagie.text.html.simpleparser; +import com.fr.third.v2.lowagie.text.html.Utils.BackgroundUtil; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -117,37 +118,7 @@ public class IncCell implements TextElementArray { cell.setStyleHeight(CSSUtils.parseFloat(value)); } //解析background相关属性并保存到cell对象 - Map backgroundRules = new HashMap(); - value = props.getProperty("bgcolor"); - if (value != null) { - backgroundRules.put("background-color", value); - } - value = props.getLastChainProperty("background-size"); - if (value != null) { - backgroundRules.put("background-size", value); - } - value = props.getLastChainProperty("background"); - if (value != null) { - Map backgroundStyles = CSSUtils.processBackground(value); - backgroundRules.putAll(backgroundStyles); - } - value = props.getLastChainProperty("background-color"); - if (value != null) { - backgroundRules.put("background-color", value); - } - value = props.getLastChainProperty("background-position"); - if (value != null) { - backgroundRules.put("background-position", value); - } - value = props.getLastChainProperty("background-repeat"); - if (value != null) { - backgroundRules.put("background-repeat", value); - } - value = props.getLastChainProperty("background-image"); - if (value != null) { - backgroundRules.put("background-image", value); - } - cell.setBackground(backgroundRules); + cell.setBackground(BackgroundUtil.parse2RulesMap(props)); } public boolean add(Object o) { diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/IncTable.java b/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/IncTable.java index 9c99d6370..0e99b6d65 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/IncTable.java +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/IncTable.java @@ -47,6 +47,8 @@ package com.fr.third.v2.lowagie.text.html.simpleparser; +import com.fr.third.v2.lowagie.text.html.HtmlTags; +import com.fr.third.v2.lowagie.text.html.Utils.BackgroundUtil; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -174,12 +176,12 @@ public class IncTable { } for (int rowIndex = 0; rowIndex < rows.size(); rowIndex++) { ArrayList cols = (ArrayList) rows.get(rowIndex); - for (int colIndex = 0; colIndex < cols.size(); colIndex++) { + int colSpan = 1; + for (int colIndex = 0; colIndex < cols.size(); colIndex+=colSpan) { PdfPCell pCell = ((PdfPCell) cols.get(colIndex)); - - int cellCols = pCell.getColspan(); - float avgWidth = pCell.getStyleWidth() / cellCols; - for (int i = 0; i < cellCols && colIndex + i < colCount; i++) { + colSpan = pCell.getColspan(); + float avgWidth = pCell.getStyleWidth() / colSpan; + for (int i = 0; i < colSpan && colIndex + i < colCount; i++) { if (relativeColWidths.get(colIndex + i) < avgWidth) { relativeColWidths.set(colIndex + i, avgWidth); } @@ -271,6 +273,11 @@ public class IncTable { if(value != null){ borderStyle.setBorderColor(Markup.decodeColor(value)); } + value = (String) props.get("bordercolor"); + if (value != null) { + borderStyle.setBorderColor(Markup.decodeColor(value)); + } + value = (String)props.get("border-collapse"); if(value != null){ tableProperties.setCollapse(value.equals("collapse")); @@ -283,6 +290,10 @@ public class IncTable { if(value != null){ tableProperties.setCellpadding(CSSUtils.parseFloat(value)); } + ChainedProperties properties = new ChainedProperties(); + properties.addToChain(HtmlTags.TABLE, props); + //解析background相关属性 + tableProperties.setBackground(BackgroundUtil.parse2RulesMap(properties)); return tableProperties; } } diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPTable.java b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPTable.java index 199d21d6f..38d9a250f 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPTable.java +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPTable.java @@ -350,8 +350,7 @@ public class PdfPTable implements LargeElement { return; float total = 0; int numCols = getNumberOfColumns(); - BorderStyle borderStyle = tableProperties.getBorderStyle(); - float borderWidth = tableProperties.isCollapse() ? borderStyle.getBorderWidth() / 2 : borderStyle.getBorderWidth(); + float borderWidth = tableProperties.getBorderWidth(); float cellspacing = tableProperties.getCellspacing(); float cellpadding = tableProperties.getCellpadding(); for (int k = 0; k < numCols; ++k) @@ -435,8 +434,7 @@ public class PdfPTable implements LargeElement { if (totalWidth <= 0) return 0; totalHeight = 0; - BorderStyle borderStyle = tableProperties.getBorderStyle(); - float borderWidth = tableProperties.isCollapse() ? borderStyle.getBorderWidth() / 2 : borderStyle.getBorderWidth(); + float borderWidth = tableProperties.getBorderWidth(); float cellspacing = tableProperties.getCellspacing(); float cellpadding = tableProperties.getCellpadding(); for (int k = 0; k < rows.size(); ++k) { diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/TableProperties.java b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/TableProperties.java index fa1c7de0b..a886f136e 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/TableProperties.java +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/TableProperties.java @@ -1,5 +1,7 @@ package com.fr.third.v2.lowagie.text.pdf; +import java.util.Map; + /** * 描述table的属性类 * 包括cellspacing、cellpadding、border等 @@ -9,11 +11,16 @@ public class TableProperties { private float cellspacing = 2.0f; private float cellpadding = 1.0f; private boolean collapse = false; + public Map background; public BorderStyle getBorderStyle() { return borderStyle; } + public float getBorderWidth() { + return 0 == getCellspacing() ? borderStyle.getBorderWidth() / 2 : borderStyle.getBorderWidth(); + } + public void setBorderStyle(BorderStyle borderStyle) { this.borderStyle = borderStyle; } @@ -42,6 +49,14 @@ public class TableProperties { this.collapse = collapse; } + public Map getBackground() { + return background; + } + + public void setBackground(Map background) { + this.background = background; + } + public String toHtmlString(){ StringBuffer sb = new StringBuffer(); if(!isCollapse()){