Browse Source

research/11.0
kerry 6 years ago
parent
commit
f8c3aeed1d
  1. 1
      fine-itext/src/com/fr/third/v2/lowagie/text/html/CSS.java
  2. 14
      fine-itext/src/com/fr/third/v2/lowagie/text/html/CSSUtils.java
  3. 80
      fine-itext/src/com/fr/third/v2/lowagie/text/html/SpaceWithPunctuationBreakIterator.java
  4. 6
      fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/ChainedProperties.java
  5. 10
      fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/HTMLWorker.java
  6. 45
      fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/IncCell.java
  7. 76
      fine-itext/src/com/fr/third/v2/lowagie/text/html/simpleparser/IncTable.java
  8. 58
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/BorderStyle.java
  9. 12
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfBlock.java
  10. 83
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfChunk.java
  11. 34
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPCell.java
  12. 30
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPRow.java
  13. 138
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfPTable.java
  14. 52
      fine-itext/src/com/fr/third/v2/lowagie/text/pdf/TableProperties.java

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

12
fine-itext/src/com/fr/third/v2/lowagie/text/pdf/PdfBlock.java

@ -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);
}

83
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);
@ -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>");

34
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;
@ -67,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. */
@ -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;
}
/**

30
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,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.

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

@ -65,13 +65,14 @@ 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)
*/
public class PdfPTable implements LargeElement, PdfBlock {
public class PdfPTable implements LargeElement {
/**
* The index of the original <CODE>PdfcontentByte</CODE>.
@ -97,11 +98,20 @@ public class PdfPTable implements LargeElement, PdfBlock {
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.
@ -124,6 +134,7 @@ public class PdfPTable implements LargeElement, PdfBlock {
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, PdfBlock {
/**
* Keeps track of the completeness of the current row.
*
* @since 2.1.6
*/
protected boolean rowCompleted = true;
@ -242,7 +254,7 @@ 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);
@ -295,6 +307,7 @@ public class PdfPTable implements LargeElement, PdfBlock {
horizontalAlignment = sourceTable.horizontalAlignment;
keepTogether = sourceTable.keepTogether;
complete = sourceTable.complete;
tableProperties = sourceTable.tableProperties;
}
/**
@ -312,7 +325,7 @@ public class PdfPTable implements LargeElement, PdfBlock {
absoluteWidths = new float[relativeWidths.length];
totalHeight = 0;
calculateWidths();
calculateHeights(true);
// calculateHeights(true);
}
/**
@ -337,10 +350,12 @@ public class PdfPTable implements LargeElement, PdfBlock {
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,7 @@ public class PdfPTable implements LargeElement, PdfBlock {
this.totalWidth = totalWidth;
totalHeight = 0;
calculateWidths();
calculateHeights(true);
// calculateHeights(true);
}
/**
@ -414,9 +429,13 @@ public class PdfPTable implements LargeElement, PdfBlock {
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 +505,7 @@ 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];
@ -503,6 +522,7 @@ public class PdfPTable implements LargeElement, PdfBlock {
/**
* 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 +535,7 @@ public class PdfPTable implements LargeElement, PdfBlock {
/**
* 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
@ -528,34 +549,34 @@ public class PdfPTable implements LargeElement, PdfBlock {
return false;
int row = currRow - 1;
PdfPRow aboveRow = (PdfPRow)rows.get(row);
PdfPRow aboveRow = (PdfPRow) rows.get(row);
if (aboveRow == null)
return false;
PdfPCell aboveCell = (PdfPCell)aboveRow.getCells()[currCol];
PdfPCell aboveCell = (PdfPCell) aboveRow.getCells()[currCol];
while ((aboveCell == null) && (row > 0)) {
aboveRow = (PdfPRow)rows.get(--row);
aboveRow = (PdfPRow) rows.get(--row);
if (aboveRow == null)
return false;
aboveCell = (PdfPCell)aboveRow.getCells()[currCol];
aboveCell = (PdfPCell) aboveRow.getCells()[currCol];
}
int distance = currRow - row;
if (aboveCell == null) {
int col = currCol - 1;
aboveCell = (PdfPCell)aboveRow.getCells()[col];
aboveCell = (PdfPCell) aboveRow.getCells()[col];
while ((aboveCell == null) && (row > 0))
aboveCell = (PdfPCell)aboveRow.getCells()[--col];
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);
aboveRow = (PdfPRow) rows.get(row + 1);
distance--;
aboveCell = (PdfPCell)aboveRow.getCells()[col];
aboveCell = (PdfPCell) aboveRow.getCells()[col];
while ((aboveCell == null) && (col > 0))
aboveCell = (PdfPCell)aboveRow.getCells()[--col];
aboveCell = (PdfPCell) aboveRow.getCells()[--col];
}
return aboveCell != null && aboveCell.getRowspan() > distance;
@ -669,7 +690,7 @@ public class PdfPTable implements LargeElement, PdfBlock {
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();
@ -680,7 +701,7 @@ public class PdfPTable implements LargeElement, PdfBlock {
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();
@ -834,6 +855,7 @@ public class PdfPTable implements LargeElement, PdfBlock {
public float getRowHeight(int idx) {
return getRowHeight(idx, false);
}
/**
* Gets the height of a particular row.
*
@ -845,7 +867,7 @@ public class PdfPTable implements LargeElement, PdfBlock {
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)
@ -854,13 +876,13 @@ public class PdfPTable implements LargeElement, PdfBlock {
PdfPCell cell;
PdfPRow tmprow;
for (int i = 0; i < relativeWidths.length; i++) {
if(!rowSpanAbove(idx, i))
if (!rowSpanAbove(idx, i))
continue;
int rs = 1;
while (rowSpanAbove(idx - rs, i)) {
rs++;
}
tmprow = (PdfPRow)rows.get(idx - rs);
tmprow = (PdfPRow) rows.get(idx - rs);
cell = tmprow.getCells()[i];
float tmp = 0;
if (cell.getRowspan() == rs + 1) {
@ -881,15 +903,15 @@ public class PdfPTable implements LargeElement, PdfBlock {
* 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) {
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];
@ -912,7 +934,7 @@ public class PdfPTable implements LargeElement, PdfBlock {
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();
}
@ -931,7 +953,7 @@ public class PdfPTable implements LargeElement, PdfBlock {
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();
}
@ -948,7 +970,7 @@ public class PdfPTable implements LargeElement, PdfBlock {
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();
}
@ -1034,16 +1056,16 @@ public class PdfPTable implements LargeElement, PdfBlock {
}
/**
* @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 +1081,7 @@ public class PdfPTable implements LargeElement, PdfBlock {
public boolean process(ElementListener listener) {
try {
return listener.add(this);
}
catch(DocumentException de) {
} catch (DocumentException de) {
return false;
}
}
@ -1111,7 +1132,7 @@ public class PdfPTable implements LargeElement, PdfBlock {
* @return the row at position idx
*/
public PdfPRow getRow(int idx) {
return (PdfPRow)rows.get(idx);
return (PdfPRow) rows.get(idx);
}
/**
@ -1125,6 +1146,7 @@ public class PdfPTable implements LargeElement, PdfBlock {
/**
* 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 +1195,7 @@ public class PdfPTable implements LargeElement, PdfBlock {
/**
* 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 +1219,9 @@ public class PdfPTable implements LargeElement, PdfBlock {
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) {
@ -1205,7 +1230,7 @@ public class PdfPTable implements LargeElement, PdfBlock {
else if (this.tableEvent == null)
this.tableEvent = event;
else if (this.tableEvent instanceof PdfPTableEventForwarder)
((PdfPTableEventForwarder)this.tableEvent).addTableEvent(event);
((PdfPTableEventForwarder) this.tableEvent).addTableEvent(event);
else {
PdfPTableEventForwarder forward = new PdfPTableEventForwarder();
forward.addTableEvent(this.tableEvent);
@ -1232,7 +1257,7 @@ public class PdfPTable implements LargeElement, PdfBlock {
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,14 +1275,13 @@ public class PdfPTable implements LargeElement, PdfBlock {
}
}
for (; firstRow < lastRow; ++firstRow) {
PdfPRow row = (PdfPRow)rows.get(firstRow);
PdfPRow row = (PdfPRow) rows.get(firstRow);
if (row == null)
++n;
else
widths[n++] = row.getEventWidth(xPos);
}
}
else {
} else {
int numCols = getNumberOfColumns();
float width[] = new float[numCols + 1];
width[0] = xPos;
@ -1560,41 +1584,5 @@ public class PdfPTable implements LargeElement, PdfBlock {
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("<table>");
for(Object r : rows){
PdfPRow row = (PdfPRow) r;
if(currentHeight>=startY && currentHeight+ row.getMaxHeights() <= endY){
a.append("<tr>");
for(PdfPCell cell : row.getCells()){
a.append(cell.getContainer().getHtmlContent(0, cell.getHeight()));
}
a.append("</tr>");
}
currentHeight+=row.getMaxHeights();
//
// if(currentHeight <= y && currentHeight + row.getMaxHeights() >x){
//
// }else{
// break;
// }
}
a.append("</table>");
return a.toString();
}
}

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