From 35dde4f353e206f47c54e594bba1aa8e4846dc78 Mon Sep 17 00:00:00 2001 From: "Hugh.C" Date: Mon, 13 Apr 2020 13:53:45 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-27284=20table=E8=A7=A3=E6=9E=90=E5=AF=BC?= =?UTF-8?q?=E5=87=BAPDF=E5=90=8E=20=E5=8D=95=E5=85=83=E6=A0=BC=E8=83=8C?= =?UTF-8?q?=E6=99=AF=E3=80=81=E8=BE=B9=E6=A1=86=E4=B8=8E=E9=A2=84=E8=A7=88?= =?UTF-8?q?=E4=B8=8D=E4=B8=80=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../text/html/Utils/BackgroundUtil.java | 48 +++++++++++++++++++ .../text/html/simpleparser/IncCell.java | 33 +------------ .../text/html/simpleparser/IncTable.java | 21 ++++++-- .../third/v2/lowagie/text/pdf/PdfPTable.java | 6 +-- .../v2/lowagie/text/pdf/TableProperties.java | 15 ++++++ 5 files changed, 83 insertions(+), 40 deletions(-) create mode 100644 fine-itext/src/com/fr/third/v2/lowagie/text/html/Utils/BackgroundUtil.java 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()){