Browse Source

Merge pull request #106 in CORE/base-third from ~KERRY/base-third:feature/10.0 to feature/10.0

* commit '2075bce909584c147b6ce46b812ee38f3d62678f':
  代码修改
  wu
  无
  WU
research/11.0
ju 6 years ago
parent
commit
73983c9393
  1. 12
      fine-itext/src/com/fr/Container.java
  2. 1
      fine-itext/src/com/fr/third/v2/lowagie/text/html/CSS.java
  3. 14
      fine-itext/src/com/fr/third/v2/lowagie/text/html/CSSUtils.java
  4. 80
      fine-itext/src/com/fr/third/v2/lowagie/text/html/SpaceWithPunctuationBreakIterator.java
  5. 6
      fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/ChainedProperties.java
  6. 10
      fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HTMLWorker.java
  7. 45
      fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/IncCell.java
  8. 76
      fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/IncTable.java
  9. 58
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/BorderStyle.java
  10. 8
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/ColumnText.java
  11. 85
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java
  12. 8
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfLine.java
  13. 45
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPCell.java
  14. 15
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPRow.java
  15. 56
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPTable.java
  16. 52
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/TableProperties.java

12
fine-itext/src/com/fr/Container.java

@ -0,0 +1,12 @@
package com.fr;
/**
* @author kerry
* @date 2018/8/20
*/
public interface Container {
String getHtmlContent(double lineStartY, double lineEndY);
float getHeight();
}

1
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";

14
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;
}
}

80
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();
}
}

6
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);

10
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;

45
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<String, String> backgroundRules = new HashMap<String, String>();
value = props.getLastChainProperty("background-size");
if(value!=null){
backgroundRules.put("background-size", value);
}
value = props.getLastChainProperty("background");
if(value!=null){
Map<String, String> 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) {

76
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<Float> rowHeights = new ArrayList<Float>();
/** 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,18 +104,44 @@ public class IncTable {
return new PdfPTable(1);
int ncol = 0;
ArrayList c0 = (ArrayList)rows.get(0);
ArrayList<Float> colWidths = new ArrayList<Float>();
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);
try {
TableProperties tableProperties = parseTableProperties();
table.setTableProperties(tableProperties);
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)));
table.setWidthPercentage(CSSUtils.parseFloat(width));
else {
table.setTotalWidth(Float.parseFloat(width));
//解析单元格宽度
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);
}
}
@ -118,6 +151,41 @@ public class IncTable {
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;
}
}

58
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;
}
}

8
fine-itext/src/com/fr/third/v2/lowagie/text/pdf/ColumnText.java

@ -243,6 +243,14 @@ public class ColumnText {
protected LinkedList compositeElements;
public LinkedList getCompositeElements() {
return compositeElements;
}
public void setCompositeElements(LinkedList compositeElements) {
this.compositeElements = compositeElements;
}
protected int listIdx = 0;
private boolean splittedRow;

85
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<String, String> background ;
public Map<String, String> getBackground() {
return background;
}
public void setBackground(Map<String, String> background) {
this.background = background;
}
static {
keysAttributes.put(Chunk.ACTION, null);
keysAttributes.put(Chunk.UNDERLINE, null);
@ -298,7 +312,7 @@ public class PdfChunk {
* @return the Unicode equivalent
*/
public int getUnicodeEquivalent(int c) {
return 0;
return c;
}
protected int getWord(String text, int start) {
@ -330,24 +344,26 @@ 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) {
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
@ -361,53 +377,24 @@ public class PdfChunk {
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)
String substring = value.substring(start, next);
currentWidth += font.width(substring);
if (currentWidth + indent.getRight() > width){
currentPosition = start - 1;
break;
// if a split-character is encountered, the splitPosition is altered
if (splitCharacter.isSplitCharacter(0, currentPosition, length, valueArray, null))
splitPosition = currentPosition + 1;
currentPosition++;
}
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;
}
@ -808,7 +795,7 @@ public class PdfChunk {
}
//todo 需要添加其他样式属性
htmlString.append("<span style='");
htmlString.append("").append(getStyleAttributes()).append("'");
htmlString.append(getStyleAttributes()).append("'");
htmlString.append(">");
htmlString.append(value);
htmlString.append("</span>");

8
fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfLine.java

@ -255,7 +255,7 @@ public class PdfLine {
* @return a value
*/
float indentLeft() {
public float indentLeft() {
if (isRTL) {
switch (alignment) {
case Element.ALIGN_LEFT:
@ -312,7 +312,7 @@ public class PdfLine {
* @return a value
*/
float widthLeft() {
public float widthLeft() {
return width;
}
@ -460,7 +460,7 @@ public class PdfLine {
* @return an extra leading for images
* @since 2.1.5
*/
float[] getMaxSize() {
public float[] getMaxSize() {
float normal_leading = 0;
float image_leading = -10000;
PdfChunk chunk;
@ -476,7 +476,7 @@ public class PdfLine {
return new float[]{normal_leading, image_leading};
}
boolean isRTL() {
public boolean isRTL() {
return isRTL;
}

45
fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPCell.java

@ -50,7 +50,9 @@
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;
import com.fr.third.v2.lowagie.text.DocumentException;
import com.fr.third.v2.lowagie.text.Element;
@ -66,6 +68,36 @@ 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<String, String> background ;
public Map<String, String> getBackground() {
return background;
}
public void setBackground(Map<String, String> background) {
this.background = background;
}
public void setStyleWidth(float styleWidth) {
this.styleWidth = styleWidth;
}
private ColumnText column = new ColumnText(null);
/** Vertical alignment of the cell. */
@ -256,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;
}
/**
@ -1010,4 +1045,14 @@ public class PdfPCell extends Rectangle {
height = getMinimumHeight();
return height;
}
private Container container;
public Container getContainer() {
return container;
}
public void addContent(Container container){
this.container = container;
}
}

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

@ -76,6 +76,16 @@ public class PdfPRow {
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,15 +196,16 @@ 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);
}
/**
* Writes the border and background of one cell in the row.
*

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

@ -65,9 +65,10 @@ import com.fr.third.v2.lowagie.text.pdf.events.PdfPTableEventForwarder;
* be added to the document as the class <CODE>Table</CODE>.
* 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.
* <P>
* <p>
* A PdfPTableEvent can be associated to the table to do custom drawing
* when the table is rendered.
*
* @author Paulo Soares (psoares@consiste.pt)
*/
@ -102,6 +103,15 @@ public class PdfPTable implements LargeElement {
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.
@ -124,6 +134,7 @@ public class PdfPTable implements LargeElement {
private boolean skipFirstHeader = false;
/**
* Holds value of property skipLastFooter.
*
* @since 2.1.6
*/
private boolean skipLastFooter = false;
@ -187,6 +198,7 @@ public class PdfPTable implements LargeElement {
/**
* Keeps track of the completeness of the current row.
*
* @since 2.1.6
*/
protected boolean rowCompleted = true;
@ -295,6 +307,7 @@ public class PdfPTable implements LargeElement {
horizontalAlignment = sourceTable.horizontalAlignment;
keepTogether = sourceTable.keepTogether;
complete = sourceTable.complete;
tableProperties = sourceTable.tableProperties;
}
/**
@ -312,7 +325,7 @@ public class PdfPTable implements LargeElement {
absoluteWidths = new float[relativeWidths.length];
totalHeight = 0;
calculateWidths();
calculateHeights(true);
// calculateHeights(true);
}
/**
@ -337,10 +350,12 @@ public class PdfPTable implements LargeElement {
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;
}
/**
@ -354,7 +369,11 @@ public class PdfPTable implements LargeElement {
this.totalWidth = totalWidth;
totalHeight = 0;
calculateWidths();
calculateHeights(true);
// calculateHeights(true);
for (int k = 0; k < rows.size(); ++k) {
PdfPRow row = (PdfPRow) rows.get(k);
row.setWidths(absoluteWidths);
}
}
/**
@ -414,9 +433,13 @@ public class PdfPTable implements LargeElement {
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 += (borderWidth + cellspacing) * (rows.size() + 1);
return totalHeight;
}
@ -486,7 +509,7 @@ public class PdfPTable implements LargeElement {
PdfPRow row = new PdfPRow(currentRow);
if (totalWidth > 0) {
row.setWidths(absoluteWidths);
totalHeight += row.getMaxHeights();
// totalHeight += row.getMaxHeights();
}
rows.add(row);
currentRow = new PdfPCell[numCols];
@ -503,6 +526,7 @@ public class PdfPTable implements LargeElement {
/**
* When updating the row index, cells with rowspan should be taken into account.
* This is what happens in this method.
*
* @since 2.1.6
*/
private void skipColsWithRowspanAbove() {
@ -515,6 +539,7 @@ public class PdfPTable implements LargeElement {
/**
* 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
@ -834,6 +859,7 @@ public class PdfPTable implements LargeElement {
public float getRowHeight(int idx) {
return getRowHeight(idx, false);
}
/**
* Gets the height of a particular row.
*
@ -881,9 +907,9 @@ public class PdfPTable implements LargeElement {
* 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).
*
* @return the height of a particular row including rowspan
* @param rowIndex the row index
* @param cellIndex the cell index
* @return the height of a particular row including rowspan
* @since 2.1.6
*/
public float getRowspanHeight(int rowIndex, int cellIndex) {
@ -1034,16 +1060,16 @@ public class PdfPTable implements LargeElement {
}
/**
* @see Element#isContent()
* @since iText 2.0.8
* @see Element#isContent()
*/
public boolean isContent() {
return true;
}
/**
* @see Element#isNestable()
* @since iText 2.0.8
* @see Element#isNestable()
*/
public boolean isNestable() {
return true;
@ -1059,8 +1085,7 @@ public class PdfPTable implements LargeElement {
public boolean process(ElementListener listener) {
try {
return listener.add(this);
}
catch(DocumentException de) {
} catch (DocumentException de) {
return false;
}
}
@ -1125,6 +1150,7 @@ public class PdfPTable implements LargeElement {
/**
* 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
@ -1173,6 +1199,7 @@ public class PdfPTable implements LargeElement {
/**
* 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
@ -1196,7 +1223,9 @@ public class PdfPTable implements LargeElement {
return row;
}
/** Sets the table event for this table.
/**
* Sets the table event for this table.
*
* @param event the table event for this table
*/
public void setTableEvent(PdfPTableEvent event) {
@ -1256,8 +1285,7 @@ public class PdfPTable implements LargeElement {
else
widths[n++] = row.getEventWidth(xPos);
}
}
else {
} else {
int numCols = getNumberOfColumns();
float width[] = new float[numCols + 1];
width[0] = xPos;
@ -1559,4 +1587,6 @@ public class PdfPTable implements LargeElement {
public void setComplete(boolean complete) {
this.complete = complete;
}
}

52
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的属性类
* 包括cellspacingcellpaddingborder等
*/
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();
}
}
Loading…
Cancel
Save