From 71ad6c59cf5b9756de8aa09354d04d0d15f297cd Mon Sep 17 00:00:00 2001 From: "Hugh.C" Date: Tue, 23 Jun 2020 11:43:10 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-34112=20review=20=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../text/html/simpleparser/IncTable.java | 35 +++++++++++++++---- .../fr/third/v2/lowagie/text/pdf/PdfPRow.java | 15 +++++++- .../third/v2/lowagie/text/pdf/PdfPTable.java | 33 +++++++++-------- 3 files changed, 61 insertions(+), 22 deletions(-) diff --git a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/IncTable.java b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/IncTable.java index 3b6fa35da..9b580d395 100644 --- a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/html/simpleparser/IncTable.java +++ b/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); ArrayList c0 = (ArrayList) rows.get(0); - String[] firstColWidths = new String[c0.size()]; - for (int k = 0; k < c0.size(); ++k) { - firstColWidths[k] = ((PdfPCell) c0.get(k)).getOriginalStyleWidth(); - } + List firstLineColWidths = getFirstLineColWidths(); int ncol = getMaxColCount(); processColWidth(ncol); @@ -124,7 +121,7 @@ public class IncTable { try { TableProperties tableProperties = parseTableProperties(); table.setTableProperties(tableProperties); - table.setFirstColWidths(firstColWidths); + table.setFirstLineColWidths(firstLineColWidths); //相对宽度 float[] floats = new float[relativeColWidths.size()]; @@ -151,11 +148,13 @@ public class IncTable { } for (int row = 0; row < rows.size(); ++row) { ArrayList col = (ArrayList) rows.get(row); + int colCount=0; for (int k = 0; k < col.size(); ++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.setInvalid(true); table.addCell(pdfPCell); @@ -169,6 +168,30 @@ public class IncTable { return table; } + /** + * 获取第一行的列宽 + * + * @return + */ + private List getFirstLineColWidths() { + ArrayList c0 = (ArrayList) rows.get(0); + List firstLineColWidths = new ArrayList(); + 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; + } + /** * 获取最大列数 * diff --git a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/pdf/PdfPRow.java b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/pdf/PdfPRow.java index 466ee0df8..38a03958b 100644 --- a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/pdf/PdfPRow.java +++ b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/pdf/PdfPRow.java @@ -141,7 +141,7 @@ public class PdfPRow { * @param widths * @return true if everything went right */ - public boolean setWidths(float widths[]) { + public boolean setWidths(float widths[], TableProperties tablePropertie) { if (widths.length != cells.length) return false; System.arraycopy(widths, 0, this.widths, 0, cells.length); @@ -160,6 +160,19 @@ public class PdfPRow { for (; k < last; ++k) total += widths[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.setTop(0); processColWidth(cell.getColumn().getCompositeElements(), cell.getWidth()); diff --git a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/pdf/PdfPTable.java b/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/pdf/PdfPTable.java index 5112c6b3c..737b16303 100644 --- a/fine-itext/src/main/java/com/fr/third/v2/lowagie/text/pdf/PdfPTable.java +++ b/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; private TableProperties tableProperties; - private String[] firstColWidths; + private List firstLineColWidths; public TableProperties getTableProperties() { return tableProperties; @@ -118,8 +118,8 @@ public class PdfPTable implements LargeElement { this.tableProperties = tableProperties; } - public void setFirstColWidths(String[] firstColWidths) { - this.firstColWidths = firstColWidths; + public void setFirstLineColWidths(List firstLineColWidths) { + this.firstLineColWidths = firstLineColWidths; } /** @@ -389,7 +389,7 @@ public class PdfPTable implements LargeElement { private void adjustRowWidth() { for (int k = 0; k < rows.size(); ++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() { int numCols = getNumberOfColumns(); - float borderWidth = tableProperties.getBorderWidth(); - return tableProperties.getCellspacing() * (numCols + 1) + numCols * 2 * (borderWidth + tableProperties.getCellpadding()) + 2 * borderWidth; + float allCellspacingWidth = tableProperties.getCellspacing() * (numCols + 1); + 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; //相对宽度和 @@ -440,10 +442,11 @@ public class PdfPTable implements LargeElement { ArrayList relIndex = new ArrayList(); //初始化一下上述字段 - for (int i = 0; i < firstColWidths.length; i++) { - float w = CSSUtils.parseFloat(firstColWidths[i]); - if (null != firstColWidths[i]) { - if (firstColWidths[i].endsWith("%")) { + for (int i = 0; i < firstLineColWidths.size(); i++) { + String widthStr = firstLineColWidths.get(i); + float w = CSSUtils.parseFloat(widthStr); + if (null != widthStr) { + if (widthStr.endsWith("%")) { w = w / 100 * contentWidth; relWidthSum += w; relIndex.add(i); @@ -591,7 +594,7 @@ public class PdfPTable implements LargeElement { } float invalidColWidth = invalidColNum * tableProperties.getCellspacing() + 2 * invalidColNum * (tableProperties.getCellpadding() + tableProperties.getBorderWidth()); 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); absoluteWidths[index] += avgWidth; } @@ -604,7 +607,7 @@ public class PdfPTable implements LargeElement { * @return */ private boolean dealFirstLineHasNoWidth(float contentWidth) { - if (null != firstColWidths && 0 != firstColWidths.length) { + if (null != firstLineColWidths && 0 != firstLineColWidths.size()) { return false; } //第一行的td都没有设置width属性,则所有列平分table宽度 @@ -747,7 +750,7 @@ public class PdfPTable implements LargeElement { } PdfPRow row = new PdfPRow(currentRow); if (totalWidth > 0) { - row.setWidths(absoluteWidths); + row.setWidths(absoluteWidths, tableProperties); // totalHeight += row.getMaxHeights(); } rows.add(row); @@ -1114,7 +1117,7 @@ public class PdfPTable implements LargeElement { if (row == null) return 0; if (firsttime) - row.setWidths(absoluteWidths); + row.setWidths(absoluteWidths, tableProperties); float height = row.getMaxHeights(); PdfPCell cell; PdfPRow tmprow;