From e847825b7a93278f8cf65a21aefcb8a5a49d9ac9 Mon Sep 17 00:00:00 2001 From: "Hugh.C" Date: Tue, 24 Dec 2019 09:48:44 +0800 Subject: [PATCH] =?UTF-8?q?REPORT-25504=20=20=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E5=86=85=E5=AE=B9=E4=B8=8D=E6=8C=89=E7=85=A7?= =?UTF-8?q?=E5=88=97=E5=AE=BD=E8=BF=9B=E8=A1=8C=E6=8D=A2=E8=A1=8C=EF=BC=8C?= =?UTF-8?q?=E8=A1=8C=E9=AB=98=E9=80=BB=E8=BE=91=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../text/html/simpleparser/HTMLWorker.java | 14 +++-- .../text/html/simpleparser/IncTable.java | 54 ++++++++++++++-- .../fr/third/v2/lowagie/text/pdf/PdfPRow.java | 61 +++++++++++++++++++ 3 files changed, 121 insertions(+), 8 deletions(-) diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HTMLWorker.java b/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HTMLWorker.java index ade85f5cf..74a7a3af7 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HTMLWorker.java +++ b/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.StringTokenizer; +import com.fr.third.v2.lowagie.text.Cell; import com.fr.third.v2.lowagie.text.DocumentException; import com.fr.third.v2.lowagie.text.Element; 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.html.CSSUtils; 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.xml.simpleparser.SimpleXMLDocHandler; import com.fr.third.v2.lowagie.text.html.Markup; @@ -661,10 +664,13 @@ public class HTMLWorker implements SimpleXMLDocHandler, DocListener { cprops.removeChain("tr"); ArrayList cells = new ArrayList(); IncTable table = null; + float maxHeight = 0; while (true) { Object obj = stack.pop(); - if (obj instanceof IncCell) { - cells.add(((IncCell) obj).getCell()); + if (obj instanceof IncCell) { + PdfPCell cell = ((IncCell) obj).getCell(); + cells.add(cell); + maxHeight = Math.max(maxHeight, cell.getStyleHeight()); } if (obj instanceof IncTable) { table = (IncTable) obj; @@ -672,11 +678,11 @@ public class HTMLWorker implements SimpleXMLDocHandler, DocListener { } } float rowHeight = 0.0f; - if(rowHeightPx!=null){ + if (rowHeightPx != null) { rowHeight = CSSUtils.parseFloat(rowHeightPx); } table.addCols(cells); - table.endRow(rowHeight); + table.endRow(Math.max(rowHeight, maxHeight)); stack.push(table); skipText = true; diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/IncTable.java b/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/IncTable.java index 5da1eb4ca..9c99d6370 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/IncTable.java +++ b/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.Collections; 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.Markup; @@ -66,6 +67,7 @@ public class IncTable { private HashMap props = new HashMap(); private ArrayList rows = new ArrayList(); private ArrayList cols; + private List rowIndex4ZeroHeight = new ArrayList(); private ArrayList rowHeights = new ArrayList(); private ArrayList relativeColWidths = new ArrayList(); @@ -88,6 +90,9 @@ public class IncTable { } public void endRow(float rowHeight) { + if (rowHeight == 0) { + rowIndex4ZeroHeight.add(rowHeights.size()); + } rowHeights.add(rowHeight); if (cols != null) { Collections.reverse(cols); @@ -204,12 +209,53 @@ public class IncTable { } public void processRowHeight(PdfPTable table) { - Float height = CSSUtils.parseFloat((String) props.get("height")); - Float eachHeight = height / table.getRows().size(); + adjustRowHeight(CSSUtils.parseFloat((String) props.get("height"))); //调整行高 - for (int a = 0; a < rowHeights.size(); a++) { - table.getRow(a).setStyleHeight(Math.max(eachHeight, rowHeights.get(a))); + for (int i = 0; i < rowHeights.size(); i++) { + 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(){ diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPRow.java b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPRow.java index ed4a6b24a..466ee0df8 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPRow.java +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPRow.java @@ -50,12 +50,19 @@ package com.fr.third.v2.lowagie.text.pdf; 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.DocumentException; import com.fr.third.v2.lowagie.text.Element; 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.html.CSS; /** * A row in a PdfPTable. @@ -155,10 +162,64 @@ public class PdfPRow { --k; cell.setRight(total); cell.setTop(0); + processColWidth(cell.getColumn().getCompositeElements(), cell.getWidth()); } return true; } + /** + * @param list + * @param width + */ + private void processColWidth(List 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. * @since 2.1.6