diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/html/CSS.java b/fine-itext/src/com/fr/third/v2/lowagie/text/html/CSS.java index e9746a604..7b6f60330 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/html/CSS.java +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/html/CSS.java @@ -109,6 +109,7 @@ public class CSS{ public static final String BACKGROUND_ATTACHMENT = "background-attachment"; public static final String BACKGROUND_POSITION = "background-position"; public static final String BACKGROUND_COLOR = "background-color"; + public static final String BACKGROUND_SIZE = "background-size"; public static final String LIST_STYLE = "list-style"; public static final String LIST_STYLE_TYPE = "list-style-type"; public static final String LIST_STYLE_POSITION = "list-style-position"; diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/html/CSSUtils.java b/fine-itext/src/com/fr/third/v2/lowagie/text/html/CSSUtils.java index de09f2154..38747853f 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/html/CSSUtils.java +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/html/CSSUtils.java @@ -88,4 +88,18 @@ public class CSSUtils { } return str; } + + public static Float parseFloat(String str){ + float result = 0.0f; + try { + if(str.endsWith("px") || str.endsWith("pt")){ + result = Float.parseFloat(str.substring(0, str.length() - 2)); + }else{ + result = Float.parseFloat(str); + } + }catch (NumberFormatException e){ + + } + return result; + } } diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/html/SpaceWithPunctuationBreakIterator.java b/fine-itext/src/com/fr/third/v2/lowagie/text/html/SpaceWithPunctuationBreakIterator.java new file mode 100644 index 000000000..94322266b --- /dev/null +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/html/SpaceWithPunctuationBreakIterator.java @@ -0,0 +1,80 @@ +package com.fr.third.v2.lowagie.text.html; + +import com.fr.third.ibm.icu.text.BreakIterator; + +import java.text.CharacterIterator; + +/** + * 之前性能插件里面的考虑空格和标点符号的分词iterator + */ +public class SpaceWithPunctuationBreakIterator extends BreakIterator { + + private BreakIterator iterator; + private int currentPos = -1; + private int currentIndex = -1; + private boolean[] spaceIndex; + + public SpaceWithPunctuationBreakIterator(String text, BreakIterator iterator){ + this.iterator = iterator; + iterator.setText(text); + this.spaceIndex = new boolean[text.length()]; + int ilen = text.length() - 1; + if(ilen > 0) { + for (int i = 0; i < ilen; i++) { + char c = text.charAt(i); + spaceIndex[i + 1] = (c == ' ' && isPunctuation(text.charAt(i + 1)) )|| c == '-' || c == '\u2010' || c== '\n'; + } + } + } + + public boolean isPunctuation(char c) { + int code = Character.getType(c); + return code == 24 || code == 20 || code == 21 || code == 22 || code == 23; + } + + public int first() { + throw new UnsupportedOperationException(); + } + + public int last() { + throw new UnsupportedOperationException(); + } + + public int next(int n) { + throw new UnsupportedOperationException(); + } + + public int next() { + if(currentIndex == currentPos) { + currentPos = this.iterator.next(); + } + for(int i = currentIndex + 1; i < currentPos; i++){ + if(spaceIndex[i]){ + currentIndex = i; + return currentIndex; + } + } + currentIndex = currentPos; + return currentIndex; + } + + public int previous() { + throw new UnsupportedOperationException(); + } + + public int following(int offset) { + throw new UnsupportedOperationException(); + } + + public int current() { + throw new UnsupportedOperationException(); + } + + public CharacterIterator getText() { + throw new UnsupportedOperationException(); + } + + public void setText(CharacterIterator newText) { + throw new UnsupportedOperationException(); + } +} diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/ChainedProperties.java b/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/ChainedProperties.java index 4ec92005a..eb709e343 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/ChainedProperties.java +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/ChainedProperties.java @@ -76,6 +76,12 @@ public class ChainedProperties { return null; } + public String getLastChainProperty(String key){ + Object obj[] = (Object[]) chain.get(chain.size()-1); + HashMap prop = (HashMap) obj[1]; + return (String) prop.get(key); + } + public boolean hasProperty(String key) { for (int k = chain.size() - 1; k >= 0; --k) { Object obj[] = (Object[]) chain.get(k); 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 dd0fcf43e..51f01d31b 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 @@ -72,6 +72,7 @@ import com.fr.third.v2.lowagie.text.List; import com.fr.third.v2.lowagie.text.ListItem; 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.draw.LineSeparator; import com.fr.third.v2.lowagie.text.xml.simpleparser.SimpleXMLDocHandler; @@ -648,6 +649,8 @@ public class HTMLWorker implements SimpleXMLDocHandler, DocListener { if (pendingTD) endElement("td"); pendingTR = false; + String rowHeightPx = cprops.getLastChainProperty("height"); + cprops.removeChain("tr"); ArrayList cells = new ArrayList(); IncTable table = null; @@ -661,8 +664,13 @@ public class HTMLWorker implements SimpleXMLDocHandler, DocListener { break; } } + float rowHeight = 0.0f; + if(rowHeightPx!=null){ + rowHeight = CSSUtils.parseFloat(rowHeightPx); + } table.addCols(cells); - table.endRow(); + table.endRow(rowHeight); + stack.push(table); skipText = true; return; diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/IncCell.java b/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/IncCell.java index 1aff5f756..805433767 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/IncCell.java +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/IncCell.java @@ -48,10 +48,13 @@ package com.fr.third.v2.lowagie.text.html.simpleparser; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; import com.fr.third.v2.lowagie.text.Element; import com.fr.third.v2.lowagie.text.ElementListener; 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.Markup; import com.fr.third.v2.lowagie.text.pdf.PdfPCell; import com.fr.third.v2.lowagie.text.Phrase; @@ -95,14 +98,52 @@ public class IncCell implements TextElementArray { value = props.getProperty("border"); float border = 0; if (value != null) - border = Float.parseFloat(value); + border = CSSUtils.parseFloat(value); cell.setBorderWidth(border); value = props.getProperty("cellpadding"); if (value != null) - cell.setPadding(Float.parseFloat(value)); + cell.setPadding(CSSUtils.parseFloat(value)); cell.setUseDescender(true); value = props.getProperty("bgcolor"); cell.setBackgroundColor(Markup.decodeColor(value)); + //解析td上声明的width + value = props.getLastChainProperty("width"); + if(value != null){ + cell.setStyleWidth(CSSUtils.parseFloat(value)); + } + //解析td上声明的height + value = props.getLastChainProperty("height"); + if(value != null){ + cell.setStyleHeight(CSSUtils.parseFloat(value)); + } + //解析background相关属性并保存到cell对象 + Map backgroundRules = new HashMap(); + value = props.getLastChainProperty("background-size"); + if(value!=null){ + backgroundRules.put("background-size", value); + } + value = props.getLastChainProperty("background"); + if(value!=null){ + Map backgroundStyles = CSSUtils.processBackground(value); + backgroundRules.putAll(backgroundStyles); + } + value = props.getLastChainProperty("background-color"); + if(value!=null){ + backgroundRules.put("background-color", value); + } + value = props.getLastChainProperty("background-position"); + if(value!=null){ + backgroundRules.put("background-position", value); + } + value = props.getLastChainProperty("background-repeat"); + if(value!=null){ + backgroundRules.put("background-repeat", value); + } + value = props.getLastChainProperty("background-image"); + if(value!=null){ + backgroundRules.put("background-image", value); + } + cell.setBackground(backgroundRules); } public boolean add(Object o) { 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 8ddf9abbd..765664606 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 @@ -51,8 +51,12 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +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.pdf.BorderStyle; import com.fr.third.v2.lowagie.text.pdf.PdfPTable; import com.fr.third.v2.lowagie.text.pdf.PdfPCell; +import com.fr.third.v2.lowagie.text.pdf.TableProperties; /** * @@ -62,6 +66,8 @@ public class IncTable { private HashMap props = new HashMap(); private ArrayList rows = new ArrayList(); private ArrayList cols; + + private ArrayList rowHeights = new ArrayList(); /** Creates a new instance of IncTable */ public IncTable(HashMap props) { this.props.putAll(props); @@ -80,7 +86,8 @@ public class IncTable { cols.addAll(ncols); } - public void endRow() { + public void endRow(float rowHeight) { + rowHeights.add(rowHeight); if (cols != null) { Collections.reverse(cols); rows.add(cols); @@ -97,27 +104,88 @@ public class IncTable { return new PdfPTable(1); int ncol = 0; ArrayList c0 = (ArrayList)rows.get(0); + ArrayList colWidths = new ArrayList(); for (int k = 0; k < c0.size(); ++k) { - ncol += ((PdfPCell)c0.get(k)).getColspan(); + PdfPCell pCell = ((PdfPCell)c0.get(k)); + int cellCols = pCell.getColspan(); + ncol += cellCols; + if(cellCols > 1){ + for(int a = 0; a < cellCols ; a++){ + colWidths.add(pCell.getStyleWidth()/cellCols); + } + }else { + colWidths.add(pCell.getStyleWidth()); + } } + PdfPTable table = new PdfPTable(ncol); - String width = (String)props.get("width"); - if (width == null) - table.setWidthPercentage(100); - else { - if (width.endsWith("%")) - table.setWidthPercentage(Float.parseFloat(width.substring(0, width.length() - 1))); + try { + TableProperties tableProperties = parseTableProperties(); + table.setTableProperties(tableProperties); + String width = (String)props.get("width"); + if (width == null) + table.setWidthPercentage(100); else { - table.setTotalWidth(Float.parseFloat(width)); - table.setLockedWidth(true); + if (width.endsWith("%")) + table.setWidthPercentage(CSSUtils.parseFloat(width)); + else { + //解析单元格宽度 + float totalWidth = CSSUtils.parseFloat(width); + float[] floats = new float[colWidths.size()]; + for (int a = 0; a < colWidths.size(); a++){ + floats[a] = colWidths.get(a); + } + //解析px数值 + table.setTotalWidth(totalWidth); + try { + table.setWidths(floats); + }catch (Exception e){ + e.printStackTrace(); + } + table.setLockedWidth(true); + } } - } - for (int row = 0; row < rows.size(); ++row) { - ArrayList col = (ArrayList)rows.get(row); - for (int k = 0; k < col.size(); ++k) { - table.addCell((PdfPCell)col.get(k)); + for (int row = 0; row < rows.size(); ++row) { + ArrayList col = (ArrayList)rows.get(row); + for (int k = 0; k < col.size(); ++k) { + table.addCell((PdfPCell)col.get(k)); + } } + //调整行高 + for(int a = 0; a < rowHeights.size(); a++){ + table.getRow(a).setStyleHeight(rowHeights.get(a)); + } + + }catch (Exception e){ + e.printStackTrace(); } return table; } + + public TableProperties parseTableProperties(){ + TableProperties tableProperties = new TableProperties(); + BorderStyle borderStyle = new BorderStyle(); + tableProperties.setBorderStyle(borderStyle); + String value = (String)props.get("border"); + if(value != null){ + borderStyle.setBorderWidth(CSSUtils.parseFloat(value)); + } + value = (String)props.get("border-color"); + if(value != null){ + borderStyle.setBorderColor(Markup.decodeColor(value)); + } + value = (String)props.get("border-collapse"); + if(value != null){ + tableProperties.setCollapse(value.equals("collapse")); + } + value = (String)props.get("cellspacing"); + if(value != null){ + tableProperties.setCellspacing(CSSUtils.parseFloat(value)); + } + value = (String)props.get("cellpadding"); + if(value != null){ + tableProperties.setCellpadding(CSSUtils.parseFloat(value)); + } + return tableProperties; + } } diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/BorderStyle.java b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/BorderStyle.java new file mode 100644 index 000000000..9a03750f9 --- /dev/null +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/BorderStyle.java @@ -0,0 +1,58 @@ +package com.fr.third.v2.lowagie.text.pdf; + +import com.fr.third.v2.lowagie.text.DocumentException; +import com.fr.third.v2.lowagie.text.html.CSSUtils; +import com.fr.third.v2.lowagie.text.html.Markup; + +import java.awt.Color; + +public class BorderStyle { + private float borderWidth = 0.0f; + private Color borderColor = Color.black; + private String borderPattern = "solid"; + + public float getBorderWidth() { + return borderWidth; + } + + public void setBorderWidth(float borderWidth) { + this.borderWidth = borderWidth; + } + + public Color getBorderColor() { + return borderColor; + } + + public void setBorderColor(Color borderColor) { + this.borderColor = borderColor; + } + + public String getBorderPattern() { + return borderPattern; + } + + public void setBorderPattern(String borderPattern) { + this.borderPattern = borderPattern; + } + + public BorderStyle() { + + } + + public BorderStyle(int borderWidth, String borderPattern, Color borderColor) { + this.borderWidth = borderWidth; + this.borderPattern = borderPattern; + this.borderWidth = borderWidth; + } + + public static BorderStyle parseBorderStyle(String borderString) throws DocumentException { + BorderStyle borderStyle = new BorderStyle(); + String[] borderPros = borderString.split(" "); + borderStyle.setBorderWidth(CSSUtils.parseFloat(borderPros[0])); + borderStyle.setBorderPattern(borderPros[1]); + borderStyle.setBorderColor(Markup.decodeColor(borderPros[2])); + return borderStyle; + + } + +} diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfBlock.java b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfBlock.java deleted file mode 100644 index 38c5b440a..000000000 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfBlock.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.fr.third.v2.lowagie.text.pdf; - -/** - * @author kerry - * @date 2018/8/10 - */ -public interface PdfBlock { - float getHeight(); - void calculateHeight(); - - String toHtmlString(double startY, double endY); -} diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java index 99d0fa7a2..bd2c00394 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java @@ -53,8 +53,10 @@ import java.awt.Color; import java.awt.FontMetrics; import java.util.HashMap; import java.util.Iterator; +import java.util.Locale; import java.util.Map; +import com.fr.third.ibm.icu.text.BreakIterator; import com.fr.third.v2.lowagie.text.SplitCharacter; import com.fr.third.v2.lowagie.text.Utilities; import com.fr.third.v2.lowagie.text.Chunk; @@ -62,6 +64,7 @@ import com.fr.third.v2.lowagie.text.Font; import com.fr.third.v2.lowagie.text.Image; import com.fr.third.v2.lowagie.text.html.IndentAttribute; import com.fr.third.v2.lowagie.text.html.ParseIndentAttrUtils; +import com.fr.third.v2.lowagie.text.html.SpaceWithPunctuationBreakIterator; import sun.font.FontDesignMetrics; /** @@ -100,6 +103,17 @@ public class PdfChunk { private IndentAttribute indent = new IndentAttribute(); + public Map background ; + + public Map getBackground() { + return background; + } + + public void setBackground(Map background) { + this.background = background; + } + + static { keysAttributes.put(Chunk.ACTION, null); keysAttributes.put(Chunk.UNDERLINE, null); @@ -319,7 +333,7 @@ public class PdfChunk { * @param width a given width * @return the PdfChunk that doesn't fit into the width. */ - + PdfChunk split(float width) { newlineSplit = false; if (image != null) { @@ -330,88 +344,61 @@ public class PdfChunk { image = null; font = PdfFont.getDefaultFont(); return pc; - } - else + } else return null; } - HyphenationEvent hyphenationEvent = (HyphenationEvent)noStroke.get(Chunk.HYPHENATION); int currentPosition = 0; - int splitPosition = -1; + int start = 0; float currentWidth = indent.getLeft(); - + // loop over all the characters of a string // or until the totalWidth is reached - int lastSpace = -1; - float lastSpaceWidth = 0; int length = value.length(); char valueArray[] = value.toCharArray(); + BreakIterator iterator = BreakIterator.getLineInstance(Locale.getDefault()); + BreakIterator iterator1 = new SpaceWithPunctuationBreakIterator(value, iterator); char character = 0; - boolean surrogate = false; - while (currentPosition < length) { - // the width of every character is added to the currentWidth - character = valueArray[currentPosition]; - // if a newLine or carriageReturn is encountered - if (character == '\r' || character == '\n') { - newlineSplit = true; - int inc = 1; - if (character == '\r' && currentPosition + 1 < length && valueArray[currentPosition + 1] == '\n') - inc = 2; - String returnValue = value.substring(currentPosition + inc); - value = value.substring(0, currentPosition); - PdfChunk pc = new PdfChunk(returnValue, this); - return pc; - } - surrogate = Utilities.isSurrogatePair(valueArray, currentPosition); - if (surrogate) - currentWidth += font.width(Utilities.convertToUtf32(valueArray[currentPosition], valueArray[currentPosition + 1])); - else - currentWidth += font.width(character); - if (character == ' ') { - lastSpace = currentPosition + 1; - lastSpaceWidth = currentWidth; - } - if (surrogate) - currentPosition++; - if (currentWidth + indent.getRight()> width) - break; - // if a split-character is encountered, the splitPosition is altered - if (splitCharacter.isSplitCharacter(0, currentPosition, length, valueArray, null)) - splitPosition = currentPosition + 1; - currentPosition++; + while (currentPosition < length) { + int next = iterator1.next(); + if(next < 1){ + break; + } + currentPosition = next - 1; + // the width of every character is added to the currentWidth + character = valueArray[currentPosition]; + // if a newLine or carriageReturn is encountered + if (character == '\r' || character == '\n') { + newlineSplit = true; + int inc = 1; + if (character == '\r' && currentPosition + 1 < length && valueArray[currentPosition + 1] == '\n') + inc = 2; + String returnValue = value.substring(currentPosition + inc); + value = value.substring(0, currentPosition); + PdfChunk pc = new PdfChunk(returnValue, this); + return pc; + } + String substring = value.substring(start, next); + currentWidth += font.width(substring); + if (currentWidth + indent.getRight() > width){ + currentPosition = start - 1; + break; } + start = next; + + } + // if all the characters fit in the total width, null is returned (there is no overflow) - if (currentPosition == length) { + if (currentPosition == length-1) { return null; } // otherwise, the string has to be truncated - if (splitPosition < 0) { - String returnValue = value; - value = ""; - PdfChunk pc = new PdfChunk(returnValue, this); - return pc; - } - if (lastSpace > splitPosition && splitCharacter.isSplitCharacter(0, 0, 1, singleSpace, null)) - splitPosition = lastSpace; - if (hyphenationEvent != null && lastSpace >= 0 && lastSpace < currentPosition) { - int wordIdx = getWord(value, lastSpace); - if (wordIdx > lastSpace) { - String pre = hyphenationEvent.getHyphenatedWordPre(value.substring(lastSpace, wordIdx), font.getFont(), font.size(), width - lastSpaceWidth); - String post = hyphenationEvent.getHyphenatedWordPost(); - if (pre.length() > 0) { - String returnValue = post + value.substring(wordIdx); - value = trim(value.substring(0, lastSpace) + pre); - PdfChunk pc = new PdfChunk(returnValue, this); - return pc; - } - } - } - String returnValue = value.substring(splitPosition); - value = trim(value.substring(0, splitPosition)); + String returnValue = value.substring(start); + value = trim(value.substring(0, start)); PdfChunk pc = new PdfChunk(returnValue, this); return pc; } - + /** * Truncates this PdfChunk if it's too long for the given width. *

@@ -808,7 +795,7 @@ public class PdfChunk { } //todo 需要添加其他样式属性 htmlString.append(""); htmlString.append(value); htmlString.append(""); diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPCell.java b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPCell.java index 16ae6961c..102e4e9d7 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPCell.java +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPCell.java @@ -50,6 +50,7 @@ package com.fr.third.v2.lowagie.text.pdf; import java.util.List; +import java.util.Map; import com.fr.Container; import com.fr.third.v2.lowagie.text.Chunk; @@ -66,7 +67,37 @@ import com.fr.third.v2.lowagie.text.Phrase; */ public class PdfPCell extends Rectangle { - + + private float styleWidth = 0.0f; + private float styleHeight = 0.0f; + + public float getStyleHeight() { + return styleHeight; + } + + public void setStyleHeight(float styleHeight) { + this.styleHeight = styleHeight; + } + + public float getStyleWidth() { + return styleWidth; + } + + public Map background ; + + public Map getBackground() { + return background; + } + + public void setBackground(Map background) { + this.background = background; + } + + public void setStyleWidth(float styleWidth) { + this.styleWidth = styleWidth; + } + + private ColumnText column = new ColumnText(null); /** Vertical alignment of the cell. */ @@ -257,6 +288,9 @@ public class PdfPCell extends Rectangle { column = ColumnText.duplicate(cell.column); useBorderPadding = cell.useBorderPadding; rotation = cell.rotation; + background = cell.background; + styleWidth = cell.styleWidth; + styleHeight = cell.styleHeight; } /** 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 ca1740436..ed4a6b24a 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 @@ -75,7 +75,17 @@ public class PdfPRow { protected PdfPCell cells[]; protected float widths[]; - + + private float styleHeight; + + public float getStyleHeight() { + return styleHeight; + } + + public void setStyleHeight(float styleHeight) { + this.styleHeight = styleHeight; + } + /** * extra heights that needs to be added to a cell because of rowspans. * @since 2.1.6 @@ -186,31 +196,15 @@ public class PdfPRow { continue; } else { - height = cell.getMaxHeight(); + height = cell.getStyleHeight() == 0 ? cell.getContainer().getHeight() : cell.getStyleHeight(); if ((height > maxHeight) && (cell.getRowspan() == 1)) maxHeight = height; } } calculated = true; - return maxHeight; + return maxHeight = Math.max(styleHeight, maxHeight); } - public float getMaxHeight2(){ - float aMaxHeight = 0.0f; - for (int k = 0; k < cells.length; ++k) { - PdfPCell cell = cells[k]; - float height = 0; - if (cell == null) { - continue; - } - else { - height = cell.getContainer().getHeight(); - if ((height > aMaxHeight) && (cell.getRowspan() == 1)) - aMaxHeight = height; - } - } - return aMaxHeight; - } /** * Writes the border and background of one cell in the row. diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPTable.java b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPTable.java index cadf284f9..938e0e70b 100644 --- a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPTable.java +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPTable.java @@ -65,140 +65,152 @@ import com.fr.third.v2.lowagie.text.pdf.events.PdfPTableEventForwarder; * be added to the document as the class Table. * In the last case when crossing pages the table always break at full rows; if a * row is bigger than the page it is dropped silently to avoid infinite loops. - *

+ *

* A PdfPTableEvent can be associated to the table to do custom drawing * when the table is rendered. + * * @author Paulo Soares (psoares@consiste.pt) */ -public class PdfPTable implements LargeElement, PdfBlock { - +public class PdfPTable implements LargeElement { + /** * The index of the original PdfcontentByte. - */ + */ public static final int BASECANVAS = 0; - + /** * The index of the duplicate PdfContentByte where the background will be drawn. - */ + */ public static final int BACKGROUNDCANVAS = 1; - + /** * The index of the duplicate PdfContentByte where the border lines will be drawn. - */ + */ public static final int LINECANVAS = 2; - + /** * The index of the duplicate PdfContentByte where the text will be drawn. - */ + */ public static final int TEXTCANVAS = 3; - + protected ArrayList rows = new ArrayList(); protected float totalHeight = 0; protected PdfPCell currentRow[]; protected int currentRowIdx = 0; - protected PdfPCell defaultCell = new PdfPCell((Phrase)null); + protected PdfPCell defaultCell = new PdfPCell((Phrase) null); protected float totalWidth = 0; protected float relativeWidths[]; protected float absoluteWidths[]; protected PdfPTableEvent tableEvent; - + private TableProperties tableProperties; + + public TableProperties getTableProperties() { + return tableProperties; + } + + public void setTableProperties(TableProperties tableProperties) { + this.tableProperties = tableProperties; + } + /** * Holds value of property headerRows. */ protected int headerRows; - + /** * Holds value of property widthPercentage. */ protected float widthPercentage = 80; - + /** * Holds value of property horizontalAlignment. */ private int horizontalAlignment = Element.ALIGN_CENTER; - + /** * Holds value of property skipFirstHeader. */ private boolean skipFirstHeader = false; /** * Holds value of property skipLastFooter. - * @since 2.1.6 + * + * @since 2.1.6 */ private boolean skipLastFooter = false; protected boolean isColspan = false; - + protected int runDirection = PdfWriter.RUN_DIRECTION_DEFAULT; /** * Holds value of property lockedWidth. */ private boolean lockedWidth = false; - + /** * Holds value of property splitRows. */ private boolean splitRows = true; - + /** * The spacing before the table. */ protected float spacingBefore; - + /** * The spacing after the table. */ protected float spacingAfter; - + /** * Holds value of property extendLastRow. */ private boolean extendLastRow; - + /** * Holds value of property headersInEvent. */ private boolean headersInEvent; - + /** * Holds value of property splitLate. */ private boolean splitLate = true; - + /** * Defines if the table should be kept * on one page if possible */ private boolean keepTogether; - + /** * Indicates if the PdfPTable is complete once added to the document. - * - * @since iText 2.0.8 + * + * @since iText 2.0.8 */ protected boolean complete = true; - + /** * Holds value of property footerRows. */ private int footerRows; - + /** * Keeps track of the completeness of the current row. - * @since 2.1.6 + * + * @since 2.1.6 */ protected boolean rowCompleted = true; - + protected PdfPTable() { } - - /** + + /** * Constructs a PdfPTable with the relative column widths. - * + * * @param relativeWidths the relative column widths - */ + */ public PdfPTable(float relativeWidths[]) { if (relativeWidths == null) throw new NullPointerException("The widths array in PdfPTable constructor can not be null."); @@ -211,12 +223,12 @@ public class PdfPTable implements LargeElement, PdfBlock { currentRow = new PdfPCell[absoluteWidths.length]; keepTogether = false; } - - /** + + /** * Constructs a PdfPTable with numColumns columns. - * + * * @param numColumns the number of columns - */ + */ public PdfPTable(int numColumns) { if (numColumns <= 0) throw new IllegalArgumentException("The number of columns in PdfPTable constructor must be greater than zero."); @@ -228,12 +240,12 @@ public class PdfPTable implements LargeElement, PdfBlock { currentRow = new PdfPCell[absoluteWidths.length]; keepTogether = false; } - - /** + + /** * Constructs a copy of a PdfPTable. - * + * * @param table the PdfPTable to be copied - */ + */ public PdfPTable(PdfPTable table) { copyFormat(table); for (int k = 0; k < currentRow.length; ++k) { @@ -242,16 +254,16 @@ public class PdfPTable implements LargeElement, PdfBlock { currentRow[k] = new PdfPCell(table.currentRow[k]); } for (int k = 0; k < table.rows.size(); ++k) { - PdfPRow row = (PdfPRow)(table.rows.get(k)); + PdfPRow row = (PdfPRow) (table.rows.get(k)); if (row != null) row = new PdfPRow(row); rows.add(row); } } - + /** * Makes a shallow copy of a table (format without content). - * + * * @param table * @return a shallow copy of the table */ @@ -263,10 +275,10 @@ public class PdfPTable implements LargeElement, PdfBlock { /** * Copies the format of the sourceTable without copying the content. - * + * * @param sourceTable - * @since 2.1.6 private is now protected - */ + * @since 2.1.6 private is now protected + */ protected void copyFormat(PdfPTable sourceTable) { relativeWidths = new float[sourceTable.getNumberOfColumns()]; absoluteWidths = new float[sourceTable.getNumberOfColumns()]; @@ -295,15 +307,16 @@ public class PdfPTable implements LargeElement, PdfBlock { horizontalAlignment = sourceTable.horizontalAlignment; keepTogether = sourceTable.keepTogether; complete = sourceTable.complete; + tableProperties = sourceTable.tableProperties; } /** * Sets the relative widths of the table. - * + * * @param relativeWidths the relative widths of the table. * @throws DocumentException if the number of widths is different than the number - * of columns - */ + * of columns + */ public void setWidths(float relativeWidths[]) throws DocumentException { if (relativeWidths.length != getNumberOfColumns()) throw new DocumentException("Wrong number of columns."); @@ -312,16 +325,16 @@ public class PdfPTable implements LargeElement, PdfBlock { absoluteWidths = new float[relativeWidths.length]; totalHeight = 0; calculateWidths(); - calculateHeights(true); +// calculateHeights(true); } /** * Sets the relative widths of the table. - * + * * @param relativeWidths the relative widths of the table. * @throws DocumentException if the number of widths is different than the number - * of columns - */ + * of columns + */ public void setWidths(int relativeWidths[]) throws DocumentException { float tb[] = new float[relativeWidths.length]; for (int k = 0; k < relativeWidths.length; ++k) @@ -329,41 +342,43 @@ public class PdfPTable implements LargeElement, PdfBlock { setWidths(tb); } - /** - * @since 2.1.6 private is now protected - */ + /** + * @since 2.1.6 private is now protected + */ protected void calculateWidths() { if (totalWidth <= 0) return; float total = 0; int numCols = getNumberOfColumns(); + float cellSpacings = tableProperties.isCollapse() ? 0 : tableProperties.getCellspacing(); + float borderWidth = tableProperties.getBorderStyle().getBorderWidth(); for (int k = 0; k < numCols; ++k) total += relativeWidths[k]; for (int k = 0; k < numCols; ++k) - absoluteWidths[k] = totalWidth * relativeWidths[k] / total; + absoluteWidths[k] = (totalWidth-(cellSpacings+ borderWidth)*(numCols+1)) * relativeWidths[k] / total; } - + /** * Sets the full width of the table. - * + * * @param totalWidth the full width of the table. - */ + */ public void setTotalWidth(float totalWidth) { if (this.totalWidth == totalWidth) return; this.totalWidth = totalWidth; totalHeight = 0; calculateWidths(); - calculateHeights(true); +// calculateHeights(true); } /** * Sets the full width of the table from the absolute column width. - * + * * @param columnWidth the absolute width of each column * @throws DocumentException if the number of widths is different than the number - * of columns - */ + * of columns + */ public void setTotalWidth(float columnWidth[]) throws DocumentException { if (columnWidth.length != getNumberOfColumns()) throw new DocumentException("Wrong number of columns."); @@ -375,11 +390,11 @@ public class PdfPTable implements LargeElement, PdfBlock { /** * Sets the percentage width of the table from the absolute column width. - * + * * @param columnWidth the absolute width of each column - * @param pageSize the page size + * @param pageSize the page size * @throws DocumentException - */ + */ public void setWidthPercentage(float columnWidth[], Rectangle pageSize) throws DocumentException { if (columnWidth.length != getNumberOfColumns()) throw new IllegalArgumentException("Wrong number of columns."); @@ -392,61 +407,65 @@ public class PdfPTable implements LargeElement, PdfBlock { /** * Gets the full width of the table. - * + * * @return the full width of the table - */ + */ public float getTotalWidth() { return totalWidth; } - + /** * Calculates the heights of the table. - * - * @param firsttime if true, the heights of the rows will be recalculated. + * + * @param firsttime if true, the heights of the rows will be recalculated. * This takes time; normally the heights of the rows are already calcultated, * so in most cases, it's save to use false as parameter. - * @return the total height of the table. Note that it will be 0 if you didn't + * @return the total height of the table. Note that it will be 0 if you didn't * specify the width of the table with setTotalWidth(). - * @since 2.1.5 added a parameter and a return type to an existing method, + * @since 2.1.5 added a parameter and a return type to an existing method, * and made it public */ public float calculateHeights(boolean firsttime) { if (totalWidth <= 0) return 0; totalHeight = 0; + BorderStyle borderStyle = tableProperties.getBorderStyle(); + float borderWidth = borderStyle.getBorderWidth(); + float cellspacing = tableProperties.getCellspacing(); for (int k = 0; k < rows.size(); ++k) { - totalHeight += getRowHeight(k, firsttime); + totalHeight += getRowHeight(k, firsttime); } + totalHeight += (borderWidth + cellspacing) * (rows.size() + 1); return totalHeight; } - + /** * Calculates the heights of the table. */ public void calculateHeightsFast() { calculateHeights(false); } - + /** * Gets the default PdfPCell that will be used as * reference for all the addCell methods except * addCell(PdfPCell). - * + * * @return default PdfPCell - */ + */ public PdfPCell getDefaultCell() { return defaultCell; } - + /** * Adds a cell element. - * + * * @param cell the cell element - */ + */ public void addCell(PdfPCell cell) { - rowCompleted = false; + rowCompleted = false; PdfPCell ncell = new PdfPCell(cell); - + int colspan = ncell.getColspan(); colspan = Math.max(colspan, 1); colspan = Math.min(colspan, currentRow.length - currentRowIdx); @@ -457,20 +476,20 @@ public class PdfPTable implements LargeElement, PdfBlock { int rdir = ncell.getRunDirection(); if (rdir == PdfWriter.RUN_DIRECTION_DEFAULT) ncell.setRunDirection(runDirection); - + skipColsWithRowspanAbove(); - + boolean cellAdded = false; - if (currentRowIdx < currentRow.length) { - currentRow[currentRowIdx] = ncell; - currentRowIdx += colspan; - cellAdded = true; + if (currentRowIdx < currentRow.length) { + currentRow[currentRowIdx] = ncell; + currentRowIdx += colspan; + cellAdded = true; } skipColsWithRowspanAbove(); - + if (currentRowIdx >= currentRow.length) { - int numCols = getNumberOfColumns(); + int numCols = getNumberOfColumns(); if (runDirection == PdfWriter.RUN_DIRECTION_RTL) { PdfPCell rtlRow[] = new PdfPCell[numCols]; int rev = currentRow.length; @@ -486,201 +505,203 @@ public class PdfPTable implements LargeElement, PdfBlock { PdfPRow row = new PdfPRow(currentRow); if (totalWidth > 0) { row.setWidths(absoluteWidths); - totalHeight += row.getMaxHeights(); +// totalHeight += row.getMaxHeights(); } rows.add(row); currentRow = new PdfPCell[numCols]; currentRowIdx = 0; rowCompleted = true; } - + if (!cellAdded) { currentRow[currentRowIdx] = ncell; currentRowIdx += colspan; } } - + /** * When updating the row index, cells with rowspan should be taken into account. * This is what happens in this method. - * @since 2.1.6 + * + * @since 2.1.6 */ private void skipColsWithRowspanAbove() { - int direction = 1; - if (runDirection == PdfWriter.RUN_DIRECTION_RTL) - direction = -1; - while (rowSpanAbove(rows.size(), currentRowIdx)) - currentRowIdx += direction; + int direction = 1; + if (runDirection == PdfWriter.RUN_DIRECTION_RTL) + direction = -1; + while (rowSpanAbove(rows.size(), currentRowIdx)) + currentRowIdx += direction; } - + /** * Checks if there are rows above belonging to a rowspan. - * @param currRow the current row to check - * @param currCol the current column to check - * @return true if there's a cell above that belongs to a rowspan - * @since 2.1.6 + * + * @param currRow the current row to check + * @param currCol the current column to check + * @return true if there's a cell above that belongs to a rowspan + * @since 2.1.6 */ boolean rowSpanAbove(int currRow, int currCol) { - - if ((currCol >= getNumberOfColumns()) - || (currCol < 0) - || (currRow == 0)) - return false; - - int row = currRow - 1; - PdfPRow aboveRow = (PdfPRow)rows.get(row); - if (aboveRow == null) - return false; - PdfPCell aboveCell = (PdfPCell)aboveRow.getCells()[currCol]; - while ((aboveCell == null) && (row > 0)) { - aboveRow = (PdfPRow)rows.get(--row); - if (aboveRow == null) - return false; - aboveCell = (PdfPCell)aboveRow.getCells()[currCol]; - } - - int distance = currRow - row; - - if (aboveCell == null) { - int col = currCol - 1; - aboveCell = (PdfPCell)aboveRow.getCells()[col]; - while ((aboveCell == null) && (row > 0)) - aboveCell = (PdfPCell)aboveRow.getCells()[--col]; - return aboveCell != null && aboveCell.getRowspan() > distance; - } - - if ((aboveCell.getRowspan() == 1) && (distance > 1)) { - int col = currCol - 1; - aboveRow = (PdfPRow)rows.get(row + 1); - distance--; - aboveCell = (PdfPCell)aboveRow.getCells()[col]; - while ((aboveCell == null) && (col > 0)) - aboveCell = (PdfPCell)aboveRow.getCells()[--col]; - } - - return aboveCell != null && aboveCell.getRowspan() > distance; - } - - + + if ((currCol >= getNumberOfColumns()) + || (currCol < 0) + || (currRow == 0)) + return false; + + int row = currRow - 1; + PdfPRow aboveRow = (PdfPRow) rows.get(row); + if (aboveRow == null) + return false; + PdfPCell aboveCell = (PdfPCell) aboveRow.getCells()[currCol]; + while ((aboveCell == null) && (row > 0)) { + aboveRow = (PdfPRow) rows.get(--row); + if (aboveRow == null) + return false; + aboveCell = (PdfPCell) aboveRow.getCells()[currCol]; + } + + int distance = currRow - row; + + if (aboveCell == null) { + int col = currCol - 1; + aboveCell = (PdfPCell) aboveRow.getCells()[col]; + while ((aboveCell == null) && (row > 0)) + aboveCell = (PdfPCell) aboveRow.getCells()[--col]; + return aboveCell != null && aboveCell.getRowspan() > distance; + } + + if ((aboveCell.getRowspan() == 1) && (distance > 1)) { + int col = currCol - 1; + aboveRow = (PdfPRow) rows.get(row + 1); + distance--; + aboveCell = (PdfPCell) aboveRow.getCells()[col]; + while ((aboveCell == null) && (col > 0)) + aboveCell = (PdfPCell) aboveRow.getCells()[--col]; + } + + return aboveCell != null && aboveCell.getRowspan() > distance; + } + + /** * Adds a cell element. - * + * * @param text the text for the cell - */ + */ public void addCell(String text) { addCell(new Phrase(text)); } - + /** * Adds a nested table. - * + * * @param table the table to be added to the cell - */ + */ public void addCell(PdfPTable table) { defaultCell.setTable(table); addCell(defaultCell); defaultCell.setTable(null); } - + /** * Adds an Image as Cell. - * + * * @param image the Image to add to the table. - * This image will fit in the cell - */ + * This image will fit in the cell + */ public void addCell(Image image) { defaultCell.setImage(image); addCell(defaultCell); defaultCell.setImage(null); } - + /** * Adds a cell element. - * + * * @param phrase the Phrase to be added to the cell - */ + */ public void addCell(Phrase phrase) { defaultCell.setPhrase(phrase); addCell(defaultCell); defaultCell.setPhrase(null); } - + /** * Writes the selected rows to the document. * canvases is obtained from beginWritingRows(). - * + * * @param rowStart the first row to be written, zero index - * @param rowEnd the last row to be written + 1. If it is -1 all the - * rows to the end are written - * @param xPos the x write coordinate - * @param yPos the y write coordinate + * @param rowEnd the last row to be written + 1. If it is -1 all the + * rows to the end are written + * @param xPos the x write coordinate + * @param yPos the y write coordinate * @param canvases an array of 4 PdfContentByte obtained from - * beginWrittingRows() + * beginWrittingRows() * @return the y coordinate position of the bottom of the last row * @see #beginWritingRows(PdfContentByte) - */ + */ public float writeSelectedRows(int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte[] canvases) { return writeSelectedRows(0, -1, rowStart, rowEnd, xPos, yPos, canvases); } - + /** * Writes the selected rows and columns to the document. * This method does not clip the columns; this is only important * if there are columns with colspan at boundaries. * canvases is obtained from beginWritingRows(). * The table event is only fired for complete rows. - * + * * @param colStart the first column to be written, zero index - * @param colEnd the last column to be written + 1. If it is -1 all the - * columns to the end are written + * @param colEnd the last column to be written + 1. If it is -1 all the + * columns to the end are written * @param rowStart the first row to be written, zero index - * @param rowEnd the last row to be written + 1. If it is -1 all the - * rows to the end are written - * @param xPos the x write coordinate - * @param yPos the y write coordinate + * @param rowEnd the last row to be written + 1. If it is -1 all the + * rows to the end are written + * @param xPos the x write coordinate + * @param yPos the y write coordinate * @param canvases an array of 4 PdfContentByte obtained from - * beginWritingRows() + * beginWritingRows() * @return the y coordinate position of the bottom of the last row * @see #beginWritingRows(PdfContentByte) - */ + */ public float writeSelectedRows(int colStart, int colEnd, int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte[] canvases) { if (totalWidth <= 0) throw new RuntimeException("The table width must be greater than zero."); - + int totalRows = rows.size(); if (rowStart < 0) rowStart = 0; if (rowEnd < 0) rowEnd = totalRows; else - rowEnd = Math.min(rowEnd, totalRows); + rowEnd = Math.min(rowEnd, totalRows); if (rowStart >= rowEnd) return yPos; - + int totalCols = getNumberOfColumns(); if (colStart < 0) colStart = 0; else - colStart = Math.min(colStart, totalCols); + colStart = Math.min(colStart, totalCols); if (colEnd < 0) colEnd = totalCols; else - colEnd = Math.min(colEnd, totalCols); - + colEnd = Math.min(colEnd, totalCols); + float yPosStart = yPos; for (int k = rowStart; k < rowEnd; ++k) { - PdfPRow row = (PdfPRow)rows.get(k); + PdfPRow row = (PdfPRow) rows.get(k); if (row != null) { row.writeCells(colStart, colEnd, xPos, yPos, canvases); yPos -= row.getMaxHeights(); } } - + if (tableEvent != null && colStart == 0 && colEnd == totalCols) { float heights[] = new float[rowEnd - rowStart + 1]; heights[0] = yPosStart; for (int k = rowStart; k < rowEnd; ++k) { - PdfPRow row = (PdfPRow)rows.get(k); + PdfPRow row = (PdfPRow) rows.get(k); float hr = 0; if (row != null) hr = row.getMaxHeights(); @@ -688,58 +709,58 @@ public class PdfPTable implements LargeElement, PdfBlock { } tableEvent.tableLayout(this, getEventWidths(xPos, rowStart, rowEnd, headersInEvent), heights, headersInEvent ? headerRows : 0, rowStart, canvases); } - + return yPos; } - + /** * Writes the selected rows to the document. - * + * * @param rowStart the first row to be written, zero index - * @param rowEnd the last row to be written + 1. If it is -1 all the - * rows to the end are written - * @param xPos the x write coordinate - * @param yPos the y write coordinate - * @param canvas the PdfContentByte where the rows will - * be written to + * @param rowEnd the last row to be written + 1. If it is -1 all the + * rows to the end are written + * @param xPos the x write coordinate + * @param yPos the y write coordinate + * @param canvas the PdfContentByte where the rows will + * be written to * @return the y coordinate position of the bottom of the last row - */ + */ public float writeSelectedRows(int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte canvas) { return writeSelectedRows(0, -1, rowStart, rowEnd, xPos, yPos, canvas); } - + /** * Writes the selected rows and columns to the document. * This method clips the columns; this is only important * if there are columns with colspan at boundaries. * The table event is only fired for complete rows. - * + * * @param colStart the first column to be written, zero index - * @param colEnd the last column to be written + 1. If it is -1 all the - * columns to the end are written + * @param colEnd the last column to be written + 1. If it is -1 all the + * columns to the end are written * @param rowStart the first row to be written, zero index - * @param rowEnd the last row to be written + 1. If it is -1 all the - * rows to the end are written - * @param xPos the x write coordinate - * @param yPos the y write coordinate - * @param canvas the PdfContentByte where the rows will - * be written to + * @param rowEnd the last row to be written + 1. If it is -1 all the + * rows to the end are written + * @param xPos the x write coordinate + * @param yPos the y write coordinate + * @param canvas the PdfContentByte where the rows will + * be written to * @return the y coordinate position of the bottom of the last row - */ + */ public float writeSelectedRows(int colStart, int colEnd, int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte canvas) { int totalCols = getNumberOfColumns(); if (colStart < 0) colStart = 0; else - colStart = Math.min(colStart, totalCols); - - if (colEnd < 0) + colStart = Math.min(colStart, totalCols); + + if (colEnd < 0) colEnd = totalCols; - else - colEnd = Math.min(colEnd, totalCols); - - boolean clip = (colStart != 0 || colEnd != totalCols); - + else + colEnd = Math.min(colEnd, totalCols); + + boolean clip = (colStart != 0 || colEnd != totalCols); + if (clip) { float w = 0; for (int k = colStart; k < colEnd; ++k) @@ -751,17 +772,17 @@ public class PdfPTable implements LargeElement, PdfBlock { canvas.clip(); canvas.newPath(); } - + PdfContentByte[] canvases = beginWritingRows(canvas); float y = writeSelectedRows(colStart, colEnd, rowStart, rowEnd, xPos, yPos, canvases); endWritingRows(canvases); - + if (clip) canvas.restoreState(); - + return y; } - + /** * Gets and initializes the 4 layers where the table is written to. The text or graphics are added to * one of the 4 PdfContentByte returned with the following order:

@@ -774,26 +795,26 @@ public class PdfPTable implements LargeElement, PdfBlock { * will be over the table. *

* The layers are placed in sequence on top of each other. - * + * * @param canvas the PdfContentByte where the rows will - * be written to + * be written to * @return an array of 4 PdfContentByte * @see #writeSelectedRows(int, int, float, float, PdfContentByte[]) - */ + */ public static PdfContentByte[] beginWritingRows(PdfContentByte canvas) { return new PdfContentByte[]{ - canvas, - canvas.getDuplicate(), - canvas.getDuplicate(), - canvas.getDuplicate(), + canvas, + canvas.getDuplicate(), + canvas.getDuplicate(), + canvas.getDuplicate(), }; } - + /** * Finishes writing the table. - * + * * @param canvases the array returned by beginWritingRows() - */ + */ public static void endWritingRows(PdfContentByte[] canvases) { PdfContentByte canvas = canvases[BASECANVAS]; canvas.saveState(); @@ -806,170 +827,171 @@ public class PdfPTable implements LargeElement, PdfBlock { canvas.restoreState(); canvas.add(canvases[TEXTCANVAS]); } - + /** * Gets the number of rows in this table. - * + * * @return the number of rows in this table - */ + */ public int size() { return rows.size(); } - + /** * Gets the total height of the table. - * + * * @return the total height of the table - */ + */ public float getTotalHeight() { return totalHeight; } /** * Gets the height of a particular row. - * + * * @param idx the row index (starts at 0) * @return the height of a particular row - */ + */ public float getRowHeight(int idx) { - return getRowHeight(idx, false); + return getRowHeight(idx, false); } + /** * Gets the height of a particular row. - * - * @param idx the row index (starts at 0) - * @param firsttime is this the first time the row heigh is calculated? + * + * @param idx the row index (starts at 0) + * @param firsttime is this the first time the row heigh is calculated? * @return the height of a particular row - * @since 3.0.0 - */ + * @since 3.0.0 + */ public float getRowHeight(int idx, boolean firsttime) { if (totalWidth <= 0 || idx < 0 || idx >= rows.size()) return 0; - PdfPRow row = (PdfPRow)rows.get(idx); + PdfPRow row = (PdfPRow) rows.get(idx); if (row == null) return 0; if (firsttime) - row.setWidths(absoluteWidths); + row.setWidths(absoluteWidths); float height = row.getMaxHeights(); PdfPCell cell; PdfPRow tmprow; for (int i = 0; i < relativeWidths.length; i++) { - if(!rowSpanAbove(idx, i)) - continue; - int rs = 1; - while (rowSpanAbove(idx - rs, i)) { - rs++; - } - tmprow = (PdfPRow)rows.get(idx - rs); - cell = tmprow.getCells()[i]; - float tmp = 0; - if (cell.getRowspan() == rs + 1) { - tmp = cell.getMaxHeight(); - while (rs > 0) { - tmp -= getRowHeight(idx - rs); - rs--; - } - } - if (tmp > height) - height = tmp; + if (!rowSpanAbove(idx, i)) + continue; + int rs = 1; + while (rowSpanAbove(idx - rs, i)) { + rs++; + } + tmprow = (PdfPRow) rows.get(idx - rs); + cell = tmprow.getCells()[i]; + float tmp = 0; + if (cell.getRowspan() == rs + 1) { + tmp = cell.getMaxHeight(); + while (rs > 0) { + tmp -= getRowHeight(idx - rs); + rs--; + } + } + if (tmp > height) + height = tmp; } row.setMaxHeights(height); return height; } - + /** * Gets the maximum height of a cell in a particular row (will only be different * from getRowHeight is one of the cells in the row has a rowspan > 1). - * - * @param rowIndex the row index - * @param cellIndex the cell index + * * @return the height of a particular row including rowspan - * @since 2.1.6 - */ + * @param rowIndex the row index + * @param cellIndex the cell index + * @since 2.1.6 + */ public float getRowspanHeight(int rowIndex, int cellIndex) { if (totalWidth <= 0 || rowIndex < 0 || rowIndex >= rows.size()) return 0; - PdfPRow row = (PdfPRow)rows.get(rowIndex); + PdfPRow row = (PdfPRow) rows.get(rowIndex); if (row == null || cellIndex >= row.getCells().length) return 0; PdfPCell cell = row.getCells()[cellIndex]; if (cell == null) - return 0; + return 0; float rowspanHeight = 0; for (int j = 0; j < cell.getRowspan(); j++) { - rowspanHeight += getRowHeight(rowIndex + j); + rowspanHeight += getRowHeight(rowIndex + j); } return rowspanHeight; } - + /** * Gets the height of the rows that constitute the header as defined by * setHeaderRows(). - * + * * @return the height of the rows that constitute the header and footer - */ + */ public float getHeaderHeight() { float total = 0; int size = Math.min(rows.size(), headerRows); for (int k = 0; k < size; ++k) { - PdfPRow row = (PdfPRow)rows.get(k); + PdfPRow row = (PdfPRow) rows.get(k); if (row != null) total += row.getMaxHeights(); } return total; } - + /** * Gets the height of the rows that constitute the footer as defined by * setFooterRows(). - * + * * @return the height of the rows that constitute the footer * @since 2.1.1 - */ + */ public float getFooterHeight() { float total = 0; int start = Math.max(0, headerRows - footerRows); int size = Math.min(rows.size(), headerRows); for (int k = start; k < size; ++k) { - PdfPRow row = (PdfPRow)rows.get(k); + PdfPRow row = (PdfPRow) rows.get(k); if (row != null) total += row.getMaxHeights(); } return total; } - + /** * Deletes a row from the table. - * + * * @param rowNumber the row to be deleted * @return true if the row was deleted - */ + */ public boolean deleteRow(int rowNumber) { if (rowNumber < 0 || rowNumber >= rows.size()) return false; if (totalWidth > 0) { - PdfPRow row = (PdfPRow)rows.get(rowNumber); + PdfPRow row = (PdfPRow) rows.get(rowNumber); if (row != null) totalHeight -= row.getMaxHeights(); } rows.remove(rowNumber); if (rowNumber < headerRows) { - --headerRows; - if (rowNumber >= (headerRows - footerRows)) - --footerRows; + --headerRows; + if (rowNumber >= (headerRows - footerRows)) + --footerRows; } return true; } - + /** * Deletes the last row in the table. - * + * * @return true if the last row was deleted - */ + */ public boolean deleteLastRow() { return deleteRow(rows.size() - 1); } - + /** * Removes all of the rows except headers */ @@ -982,31 +1004,31 @@ public class PdfPTable implements LargeElement, PdfBlock { if (totalWidth > 0) totalHeight = getHeaderHeight(); } - + /** * Returns the number of columns. - * - * @return the number of columns. - * @since 2.1.1 + * + * @return the number of columns. + * @since 2.1.1 */ public int getNumberOfColumns() { - return relativeWidths.length; + return relativeWidths.length; } /** * Gets the number of the rows that constitute the header. - * + * * @return the number of the rows that constitute the header */ public int getHeaderRows() { return headerRows; } - + /** * Sets the number of the top rows that constitute the header. * This header has only meaning if the table is added to Document * and the table crosses pages. - * + * * @param headerRows the number of the top rows that constitute the header */ public void setHeaderRows(int headerRows) { @@ -1014,66 +1036,65 @@ public class PdfPTable implements LargeElement, PdfBlock { headerRows = 0; this.headerRows = headerRows; } - + /** * Gets all the chunks in this element. * - * @return an ArrayList + * @return an ArrayList */ public ArrayList getChunks() { return new ArrayList(); } - + /** * Gets the type of the text element. * - * @return a type + * @return a type */ public int type() { return Element.PTABLE; } - - /** - * @see Element#isContent() - * @since iText 2.0.8 - */ - public boolean isContent() { - return true; - } - - /** - * @see Element#isNestable() - * @since iText 2.0.8 - */ - public boolean isNestable() { - return true; - } - + + /** + * @since iText 2.0.8 + * @see Element#isContent() + */ + public boolean isContent() { + return true; + } + + /** + * @since iText 2.0.8 + * @see Element#isNestable() + */ + public boolean isNestable() { + return true; + } + /** * Processes the element by adding it (or the different parts) to an * ElementListener. * - * @param listener an ElementListener - * @return true if the element was processed successfully + * @param listener an ElementListener + * @return true if the element was processed successfully */ public boolean process(ElementListener listener) { try { return listener.add(this); - } - catch(DocumentException de) { + } catch (DocumentException de) { return false; } } - + /** * Gets the width percentage that the table will occupy in the page. - * + * * @return the width percentage that the table will occupy in the page */ public float getWidthPercentage() { return widthPercentage; } - + /** * Sets the width percentage that the table will occupy in the page. * @@ -1082,157 +1103,161 @@ public class PdfPTable implements LargeElement, PdfBlock { public void setWidthPercentage(float widthPercentage) { this.widthPercentage = widthPercentage; } - + /** * Gets the horizontal alignment of the table relative to the page. - * + * * @return the horizontal alignment of the table relative to the page */ public int getHorizontalAlignment() { return horizontalAlignment; } - + /** * Sets the horizontal alignment of the table relative to the page. * It only has meaning if the width percentage is less than 100%. * * @param horizontalAlignment the horizontal alignment of the table - * relative to the page + * relative to the page */ public void setHorizontalAlignment(int horizontalAlignment) { this.horizontalAlignment = horizontalAlignment; } - + /** * Gets a row with a given index * (added by Jin-Hsia Yang). - * + * * @param idx * @return the row at position idx */ public PdfPRow getRow(int idx) { - return (PdfPRow)rows.get(idx); + return (PdfPRow) rows.get(idx); } /** * Gets an arraylist with all the rows in the table. - * + * * @return an arraylist */ public ArrayList getRows() { return rows; } - + /** * Gets an arraylist with a selection of rows. - * @param start the first row in the selection - * @param end the first row that isn't part of the selection - * @return a selection of rows - * @since 2.1.6 + * + * @param start the first row in the selection + * @param end the first row that isn't part of the selection + * @return a selection of rows + * @since 2.1.6 */ public ArrayList getRows(int start, int end) { - ArrayList list = new ArrayList(); - if (start < 0 || end > size()) { - return list; - } - PdfPRow firstRow = adjustCellsInRow(start, end); - int colIndex = 0; - PdfPCell cell; - while (colIndex < getNumberOfColumns()) { - int rowIndex = start; - while (rowSpanAbove(rowIndex--, colIndex)) { - PdfPRow row = getRow(rowIndex); - if (row != null) { - PdfPCell replaceCell = row.getCells()[colIndex]; - if (replaceCell != null) { - firstRow.getCells()[colIndex] = new PdfPCell(replaceCell); - float extra = 0; - int stop = Math.min(rowIndex + replaceCell.getRowspan(), end); - for (int j = start + 1; j < stop; j++) { - extra += getRowHeight(j); - } - firstRow.setExtraHeight(colIndex, extra); - float diff = getRowspanHeight(rowIndex, colIndex) - - getRowHeight(start) - extra; - firstRow.getCells()[colIndex].consumeHeight(diff); - } - } - } - cell = firstRow.getCells()[colIndex]; - if (cell == null) - colIndex++; - else - colIndex += cell.getColspan(); - } - list.add(firstRow); - for (int i = start + 1; i < end; i++) { - list.add(adjustCellsInRow(i, end)); - } - return list; - } - + ArrayList list = new ArrayList(); + if (start < 0 || end > size()) { + return list; + } + PdfPRow firstRow = adjustCellsInRow(start, end); + int colIndex = 0; + PdfPCell cell; + while (colIndex < getNumberOfColumns()) { + int rowIndex = start; + while (rowSpanAbove(rowIndex--, colIndex)) { + PdfPRow row = getRow(rowIndex); + if (row != null) { + PdfPCell replaceCell = row.getCells()[colIndex]; + if (replaceCell != null) { + firstRow.getCells()[colIndex] = new PdfPCell(replaceCell); + float extra = 0; + int stop = Math.min(rowIndex + replaceCell.getRowspan(), end); + for (int j = start + 1; j < stop; j++) { + extra += getRowHeight(j); + } + firstRow.setExtraHeight(colIndex, extra); + float diff = getRowspanHeight(rowIndex, colIndex) + - getRowHeight(start) - extra; + firstRow.getCells()[colIndex].consumeHeight(diff); + } + } + } + cell = firstRow.getCells()[colIndex]; + if (cell == null) + colIndex++; + else + colIndex += cell.getColspan(); + } + list.add(firstRow); + for (int i = start + 1; i < end; i++) { + list.add(adjustCellsInRow(i, end)); + } + return list; + } + /** * Calculates the extra height needed in a row because of rowspans. - * @param start the index of the start row (the one to adjust) - * @param end the index of the end row on the page - * @since 2.1.6 + * + * @param start the index of the start row (the one to adjust) + * @param end the index of the end row on the page + * @since 2.1.6 */ protected PdfPRow adjustCellsInRow(int start, int end) { - PdfPRow row = new PdfPRow(getRow(start)); - row.initExtraHeights(); - PdfPCell cell; - PdfPCell[] cells = row.getCells(); - for (int i = 0; i < cells.length; i++) { - cell = cells[i]; - if (cell == null || cell.getRowspan() == 1) - continue; - int stop = Math.min(end, start + cell.getRowspan()); - float extra = 0; - for (int k = start + 1; k < stop; k++) { - extra += getRowHeight(k); - } - row.setExtraHeight(i, extra); - } - return row; - } - - /** Sets the table event for this table. + PdfPRow row = new PdfPRow(getRow(start)); + row.initExtraHeights(); + PdfPCell cell; + PdfPCell[] cells = row.getCells(); + for (int i = 0; i < cells.length; i++) { + cell = cells[i]; + if (cell == null || cell.getRowspan() == 1) + continue; + int stop = Math.min(end, start + cell.getRowspan()); + float extra = 0; + for (int k = start + 1; k < stop; k++) { + extra += getRowHeight(k); + } + row.setExtraHeight(i, extra); + } + return row; + } + + /** + * Sets the table event for this table. + * * @param event the table event for this table - */ + */ public void setTableEvent(PdfPTableEvent event) { - if (event == null) - this.tableEvent = null; - else if (this.tableEvent == null) - this.tableEvent = event; - else if (this.tableEvent instanceof PdfPTableEventForwarder) - ((PdfPTableEventForwarder)this.tableEvent).addTableEvent(event); - else { - PdfPTableEventForwarder forward = new PdfPTableEventForwarder(); - forward.addTableEvent(this.tableEvent); - forward.addTableEvent(event); - this.tableEvent = forward; - } - } - + if (event == null) + this.tableEvent = null; + else if (this.tableEvent == null) + this.tableEvent = event; + else if (this.tableEvent instanceof PdfPTableEventForwarder) + ((PdfPTableEventForwarder) this.tableEvent).addTableEvent(event); + else { + PdfPTableEventForwarder forward = new PdfPTableEventForwarder(); + forward.addTableEvent(this.tableEvent); + forward.addTableEvent(event); + this.tableEvent = forward; + } + } + /** * Gets the table event for this page. - * + * * @return the table event for this page - */ + */ public PdfPTableEvent getTableEvent() { return tableEvent; } - + /** * Gets the absolute sizes of each column width. - * + * * @return he absolute sizes of each column width - */ + */ public float[] getAbsoluteWidths() { return absoluteWidths; } - - float [][] getEventWidths(float xPos, int firstRow, int lastRow, boolean includeHeaders) { + + float[][] getEventWidths(float xPos, int firstRow, int lastRow, boolean includeHeaders) { if (includeHeaders) { firstRow = Math.max(firstRow, headerRows); lastRow = Math.max(lastRow, headerRows); @@ -1242,7 +1267,7 @@ public class PdfPTable implements LargeElement, PdfBlock { int n = 0; if (includeHeaders) { for (int k = 0; k < headerRows; ++k) { - PdfPRow row = (PdfPRow)rows.get(k); + PdfPRow row = (PdfPRow) rows.get(k); if (row == null) ++n; else @@ -1250,15 +1275,14 @@ public class PdfPTable implements LargeElement, PdfBlock { } } for (; firstRow < lastRow; ++firstRow) { - PdfPRow row = (PdfPRow)rows.get(firstRow); - if (row == null) - ++n; - else - widths[n++] = row.getEventWidth(xPos); + PdfPRow row = (PdfPRow) rows.get(firstRow); + if (row == null) + ++n; + else + widths[n++] = row.getEventWidth(xPos); } - } - else { - int numCols = getNumberOfColumns(); + } else { + int numCols = getNumberOfColumns(); float width[] = new float[numCols + 1]; width[0] = xPos; for (int k = 0; k < numCols; ++k) @@ -1273,7 +1297,7 @@ public class PdfPTable implements LargeElement, PdfBlock { /** * Tells you if the first header needs to be skipped * (for instance if the header says "continued from the previous page"). - * + * * @return Value of property skipFirstHeader. */ public boolean isSkipFirstHeader() { @@ -1284,30 +1308,30 @@ public class PdfPTable implements LargeElement, PdfBlock { /** * Tells you if the last footer needs to be skipped * (for instance if the footer says "continued on the next page") - * + * * @return Value of property skipLastFooter. - * @since 2.1.6 + * @since 2.1.6 */ public boolean isSkipLastFooter() { return skipLastFooter; } - + /** * Skips the printing of the first header. Used when printing * tables in succession belonging to the same printed table aspect. - * + * * @param skipFirstHeader New value of property skipFirstHeader. */ public void setSkipFirstHeader(boolean skipFirstHeader) { this.skipFirstHeader = skipFirstHeader; } - + /** * Skips the printing of the last footer. Used when printing * tables in succession belonging to the same printed table aspect. - * + * * @param skipLastFooter New value of property skipLastFooter. - * @since 2.1.6 + * @since 2.1.6 */ public void setSkipLastFooter(boolean skipLastFooter) { this.skipLastFooter = skipLastFooter; @@ -1315,27 +1339,27 @@ public class PdfPTable implements LargeElement, PdfBlock { /** * Sets the run direction of the contents of the table. - * + * * @param runDirection One of the following values: - * PdfWriter.RUN_DIRECTION_DEFAULT, PdfWriter.RUN_DIRECTION_NO_BIDI, - * PdfWriter.RUN_DIRECTION_LTR or PdfWriter.RUN_DIRECTION_RTL. + * PdfWriter.RUN_DIRECTION_DEFAULT, PdfWriter.RUN_DIRECTION_NO_BIDI, + * PdfWriter.RUN_DIRECTION_LTR or PdfWriter.RUN_DIRECTION_RTL. */ public void setRunDirection(int runDirection) { switch (runDirection) { - case PdfWriter.RUN_DIRECTION_DEFAULT: - case PdfWriter.RUN_DIRECTION_NO_BIDI: - case PdfWriter.RUN_DIRECTION_LTR: - case PdfWriter.RUN_DIRECTION_RTL: - this.runDirection = runDirection; - break; - default: - throw new RuntimeException("Invalid run direction: " + runDirection); + case PdfWriter.RUN_DIRECTION_DEFAULT: + case PdfWriter.RUN_DIRECTION_NO_BIDI: + case PdfWriter.RUN_DIRECTION_LTR: + case PdfWriter.RUN_DIRECTION_RTL: + this.runDirection = runDirection; + break; + default: + throw new RuntimeException("Invalid run direction: " + runDirection); } } - + /** * Returns the run direction of the contents in the table. - * + * * @return One of the following values: * PdfWriter.RUN_DIRECTION_DEFAULT, PdfWriter.RUN_DIRECTION_NO_BIDI, * PdfWriter.RUN_DIRECTION_LTR or PdfWriter.RUN_DIRECTION_RTL. @@ -1343,140 +1367,140 @@ public class PdfPTable implements LargeElement, PdfBlock { public int getRunDirection() { return runDirection; } - + /** * Getter for property lockedWidth. - * + * * @return Value of property lockedWidth. */ public boolean isLockedWidth() { return this.lockedWidth; } - + /** * Uses the value in setTotalWidth() in Document.add(). - * + * * @param lockedWidth true to use the value in setTotalWidth() in Document.add() */ public void setLockedWidth(boolean lockedWidth) { this.lockedWidth = lockedWidth; } - + /** * Gets the split value. - * + * * @return true to split; false otherwise */ public boolean isSplitRows() { return this.splitRows; } - + /** - * When set the rows that won't fit in the page will be split. + * When set the rows that won't fit in the page will be split. * Note that it takes at least twice the memory to handle a split table row * than a normal table. true by default. - * + * * @param splitRows true to split; false otherwise */ public void setSplitRows(boolean splitRows) { this.splitRows = splitRows; } - + /** * Sets the spacing before this table. * - * @param spacing the new spacing + * @param spacing the new spacing */ public void setSpacingBefore(float spacing) { this.spacingBefore = spacing; } - + /** * Sets the spacing after this table. * - * @param spacing the new spacing + * @param spacing the new spacing */ public void setSpacingAfter(float spacing) { this.spacingAfter = spacing; - } + } /** * Gets the spacing before this table. * - * @return the spacing + * @return the spacing */ public float spacingBefore() { return spacingBefore; } - + /** * Gets the spacing after this table. * - * @return the spacing + * @return the spacing */ public float spacingAfter() { return spacingAfter; - } - + } + /** * Gets the value of the last row extension. - * + * * @return true if the last row will extend; false otherwise */ public boolean isExtendLastRow() { return extendLastRow; } - + /** * When set the last row will be extended to fill all the remaining space * to the bottom boundary. - * + * * @param extendLastRow true to extend the last row; false otherwise */ public void setExtendLastRow(boolean extendLastRow) { this.extendLastRow = extendLastRow; } - + /** * Gets the header status inclusion in PdfPTableEvent. - * + * * @return true if the headers are included; false otherwise */ public boolean isHeadersInEvent() { return headersInEvent; } - + /** * When set the PdfPTableEvent will include the headers. - * + * * @param headersInEvent true to include the headers; false otherwise */ public void setHeadersInEvent(boolean headersInEvent) { this.headersInEvent = headersInEvent; } - + /** * Gets the property splitLate. - * + * * @return the property splitLate */ public boolean isSplitLate() { return splitLate; } - + /** * If true the row will only split if it's the first one in an empty page. * It's true by default. * It's only meaningful if setSplitRows(true). - * + * * @param splitLate the property value */ public void setSplitLate(boolean splitLate) { this.splitLate = splitLate; } - + /** - * If true the table will be kept on one page if it fits, by forcing a + * If true the table will be kept on one page if it fits, by forcing a * new page if it doesn't fit on the current page. The default is to * split the table over multiple pages. * @@ -1485,26 +1509,26 @@ public class PdfPTable implements LargeElement, PdfBlock { public void setKeepTogether(boolean keepTogether) { this.keepTogether = keepTogether; } - + /** * Getter for property keepTogether - * + * * @return true if it is tried to keep the table on one page; * false otherwise */ public boolean getKeepTogether() { return keepTogether; } - + /** * Gets the number of rows in the footer. - * + * * @return the number of rows in the footer */ public int getFooterRows() { return this.footerRows; } - + /** * Sets the number of rows to be used for the footer. The number * of footer rows are subtracted from the header rows. For @@ -1515,7 +1539,7 @@ public class PdfPTable implements LargeElement, PdfBlock { * table.setFooterRows(1); * * Row 0 and 1 will be the header rows and row 2 will be the footer row. - * + * * @param footerRows the number of rows to be used for the footer */ public void setFooterRows(int footerRows) { @@ -1523,7 +1547,7 @@ public class PdfPTable implements LargeElement, PdfBlock { footerRows = 0; this.footerRows = footerRows; } - + /** * Completes the current row with the default cell. An incomplete row will * be dropped but calling this method will make sure that it will be @@ -1534,67 +1558,31 @@ public class PdfPTable implements LargeElement, PdfBlock { addCell(defaultCell); } } - - /** - * @since iText 2.0.8 - * @see LargeElement#flushContent() - */ - public void flushContent() { - deleteBodyRows(); - setSkipFirstHeader(true); - } - - /** - * @since iText 2.0.8 - * @see LargeElement#isComplete() - */ - public boolean isComplete() { - return complete; - } - - /** - * @since iText 2.0.8 - * @see LargeElement#setComplete(boolean) - */ - public void setComplete(boolean complete) { - this.complete = complete; - } - - @Override - public float getHeight() { - return getTotalHeight(); - } - - @Override - public void calculateHeight() { - - } - - @Override - public String toHtmlString(double startY, double endY) { - StringBuffer a = new StringBuffer(); - float currentHeight = 0.0f; - a.append(""); - for(Object r : rows){ - PdfPRow row = (PdfPRow) r; - - if(currentHeight>=startY && currentHeight+ row.getMaxHeights() <= endY){ - a.append(""); - for(PdfPCell cell : row.getCells()){ - a.append(cell.getContainer().getHtmlContent(0, cell.getHeight())); - } - a.append(""); - } - currentHeight+=row.getMaxHeights(); -// -// if(currentHeight <= y && currentHeight + row.getMaxHeights() >x){ -// -// }else{ -// break; -// } - } - a.append("
"); - return a.toString(); + + /** + * @since iText 2.0.8 + * @see LargeElement#flushContent() + */ + public void flushContent() { + deleteBodyRows(); + setSkipFirstHeader(true); } + /** + * @since iText 2.0.8 + * @see LargeElement#isComplete() + */ + public boolean isComplete() { + return complete; + } + + /** + * @since iText 2.0.8 + * @see LargeElement#setComplete(boolean) + */ + public void setComplete(boolean complete) { + this.complete = complete; + } + + } \ No newline at end of file diff --git a/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/TableProperties.java b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/TableProperties.java new file mode 100644 index 000000000..317f70fd4 --- /dev/null +++ b/fine-itext/src/com/fr/third/v2/lowagie/text/pdf/TableProperties.java @@ -0,0 +1,52 @@ +package com.fr.third.v2.lowagie.text.pdf; + +/** + * 描述table的属性类 + * 包括cellspacing、cellpadding、border等 + */ +public class TableProperties { + private BorderStyle borderStyle = new BorderStyle(); + private float cellspacing = 2.0f; + private float cellpadding = 1.0f; + private boolean collapse = false; + + public BorderStyle getBorderStyle() { + return borderStyle; + } + + public void setBorderStyle(BorderStyle borderStyle) { + this.borderStyle = borderStyle; + } + + public float getCellspacing() { + return cellspacing; + } + + public void setCellspacing(float cellspacing) { + this.cellspacing = cellspacing; + } + + public float getCellpadding() { + return cellpadding; + } + + public void setCellpadding(float cellpadding) { + this.cellpadding = cellpadding; + } + + public boolean isCollapse() { + return collapse; + } + + public void setCollapse(boolean collapse) { + this.collapse = collapse; + } + + public String toHtmlString(){ + StringBuffer sb = new StringBuffer(); + sb.append("cellspacing").append("=").append(cellspacing).append(" "); + sb.append("cellpadding").append("=").append(cellpadding).append(" "); + sb.append("border").append("=").append(borderStyle.getBorderWidth()).append(" "); + return sb.toString(); + } +}