📄 pdfcell.java
字号:
int aCounter = 0;
for (Iterator it = list.getItems().iterator(); it.hasNext();) {
Element ele = (Element)it.next();
switch (ele.type()) {
case Element.LISTITEM:
ListItem item = (ListItem)ele;
line = new PdfLine(left + item.getIndentationLeft(), right, alignment, item.getLeading());
line.setListItem(item);
for (Iterator j = item.getChunks().iterator(); j.hasNext();) {
chunk = new PdfChunk((Chunk) j.next(), (PdfAction) (allActions.get(aCounter++)));
while ((overflow = line.add(chunk)) != null) {
addLine(line);
line = new PdfLine(left + item.getIndentationLeft(), right, alignment, item.getLeading());
chunk = overflow;
}
line.resetAlignment();
addLine(line);
line = new PdfLine(left + item.getIndentationLeft(), right, alignment, leading);
}
break;
case Element.LIST:
List sublist = (List)ele;
addList(sublist, left + sublist.getIndentationLeft(), right, alignment);
break;
}
}
}
// overriding of the Rectangle methods
/**
* Sets the bottom of the Rectangle and determines the proper {link #verticalOffset}
* to appropriately align the contents vertically.
* @param value
*/
public void setBottom(float value) {
super.setBottom(value);
float firstLineRealHeight = firstLineRealHeight();
float totalHeight = ury - value; // can't use top (already compensates for cellspacing)
float nonContentHeight = (cellpadding() * 2f) + (cellspacing() * 2f);
nonContentHeight += getBorderWidthInside(TOP) + getBorderWidthInside(BOTTOM);
float interiorHeight = totalHeight - nonContentHeight;
float extraHeight = 0.0f;
switch (verticalAlignment) {
case Element.ALIGN_BOTTOM:
extraHeight = interiorHeight - contentHeight;
break;
case Element.ALIGN_MIDDLE:
extraHeight = (interiorHeight - contentHeight) / 2.0f;
break;
default: // ALIGN_TOP
extraHeight = 0f;
}
extraHeight += cellpadding() + cellspacing();
extraHeight += getBorderWidthInside(TOP);
if (firstLine != null) {
firstLine.height = firstLineRealHeight + extraHeight;
}
}
/**
* Returns the lower left x-coordinaat.
*
* @return the lower left x-coordinaat
* @deprecated Use {@link #getLeft()} instead
*/
public float left() {
return getLeft();
}
/**
* Returns the lower left x-coordinaat.
*
* @return the lower left x-coordinaat
*/
public float getLeft() {
return super.getLeft(cellspacing);
}
/**
* Returns the upper right x-coordinate.
*
* @return the upper right x-coordinate
* @deprecated Use {@link #getRight()} instead
*/
public float right() {
return getRight();
}
/**
* Returns the upper right x-coordinate.
*
* @return the upper right x-coordinate
*/
public float getRight() {
return super.getRight(cellspacing);
}
/**
* Returns the upper right y-coordinate.
*
* @return the upper right y-coordinate
* @deprecated Use {@link #getTop()} instead
*/
public float top() {
return getTop();
}
/**
* Returns the upper right y-coordinate.
*
* @return the upper right y-coordinate
*/
public float getTop() {
return super.getTop(cellspacing);
}
/**
* Returns the lower left y-coordinate.
*
* @return the lower left y-coordinate
* @deprecated Use {@link #getBottom()} instead
*/
public float bottom() {
return getBottom();
}
/**
* Returns the lower left y-coordinate.
*
* @return the lower left y-coordinate
*/
public float getBottom() {
return super.getBottom(cellspacing);
}
// methods
private void addLine(PdfLine line) {
lines.add(line);
contentHeight += line.height();
lastLine = line;
this.line = null;
}
private PdfLine removeLine(int index) {
PdfLine oldLine = (PdfLine) lines.remove(index);
contentHeight -= oldLine.height();
if (index == 0) {
if (!lines.isEmpty()) {
firstLine = (PdfLine) lines.get(0);
float firstLineRealHeight = firstLineRealHeight();
contentHeight -= firstLine.height();
firstLine.height = firstLineRealHeight;
contentHeight += firstLineRealHeight;
}
}
return oldLine;
}
private void flushCurrentLine() {
if (line != null && line.size() > 0) {
addLine(line);
}
}
/**
* Calculates what the height of the first line should be so that the content will be
* flush with the top. For text, this is the height of the ascender. For an image,
* it is the actual height of the image.
* @return the real height of the first line
*/
private float firstLineRealHeight() {
float firstLineRealHeight = 0f;
if (firstLine != null) {
PdfChunk chunk = firstLine.getChunk(0);
if (chunk != null) {
Image image = chunk.getImage();
if (image != null) {
firstLineRealHeight = firstLine.getChunk(0).getImage().getScaledHeight();
} else {
firstLineRealHeight = useAscender ? firstLine.getAscender() : leading;
}
}
}
return firstLineRealHeight;
}
/**
* Gets the amount of the border for the specified side that is inside the Rectangle.
* For non-variable width borders this is only 1/2 the border width on that side. This
* always returns 0 if {@link #useBorderPadding} is false;
* @param side the side to check. One of the side constants in {@link com.lowagie.text.Rectangle}
* @return the borderwidth inside the cell
*/
private float getBorderWidthInside(int side) {
float width = 0f;
if (useBorderPadding) {
switch (side) {
case Rectangle.LEFT:
width = getBorderWidthLeft();
break;
case Rectangle.RIGHT:
width = getBorderWidthRight();
break;
case Rectangle.TOP:
width = getBorderWidthTop();
break;
default: // default and BOTTOM
width = getBorderWidthBottom();
break;
}
// non-variable (original style) borders overlap the rectangle (only 1/2 counts)
if (!isUseVariableBorders()) {
width = width / 2f;
}
}
return width;
}
/**
* Adds an image to this Cell.
*
* @param i the image to add
* @param left the left border
* @param right the right border
* @param extraHeight extra height to add above image
* @param alignment horizontal alignment (constant from Element class)
* @return the height of the image
*/
private float addImage(Image i, float left, float right, float extraHeight, int alignment) {
Image image = Image.getInstance(i);
if (image.getScaledWidth() > right - left) {
image.scaleToFit(right - left, Float.MAX_VALUE);
}
flushCurrentLine();
if (line == null) {
line = new PdfLine(left, right, alignment, leading);
}
PdfLine imageLine = line;
// left and right in chunk is relative to the start of the line
right = right - left;
left = 0f;
if ((image.getAlignment() & Image.RIGHT) == Image.RIGHT) {
left = right - image.getScaledWidth();
} else if ((image.getAlignment() & Image.MIDDLE) == Image.MIDDLE) {
left = left + ((right - left - image.getScaledWidth()) / 2f);
}
Chunk imageChunk = new Chunk(image, left, 0);
imageLine.add(new PdfChunk(imageChunk, null));
addLine(imageLine);
return imageLine.height();
}
/**
* Gets the lines of a cell that can be drawn between certain limits.
* <P>
* Remark: all the lines that can be drawn are removed from the object!
*
* @param top the top of the part of the table that can be drawn
* @param bottom the bottom of the part of the table that can be drawn
* @return an <CODE>ArrayList</CODE> of <CODE>PdfLine</CODE>s
*/
public ArrayList getLines(float top, float bottom) {
float lineHeight;
float currentPosition = Math.min(getTop(), top);
setTop(currentPosition + cellspacing);
ArrayList result = new ArrayList();
// if the bottom of the page is higher than the top of the cell: do nothing
if (getTop() < bottom) {
return result;
}
// we loop over the lines
int size = lines.size();
boolean aboveBottom = true;
for (int i = 0; i < size && aboveBottom; i++) {
line = (PdfLine) lines.get(i);
lineHeight = line.height();
currentPosition -= lineHeight;
// if the currentPosition is higher than the bottom, we add the line to the result
if (currentPosition > (bottom + cellpadding + getBorderWidthInside(BOTTOM))) {
result.add(line);
} else {
aboveBottom = false;
}
}
// if the bottom of the cell is higher than the bottom of the page, the cell is written, so we can remove all lines
float difference = 0f;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -