Browse Source

REPORT-27284 table解析导出PDF后 单元格背景、边框与预览不一致

release/10.0
Hugh.C 5 years ago
parent
commit
35dde4f353
  1. 48
      fine-itext/src/com/fr/third/v2/lowagie/text/html/Utils/BackgroundUtil.java
  2. 33
      fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/IncCell.java
  3. 21
      fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/IncTable.java
  4. 6
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPTable.java
  5. 15
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/TableProperties.java

48
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<String, String> parse2RulesMap(ChainedProperties props) {
Map<String, String> backgroundRules = new HashMap<String, String>();
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<String, String> 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;
}
}

33
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<String, String> backgroundRules = new HashMap<String, 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<String, String> 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) {

21
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;
}
}

6
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) {

15
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的属性类
* 包括cellspacingcellpaddingborder等
@ -9,11 +11,16 @@ public class TableProperties {
private float cellspacing = 2.0f;
private float cellpadding = 1.0f;
private boolean collapse = false;
public Map<String, String> 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<String, String> getBackground() {
return background;
}
public void setBackground(Map<String, String> background) {
this.background = background;
}
public String toHtmlString(){
StringBuffer sb = new StringBuffer();
if(!isCollapse()){

Loading…
Cancel
Save