|
|
@ -68,6 +68,7 @@ public class IncTable { |
|
|
|
private ArrayList cols; |
|
|
|
private ArrayList cols; |
|
|
|
|
|
|
|
|
|
|
|
private ArrayList<Float> rowHeights = new ArrayList<Float>(); |
|
|
|
private ArrayList<Float> rowHeights = new ArrayList<Float>(); |
|
|
|
|
|
|
|
private ArrayList<Float> relativeColWidths = new ArrayList<Float>(); |
|
|
|
/** Creates a new instance of IncTable */ |
|
|
|
/** Creates a new instance of IncTable */ |
|
|
|
public IncTable(HashMap props) { |
|
|
|
public IncTable(HashMap props) { |
|
|
|
this.props.putAll(props); |
|
|
|
this.props.putAll(props); |
|
|
@ -103,33 +104,30 @@ public class IncTable { |
|
|
|
if (rows.isEmpty()) |
|
|
|
if (rows.isEmpty()) |
|
|
|
return new PdfPTable(1); |
|
|
|
return new PdfPTable(1); |
|
|
|
int ncol = 0; |
|
|
|
int ncol = 0; |
|
|
|
ArrayList c0 = (ArrayList)rows.get(0); |
|
|
|
ArrayList c0 = (ArrayList) rows.get(0); |
|
|
|
ArrayList<Float> colWidths = new ArrayList<Float>(); |
|
|
|
|
|
|
|
float widthSum = 0; |
|
|
|
|
|
|
|
for (int k = 0; k < c0.size(); ++k) { |
|
|
|
for (int k = 0; k < c0.size(); ++k) { |
|
|
|
PdfPCell pCell = ((PdfPCell)c0.get(k)); |
|
|
|
ncol += ((PdfPCell) c0.get(k)).getColspan(); |
|
|
|
int cellCols = pCell.getColspan(); |
|
|
|
|
|
|
|
ncol += cellCols; |
|
|
|
|
|
|
|
//不取 0 ,后面可能需要求比值
|
|
|
|
|
|
|
|
float styleWidth = pCell.getStyleWidth(); |
|
|
|
|
|
|
|
if(cellCols > 1){ |
|
|
|
|
|
|
|
float avgWidth = styleWidth / cellCols; |
|
|
|
|
|
|
|
float width = 0 == avgWidth ? 1 : avgWidth; |
|
|
|
|
|
|
|
widthSum = width * cellCols; |
|
|
|
|
|
|
|
for(int a = 0; a < cellCols ; a++){ |
|
|
|
|
|
|
|
colWidths.add(width); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}else { |
|
|
|
|
|
|
|
float width = 0 == styleWidth ? 1 : styleWidth; |
|
|
|
|
|
|
|
widthSum += width; |
|
|
|
|
|
|
|
colWidths.add(width); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
processColWidth(ncol); |
|
|
|
|
|
|
|
|
|
|
|
PdfPTable table = new PdfPTable(ncol); |
|
|
|
PdfPTable table = new PdfPTable(ncol); |
|
|
|
try { |
|
|
|
try { |
|
|
|
TableProperties tableProperties = parseTableProperties(); |
|
|
|
TableProperties tableProperties = parseTableProperties(); |
|
|
|
table.setTableProperties(tableProperties); |
|
|
|
table.setTableProperties(tableProperties); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//相对宽度
|
|
|
|
|
|
|
|
float[] floats = new float[relativeColWidths.size()]; |
|
|
|
|
|
|
|
for (int a = 0; a < relativeColWidths.size(); a++) { |
|
|
|
|
|
|
|
floats[a] = relativeColWidths.get(a); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
table.setWidths(floats); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String width = (String)props.get("width"); |
|
|
|
String width = (String)props.get("width"); |
|
|
|
if (width == null) |
|
|
|
if (width == null) |
|
|
|
table.setWidthPercentage(100); |
|
|
|
table.setWidthPercentage(100); |
|
|
@ -138,18 +136,7 @@ public class IncTable { |
|
|
|
table.setWidthPercentage(CSSUtils.parseFloat(width)); |
|
|
|
table.setWidthPercentage(CSSUtils.parseFloat(width)); |
|
|
|
else { |
|
|
|
else { |
|
|
|
//解析单元格宽度
|
|
|
|
//解析单元格宽度
|
|
|
|
float totalWidth = CSSUtils.parseFloat(width); |
|
|
|
table.setTotalWidth(CSSUtils.parseFloat(width)); |
|
|
|
float[] floats = new float[colWidths.size()]; |
|
|
|
|
|
|
|
for (int a = 0; a < colWidths.size(); a++) { |
|
|
|
|
|
|
|
floats[a] = totalWidth * colWidths.get(a) / widthSum; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//解析px数值
|
|
|
|
|
|
|
|
table.setTotalWidth(totalWidth); |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
table.setWidths(floats); |
|
|
|
|
|
|
|
}catch (Exception e){ |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
table.setLockedWidth(true); |
|
|
|
table.setLockedWidth(true); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -161,14 +148,60 @@ public class IncTable { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
processRowHeight(table); |
|
|
|
processRowHeight(table); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}catch (Exception e){ |
|
|
|
}catch (Exception e){ |
|
|
|
e.printStackTrace(); |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
return table; |
|
|
|
return table; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 调整列宽,每列取最大值 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param colCount 列数 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private void processColWidth(int colCount) { |
|
|
|
|
|
|
|
if (null == rows || 0 == rows.size()) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//初始化一下
|
|
|
|
|
|
|
|
for (int i = 0; i < colCount; i++) { |
|
|
|
|
|
|
|
relativeColWidths.add(0f); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for (int rowIndex = 0; rowIndex < rows.size(); rowIndex++) { |
|
|
|
|
|
|
|
ArrayList cols = (ArrayList) rows.get(rowIndex); |
|
|
|
|
|
|
|
for (int colIndex = 0; colIndex < cols.size(); colIndex++) { |
|
|
|
|
|
|
|
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++) { |
|
|
|
|
|
|
|
if (relativeColWidths.get(i) < avgWidth) { |
|
|
|
|
|
|
|
relativeColWidths.set(colIndex + i, avgWidth); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
dealRelativeColWidthsWhenAllAreZero(relativeColWidths); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 全部为0,则全部赋值1 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param relativeColWidths 相对宽度 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private void dealRelativeColWidthsWhenAllAreZero(ArrayList<Float> relativeColWidths) { |
|
|
|
|
|
|
|
if (0 == relativeColWidths.size()) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for (int i = 0; i < relativeColWidths.size(); i++) { |
|
|
|
|
|
|
|
if (0 != relativeColWidths.get(i)) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
for (int i = 0; i < relativeColWidths.size(); i++) { |
|
|
|
|
|
|
|
relativeColWidths.set(i, 1f); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void processRowHeight(PdfPTable table) { |
|
|
|
public void processRowHeight(PdfPTable table) { |
|
|
|
Float height = CSSUtils.parseFloat((String) props.get("height")); |
|
|
|
Float height = CSSUtils.parseFloat((String) props.get("height")); |
|
|
|
Float eachHeight = height / table.getRows().size(); |
|
|
|
Float eachHeight = height / table.getRows().size(); |
|
|
|