Browse Source

REPORT-34112 review 改进

research/11.0
Hugh.C 4 years ago
parent
commit
71ad6c59cf
  1. 35
      fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/IncTable.java
  2. 15
      fine-itext/src/main/java/com/fr/third/v2/lowagie/text/pdf/PdfPRow.java
  3. 33
      fine-itext/src/main/java/com/fr/third/v2/lowagie/text/pdf/PdfPTable.java

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

@ -112,10 +112,7 @@ public class IncTable {
return new PdfPTable(1); return new PdfPTable(1);
ArrayList c0 = (ArrayList) rows.get(0); ArrayList c0 = (ArrayList) rows.get(0);
String[] firstColWidths = new String[c0.size()]; List<String> firstLineColWidths = getFirstLineColWidths();
for (int k = 0; k < c0.size(); ++k) {
firstColWidths[k] = ((PdfPCell) c0.get(k)).getOriginalStyleWidth();
}
int ncol = getMaxColCount(); int ncol = getMaxColCount();
processColWidth(ncol); processColWidth(ncol);
@ -124,7 +121,7 @@ public class IncTable {
try { try {
TableProperties tableProperties = parseTableProperties(); TableProperties tableProperties = parseTableProperties();
table.setTableProperties(tableProperties); table.setTableProperties(tableProperties);
table.setFirstColWidths(firstColWidths); table.setFirstLineColWidths(firstLineColWidths);
//相对宽度 //相对宽度
float[] floats = new float[relativeColWidths.size()]; float[] floats = new float[relativeColWidths.size()];
@ -151,11 +148,13 @@ public class IncTable {
} }
for (int row = 0; row < rows.size(); ++row) { for (int row = 0; row < rows.size(); ++row) {
ArrayList col = (ArrayList) rows.get(row); ArrayList col = (ArrayList) rows.get(row);
int colCount=0;
for (int k = 0; k < col.size(); ++k) { for (int k = 0; k < col.size(); ++k) {
table.addCell((PdfPCell) col.get(k)); table.addCell((PdfPCell) col.get(k));
colCount += ((PdfPCell) col.get(k)).getColspan();
} }
//补充空白列 //补充空白列
for (int i = 0; i < ncol - c0.size(); i++) { for (int i = 0; i < ncol - colCount; i++) {
PdfPCell pdfPCell = new PdfPCell(); PdfPCell pdfPCell = new PdfPCell();
pdfPCell.setInvalid(true); pdfPCell.setInvalid(true);
table.addCell(pdfPCell); table.addCell(pdfPCell);
@ -169,6 +168,30 @@ public class IncTable {
return table; return table;
} }
/**
* 获取第一行的列宽
*
* @return
*/
private List<String> getFirstLineColWidths() {
ArrayList c0 = (ArrayList) rows.get(0);
List<String> firstLineColWidths = new ArrayList<String>();
for (int i = 0; i < c0.size(); i++) {
PdfPCell pdfPCell = (PdfPCell) c0.get(i);
String width = pdfPCell.getOriginalStyleWidth();
if (null != width && 1 < pdfPCell.getColspan()) {
float w = CSSUtils.parseFloat(width);
//平分且需要保持原始的样式(有百分号的需要加上百分百)
w /= pdfPCell.getColspan();
width = width.endsWith("%") ? String.valueOf(w) + "%" : String.valueOf(w);
}
for (int j = 0; j < pdfPCell.getColspan(); j++) {
firstLineColWidths.add(width);
}
}
return firstLineColWidths;
}
/** /**
* 获取最大列数 * 获取最大列数
* *

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

@ -141,7 +141,7 @@ public class PdfPRow {
* @param widths * @param widths
* @return true if everything went right * @return true if everything went right
*/ */
public boolean setWidths(float widths[]) { public boolean setWidths(float widths[], TableProperties tablePropertie) {
if (widths.length != cells.length) if (widths.length != cells.length)
return false; return false;
System.arraycopy(widths, 0, this.widths, 0, cells.length); System.arraycopy(widths, 0, this.widths, 0, cells.length);
@ -160,6 +160,19 @@ public class PdfPRow {
for (; k < last; ++k) for (; k < last; ++k)
total += widths[k]; total += widths[k];
--k; --k;
float borderWidth=0;
float cellspacing=0;
float cellpadding=0;
if (null != tablePropertie) {
borderWidth = tablePropertie.getBorderWidth();
cellspacing = tablePropertie.getCellspacing();
cellpadding = tablePropertie.getCellpadding();
}
if (cell.getColspan() > 1) {
total += ((cell.getColspan() - 1) * 2 * borderWidth);
total += ((cell.getColspan() - 1) * cellspacing);
total += ((cell.getColspan() - 1) * 2 * cellpadding);
}
cell.setRight(total); cell.setRight(total);
cell.setTop(0); cell.setTop(0);
processColWidth(cell.getColumn().getCompositeElements(), cell.getWidth()); processColWidth(cell.getColumn().getCompositeElements(), cell.getWidth());

33
fine-itext/src/main/java/com/fr/third/v2/lowagie/text/pdf/PdfPTable.java

@ -108,7 +108,7 @@ public class PdfPTable implements LargeElement {
protected PdfPTableEvent tableEvent; protected PdfPTableEvent tableEvent;
private TableProperties tableProperties; private TableProperties tableProperties;
private String[] firstColWidths; private List<String> firstLineColWidths;
public TableProperties getTableProperties() { public TableProperties getTableProperties() {
return tableProperties; return tableProperties;
@ -118,8 +118,8 @@ public class PdfPTable implements LargeElement {
this.tableProperties = tableProperties; this.tableProperties = tableProperties;
} }
public void setFirstColWidths(String[] firstColWidths) { public void setFirstLineColWidths(List<String> firstLineColWidths) {
this.firstColWidths = firstColWidths; this.firstLineColWidths = firstLineColWidths;
} }
/** /**
@ -389,7 +389,7 @@ public class PdfPTable implements LargeElement {
private void adjustRowWidth() { private void adjustRowWidth() {
for (int k = 0; k < rows.size(); ++k) { for (int k = 0; k < rows.size(); ++k) {
PdfPRow row = (PdfPRow) rows.get(k); PdfPRow row = (PdfPRow) rows.get(k);
row.setWidths(absoluteWidths); row.setWidths(absoluteWidths, tableProperties);
} }
} }
@ -400,8 +400,10 @@ public class PdfPTable implements LargeElement {
*/ */
private float calOutOfContentWidth() { private float calOutOfContentWidth() {
int numCols = getNumberOfColumns(); int numCols = getNumberOfColumns();
float borderWidth = tableProperties.getBorderWidth(); float allCellspacingWidth = tableProperties.getCellspacing() * (numCols + 1);
return tableProperties.getCellspacing() * (numCols + 1) + numCols * 2 * (borderWidth + tableProperties.getCellpadding()) + 2 * borderWidth; float allCellpaddingWidth = numCols * 2 * tableProperties.getCellpadding();
float allCellBorderWidth = (numCols + 1) * 2 * tableProperties.getBorderWidth();
return allCellspacingWidth + allCellpaddingWidth + allCellBorderWidth;
} }
/** /**
@ -429,7 +431,7 @@ public class PdfPTable implements LargeElement {
} }
//第一行列宽 //第一行列宽
float[] colWidths = new float[firstColWidths.length]; float[] colWidths = new float[firstLineColWidths.size()];
//绝对宽度和 //绝对宽度和
float absWidthSum = 0; float absWidthSum = 0;
//相对宽度和 //相对宽度和
@ -440,10 +442,11 @@ public class PdfPTable implements LargeElement {
ArrayList<Integer> relIndex = new ArrayList<Integer>(); ArrayList<Integer> relIndex = new ArrayList<Integer>();
//初始化一下上述字段 //初始化一下上述字段
for (int i = 0; i < firstColWidths.length; i++) { for (int i = 0; i < firstLineColWidths.size(); i++) {
float w = CSSUtils.parseFloat(firstColWidths[i]); String widthStr = firstLineColWidths.get(i);
if (null != firstColWidths[i]) { float w = CSSUtils.parseFloat(widthStr);
if (firstColWidths[i].endsWith("%")) { if (null != widthStr) {
if (widthStr.endsWith("%")) {
w = w / 100 * contentWidth; w = w / 100 * contentWidth;
relWidthSum += w; relWidthSum += w;
relIndex.add(i); relIndex.add(i);
@ -591,7 +594,7 @@ public class PdfPTable implements LargeElement {
} }
float invalidColWidth = invalidColNum * tableProperties.getCellspacing() + 2 * invalidColNum * (tableProperties.getCellpadding() + tableProperties.getBorderWidth()); float invalidColWidth = invalidColNum * tableProperties.getCellspacing() + 2 * invalidColNum * (tableProperties.getCellpadding() + tableProperties.getBorderWidth());
float avgWidth = invalidColWidth / validIndexList.size(); float avgWidth = invalidColWidth / validIndexList.size();
for (int i = 0; i < validIndexList.size() && i < absoluteWidths.length; i++) { for (int i = 0; i < validIndexList.size() && validIndexList.get(i) < absoluteWidths.length; i++) {
Integer index = validIndexList.get(i); Integer index = validIndexList.get(i);
absoluteWidths[index] += avgWidth; absoluteWidths[index] += avgWidth;
} }
@ -604,7 +607,7 @@ public class PdfPTable implements LargeElement {
* @return * @return
*/ */
private boolean dealFirstLineHasNoWidth(float contentWidth) { private boolean dealFirstLineHasNoWidth(float contentWidth) {
if (null != firstColWidths && 0 != firstColWidths.length) { if (null != firstLineColWidths && 0 != firstLineColWidths.size()) {
return false; return false;
} }
//第一行的td都没有设置width属性,则所有列平分table宽度 //第一行的td都没有设置width属性,则所有列平分table宽度
@ -747,7 +750,7 @@ public class PdfPTable implements LargeElement {
} }
PdfPRow row = new PdfPRow(currentRow); PdfPRow row = new PdfPRow(currentRow);
if (totalWidth > 0) { if (totalWidth > 0) {
row.setWidths(absoluteWidths); row.setWidths(absoluteWidths, tableProperties);
// totalHeight += row.getMaxHeights(); // totalHeight += row.getMaxHeights();
} }
rows.add(row); rows.add(row);
@ -1114,7 +1117,7 @@ public class PdfPTable implements LargeElement {
if (row == null) if (row == null)
return 0; return 0;
if (firsttime) if (firsttime)
row.setWidths(absoluteWidths); row.setWidths(absoluteWidths, tableProperties);
float height = row.getMaxHeights(); float height = row.getMaxHeights();
PdfPCell cell; PdfPCell cell;
PdfPRow tmprow; PdfPRow tmprow;

Loading…
Cancel
Save