Browse Source

REPORT-25504 <table> 标签中的内容不按照列宽进行换行,行高逻辑错误

release/10.0
Hugh.C 5 years ago
parent
commit
e847825b7a
  1. 14
      fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HTMLWorker.java
  2. 54
      fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/IncTable.java
  3. 61
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPRow.java

14
fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HTMLWorker.java

@ -63,6 +63,7 @@ import java.util.Map;
import java.util.Stack; import java.util.Stack;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import com.fr.third.v2.lowagie.text.Cell;
import com.fr.third.v2.lowagie.text.DocumentException; import com.fr.third.v2.lowagie.text.DocumentException;
import com.fr.third.v2.lowagie.text.Element; import com.fr.third.v2.lowagie.text.Element;
import com.fr.third.v2.lowagie.text.ExceptionConverter; import com.fr.third.v2.lowagie.text.ExceptionConverter;
@ -74,6 +75,8 @@ import com.fr.third.v2.lowagie.text.Rectangle;
import com.fr.third.v2.lowagie.text.TextElementArray; import com.fr.third.v2.lowagie.text.TextElementArray;
import com.fr.third.v2.lowagie.text.html.CSSUtils; import com.fr.third.v2.lowagie.text.html.CSSUtils;
import com.fr.third.v2.lowagie.text.html.HtmlTags; import com.fr.third.v2.lowagie.text.html.HtmlTags;
import com.fr.third.v2.lowagie.text.pdf.PdfCell;
import com.fr.third.v2.lowagie.text.pdf.PdfPCell;
import com.fr.third.v2.lowagie.text.pdf.draw.LineSeparator; import com.fr.third.v2.lowagie.text.pdf.draw.LineSeparator;
import com.fr.third.v2.lowagie.text.xml.simpleparser.SimpleXMLDocHandler; import com.fr.third.v2.lowagie.text.xml.simpleparser.SimpleXMLDocHandler;
import com.fr.third.v2.lowagie.text.html.Markup; import com.fr.third.v2.lowagie.text.html.Markup;
@ -661,10 +664,13 @@ public class HTMLWorker implements SimpleXMLDocHandler, DocListener {
cprops.removeChain("tr"); cprops.removeChain("tr");
ArrayList cells = new ArrayList(); ArrayList cells = new ArrayList();
IncTable table = null; IncTable table = null;
float maxHeight = 0;
while (true) { while (true) {
Object obj = stack.pop(); Object obj = stack.pop();
if (obj instanceof IncCell) { if (obj instanceof IncCell) {
cells.add(((IncCell) obj).getCell()); PdfPCell cell = ((IncCell) obj).getCell();
cells.add(cell);
maxHeight = Math.max(maxHeight, cell.getStyleHeight());
} }
if (obj instanceof IncTable) { if (obj instanceof IncTable) {
table = (IncTable) obj; table = (IncTable) obj;
@ -672,11 +678,11 @@ public class HTMLWorker implements SimpleXMLDocHandler, DocListener {
} }
} }
float rowHeight = 0.0f; float rowHeight = 0.0f;
if(rowHeightPx!=null){ if (rowHeightPx != null) {
rowHeight = CSSUtils.parseFloat(rowHeightPx); rowHeight = CSSUtils.parseFloat(rowHeightPx);
} }
table.addCols(cells); table.addCols(cells);
table.endRow(rowHeight); table.endRow(Math.max(rowHeight, maxHeight));
stack.push(table); stack.push(table);
skipText = true; skipText = true;

54
fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/IncTable.java

@ -50,6 +50,7 @@ package com.fr.third.v2.lowagie.text.html.simpleparser;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import com.fr.third.v2.lowagie.text.html.CSSUtils; import com.fr.third.v2.lowagie.text.html.CSSUtils;
import com.fr.third.v2.lowagie.text.html.Markup; import com.fr.third.v2.lowagie.text.html.Markup;
@ -66,6 +67,7 @@ public class IncTable {
private HashMap props = new HashMap(); private HashMap props = new HashMap();
private ArrayList rows = new ArrayList(); private ArrayList rows = new ArrayList();
private ArrayList cols; private ArrayList cols;
private List<Integer> rowIndex4ZeroHeight = new ArrayList();
private ArrayList<Float> rowHeights = new ArrayList<Float>(); private ArrayList<Float> rowHeights = new ArrayList<Float>();
private ArrayList<Float> relativeColWidths = new ArrayList<Float>(); private ArrayList<Float> relativeColWidths = new ArrayList<Float>();
@ -88,6 +90,9 @@ public class IncTable {
} }
public void endRow(float rowHeight) { public void endRow(float rowHeight) {
if (rowHeight == 0) {
rowIndex4ZeroHeight.add(rowHeights.size());
}
rowHeights.add(rowHeight); rowHeights.add(rowHeight);
if (cols != null) { if (cols != null) {
Collections.reverse(cols); Collections.reverse(cols);
@ -204,12 +209,53 @@ public class IncTable {
} }
public void processRowHeight(PdfPTable table) { public void processRowHeight(PdfPTable table) {
Float height = CSSUtils.parseFloat((String) props.get("height")); adjustRowHeight(CSSUtils.parseFloat((String) props.get("height")));
Float eachHeight = height / table.getRows().size();
//调整行高 //调整行高
for (int a = 0; a < rowHeights.size(); a++) { for (int i = 0; i < rowHeights.size(); i++) {
table.getRow(a).setStyleHeight(Math.max(eachHeight, rowHeights.get(a))); table.getRow(i).setStyleHeight(rowHeights.get(i));
}
}
/**
* 调整行高
* 1 tableHeight < rowHeightSum , return 每行扔取取指定的 height (该行的最大值)
* 2 tableHeight > rowHeightSum ,每行都有指定 height (该行的最大值),则按比例平分tableHeight
* 3 tableHeight > rowHeightSum ,有些行没有指定 height ,则将tableHeight - rowHeightSum 平分给这些行
* 最后若计算出的内容高度大于上述计算出的行高则取内容高度
*
* @param tableHeight
*/
private void adjustRowHeight(Float tableHeight) {
if (0 > tableHeight) {
return;
}
float totalHeight = getRowHeightSum();
if (tableHeight < totalHeight) {
return;
}
float extraHeight = tableHeight - totalHeight;
int size = rowIndex4ZeroHeight.size();
//多出来的高度平分给没有设置高度的行
if (size > 0) {
float eachHeight = extraHeight / size;
for (int i = 0; i < size; i++) {
rowHeights.set(rowIndex4ZeroHeight.get(i), eachHeight);
}
}
//按比例分
else {
for (int i = 0; i < rowHeights.size(); i++) {
rowHeights.set(i, tableHeight * rowHeights.get(i) / totalHeight);
}
}
}
private float getRowHeightSum() {
float sum = 0;
for (int i = 0; i < rowHeights.size(); i++) {
sum += rowHeights.get(i);
} }
return sum;
} }
public TableProperties parseTableProperties(){ public TableProperties parseTableProperties(){

61
fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPRow.java

@ -50,12 +50,19 @@
package com.fr.third.v2.lowagie.text.pdf; package com.fr.third.v2.lowagie.text.pdf;
import java.awt.Color; import java.awt.Color;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import com.fr.third.v2.lowagie.text.ExceptionConverter; import com.fr.third.v2.lowagie.text.ExceptionConverter;
import com.fr.third.v2.lowagie.text.DocumentException; import com.fr.third.v2.lowagie.text.DocumentException;
import com.fr.third.v2.lowagie.text.Element; import com.fr.third.v2.lowagie.text.Element;
import com.fr.third.v2.lowagie.text.Image; import com.fr.third.v2.lowagie.text.Image;
import com.fr.third.v2.lowagie.text.ListItem;
import com.fr.third.v2.lowagie.text.Paragraph;
import com.fr.third.v2.lowagie.text.Rectangle; import com.fr.third.v2.lowagie.text.Rectangle;
import com.fr.third.v2.lowagie.text.html.CSS;
/** /**
* A row in a PdfPTable. * A row in a PdfPTable.
@ -155,10 +162,64 @@ public class PdfPRow {
--k; --k;
cell.setRight(total); cell.setRight(total);
cell.setTop(0); cell.setTop(0);
processColWidth(cell.getColumn().getCompositeElements(), cell.getWidth());
} }
return true; return true;
} }
/**
* @param list
* @param width
*/
private void processColWidth(List<Element> list, float width) {
if (null == list) {
return;
}
String widthStr = String.valueOf(width);
//调整列宽,不然内容换行任取指定的width
for (int i = 0; i < list.size(); i++) {
setStyleWidth(list.get(i), widthStr);
}
}
private void setStyleWidth(Element element, String width) {
if (null == element) {
return;
}
try {
switch (element.type()) {
case Element.PARAGRAPH: {
HashMap attr = ((Paragraph) element).getAttributes();
attr.put(CSS.Property.WIDTH, width);
break;
}
case Element.LIST: {
com.fr.third.v2.lowagie.text.List list = (com.fr.third.v2.lowagie.text.List) element;
for (Iterator i = list.getList().iterator(); i.hasNext(); ) {
setStyleWidth((Element) i.next(), width);
}
break;
}
case Element.LISTITEM: {
ListItem listItem = (ListItem) element;
for (Iterator i = listItem.iterator(); i.hasNext(); ) {
setStyleWidth((Element) i.next(), width);
}
break;
}
case Element.PTABLE: {
PdfPTable table = (PdfPTable) element;
table.setTotalWidth(Float.parseFloat(width));
break;
}
default:
return;
}
} catch (Exception e) {
throw new ExceptionConverter(e);
}
}
/** /**
* Initializes the extra heights array. * Initializes the extra heights array.
* @since 2.1.6 * @since 2.1.6

Loading…
Cancel
Save