public void writeCells(int colStart,
int colEnd,
float xPos,
float yPos,
PdfContentByte[] canvases) {
if (!calculated)
calculateHeights();
if (colEnd < 0)
colEnd = cells.length;
colEnd = Math.min(colEnd, cells.length);
if (colStart < 0)
colStart = 0;
if (colStart >= colEnd)
return;
int newStart;
for (newStart = colStart; newStart >= 0; --newStart) {
if (cells[newStart] != null)
break;
xPos -= widths[newStart - 1];
}
xPos -= cells[newStart].getLeft();
for (int k = newStart; k < colEnd; ++k) {
PdfPCell cell = cells[k];
if (cell == null)
continue;
writeBorderAndBackground(xPos, yPos, cell, canvases);
Image img = cell.getImage();
float tly = 0;
switch (cell.getVerticalAlignment()) {
case Element.ALIGN_BOTTOM:
tly = cell.getTop() + yPos - maxHeight + cell.getHeight()
- cell.getEffectivePaddingTop();
break;
case Element.ALIGN_MIDDLE:
tly = cell.getTop() + yPos + (cell.getHeight() - maxHeight) / 2
- cell.getEffectivePaddingTop();
break;
default:
tly = cell.getTop() + yPos - cell.getEffectivePaddingTop();
break;
}
if (img != null) {
if (cell.getRotation() != 0) {
img = Image.getInstance(img);
img.setRotation(img.getImageRotation() + (float)(cell.getRotation() * Math.PI / 180.0));
}
boolean vf = false;
if (cell.getHeight() > maxHeight) {
img.scalePercent(100);
float scale = (maxHeight - cell.getEffectivePaddingTop() - cell
.getEffectivePaddingBottom())
/ img.getScaledHeight();
img.scalePercent(scale * 100);
vf = true;
}
float left = cell.getLeft() + xPos
+ cell.getEffectivePaddingLeft();
if (vf) {
switch (cell.getHorizontalAlignment()) {
case Element.ALIGN_CENTER:
left = xPos
+ (cell.getLeft() + cell.getEffectivePaddingLeft()
+ cell.getRight()
- cell.getEffectivePaddingRight() - img
.getScaledWidth()) / 2;
break;
case Element.ALIGN_RIGHT:
left = xPos + cell.getRight()
- cell.getEffectivePaddingRight()
- img.getScaledWidth();
break;
default:
break;
}
tly = cell.getTop() + yPos - cell.getEffectivePaddingTop();
}
img.setAbsolutePosition(left, tly - img.getScaledHeight());
try {
canvases[PdfPTable.TEXTCANVAS].addImage(img);
} catch (DocumentException e) {
throw new ExceptionConverter(e);
}
} else {
// rotation sponsored by Connection GmbH
if (cell.getRotation() == 90 || cell.getRotation() == 270) {
float netWidth = maxHeight - cell.getEffectivePaddingTop() - cell.getEffectivePaddingBottom();
float netHeight = cell.getWidth() - cell.getEffectivePaddingLeft() - cell.getEffectivePaddingRight();
ColumnText ct = ColumnText.duplicate(cell.getColumn());
ct.setCanvases(canvases);
ct.setSimpleColumn(0, 0, netWidth + 0.001f, -netHeight);
try {
ct.go(true);
} catch (DocumentException e) {
throw new ExceptionConverter(e);
}
float calcHeight = -ct.getYLine();
if (netWidth < = 0 || netHeight < = 0)
calcHeight = 0;
if (calcHeight > 0) {
if (cell.isUseDescender())
calcHeight -= ct.getDescender();
ct = ColumnText.duplicate(cell.getColumn());
ct.setCanvases(canvases);
ct.setSimpleColumn(0, -0.001f, netWidth + 0.001f, calcHeight);
float pivotX;
float pivotY;
if (cell.getRotation() == 90) {
pivotY = cell.getTop() + yPos - maxHeight + cell.getEffectivePaddingBottom();
switch (cell.getVerticalAlignment()) {
case Element.ALIGN_BOTTOM:
pivotX = cell.getLeft() + xPos + cell.getWidth() - cell.getEffectivePaddingRight();
break;
case Element.ALIGN_MIDDLE:
pivotX = cell.getLeft() + xPos + (cell.getWidth() + cell.getEffectivePaddingLeft() - cell.getEffectivePaddingRight() + calcHeight) / 2;
break;
default: //top
pivotX = cell.getLeft() + xPos + cell.getEffectivePaddingLeft() + calcHeight;
break;
}
saveAndRotateCanvases(canvases, 0,1,-1,0,pivotX,pivotY);
}
else {
pivotY = cell.getTop() + yPos - cell.getEffectivePaddingTop();
switch (cell.getVerticalAlignment()) {
case Element.ALIGN_BOTTOM:
pivotX = cell.getLeft() + xPos + cell.getEffectivePaddingLeft();
break;
case Element.ALIGN_MIDDLE:
pivotX = cell.getLeft() + xPos + (cell.getWidth() + cell.getEffectivePaddingLeft() - cell.getEffectivePaddingRight() - calcHeight) / 2;
break;
default: //top
pivotX = cell.getLeft() + xPos + cell.getWidth() - cell.getEffectivePaddingRight() - calcHeight;
break;
}
saveAndRotateCanvases(canvases, 0,-1,1,0,pivotX,pivotY);
}
try {
ct.go();
} catch (DocumentException e) {
throw new ExceptionConverter(e);
} finally {
restoreCanvases(canvases);
}
}
}
else {
float fixedHeight = cell.getFixedHeight();
float rightLimit = cell.getRight() + xPos
- cell.getEffectivePaddingRight();
float leftLimit = cell.getLeft() + xPos
+ cell.getEffectivePaddingLeft();
if (cell.isNoWrap()) {
switch (cell.getHorizontalAlignment()) {
case Element.ALIGN_CENTER:
rightLimit += 10000;
leftLimit -= 10000;
break;
case Element.ALIGN_RIGHT:
leftLimit -= 20000;
break;
default:
rightLimit += 20000;
break;
}
}
ColumnText ct = ColumnText.duplicate(cell.getColumn());
ct.setCanvases(canvases);
float bry = tly
- (maxHeight /* cell.height() */
- cell.getEffectivePaddingTop() - cell.getEffectivePaddingBottom());
if (fixedHeight > 0) {
if (cell.getHeight() > maxHeight) {
tly = cell.getTop() + yPos - cell.getEffectivePaddingTop();
bry = cell.getTop() + yPos - maxHeight + cell.getEffectivePaddingBottom();
}
}
if ((tly > bry || ct.zeroHeightElement()) && leftLimit < rightLimit) {
ct.setSimpleColumn(leftLimit, bry - 0.001f, rightLimit, tly);
if (cell.getRotation() == 180) {
float shx = leftLimit + rightLimit;
float shy = yPos + yPos - maxHeight + cell.getEffectivePaddingBottom() - cell.getEffectivePaddingTop();
saveAndRotateCanvases(canvases, -1,0,0,-1,shx,shy);
}
try {
ct.go();
} catch (DocumentException e) {
throw new ExceptionConverter(e);
} finally {
if (cell.getRotation() == 180) {
restoreCanvases(canvases);
}
}
}
}
}
PdfPCellEvent evt = cell.getCellEvent();
if (evt != null) {
Rectangle rect = new Rectangle(cell.getLeft() + xPos, cell.getTop()
+ yPos - maxHeight, cell.getRight() + xPos, cell.getTop()
+ yPos);
evt.cellLayout(cell, rect, canvases);
}
}
}
Writes a number of cells (not necessarily all cells). |