📄 columntext.java
字号:
}
/**
* Adds a <CODE>Chunk</CODE> to the current text array.
* Will not have any effect if addElement() was called before.
* @param chunk the text
*/
public void addText(Chunk chunk) {
if (chunk == null || composite)
return;
addText(new Phrase(chunk));
}
/**
* Adds an element. Elements supported are <CODE>Paragraph</CODE>,
* <CODE>List</CODE>, <CODE>PdfPTable</CODE>, <CODE>Image</CODE> and
* <CODE>Graphic</CODE>.
* <p>
* It removes all the text placed with <CODE>addText()</CODE>.
* @param element the <CODE>Element</CODE>
*/
public void addElement(Element element) {
if (element == null)
return;
if (element instanceof Image) {
Image img = (Image)element;
PdfPTable t = new PdfPTable(1);
float w = img.getWidthPercentage();
if (w == 0) {
t.setTotalWidth(img.getScaledWidth());
t.setLockedWidth(true);
}
else
t.setWidthPercentage(w);
t.setSpacingAfter(img.getSpacingAfter());
t.setSpacingBefore(img.getSpacingBefore());
switch (img.getAlignment()) {
case Image.LEFT:
t.setHorizontalAlignment(Element.ALIGN_LEFT);
break;
case Image.RIGHT:
t.setHorizontalAlignment(Element.ALIGN_RIGHT);
break;
default:
t.setHorizontalAlignment(Element.ALIGN_CENTER);
break;
}
PdfPCell c = new PdfPCell(img, true);
c.setPadding(0);
c.setBorder(img.getBorder());
c.setBorderColor(img.getBorderColor());
c.setBorderWidth(img.getBorderWidth());
c.setBackgroundColor(img.getBackgroundColor());
t.addCell(c);
element = t;
}
if (element.type() == Element.CHUNK) {
element = new Paragraph((Chunk)element);
}
else if (element.type() == Element.PHRASE) {
element = new Paragraph((Phrase)element);
}
if (element instanceof SimpleTable) {
try {
element = ((SimpleTable)element).createPdfPTable();
} catch (DocumentException e) {
throw new IllegalArgumentException("Element not allowed.");
}
}
else if (element.type() != Element.PARAGRAPH && element.type() != Element.LIST && element.type() != Element.PTABLE)
throw new IllegalArgumentException("Element not allowed.");
if (!composite) {
composite = true;
compositeElements = new LinkedList();
bidiLine = null;
waitPhrase = null;
}
compositeElements.add(element);
}
/**
* Converts a sequence of lines representing one of the column bounds into
* an internal format.
* <p>
* Each array element will contain a <CODE>float[4]</CODE> representing
* the line x = ax + b.
* @param cLine the column array
* @return the converted array
*/
protected ArrayList convertColumn(float cLine[]) {
if (cLine.length < 4)
throw new RuntimeException("No valid column line found.");
ArrayList cc = new ArrayList();
for (int k = 0; k < cLine.length - 2; k += 2) {
float x1 = cLine[k];
float y1 = cLine[k + 1];
float x2 = cLine[k + 2];
float y2 = cLine[k + 3];
if (y1 == y2)
continue;
// x = ay + b
float a = (x1 - x2) / (y1 - y2);
float b = x1 - a * y1;
float r[] = new float[4];
r[0] = Math.min(y1, y2);
r[1] = Math.max(y1, y2);
r[2] = a;
r[3] = b;
cc.add(r);
maxY = Math.max(maxY, r[1]);
minY = Math.min(minY, r[0]);
}
if (cc.isEmpty())
throw new RuntimeException("No valid column line found.");
return cc;
}
/**
* Finds the intersection between the <CODE>yLine</CODE> and the column. It will
* set the <CODE>lineStatus</CODE> apropriatly.
* @param wall the column to intersect
* @return the x coordinate of the intersection
*/
protected float findLimitsPoint(ArrayList wall) {
lineStatus = LINE_STATUS_OK;
if (yLine < minY || yLine > maxY) {
lineStatus = LINE_STATUS_OFFLIMITS;
return 0;
}
for (int k = 0; k < wall.size(); ++k) {
float r[] = (float[])wall.get(k);
if (yLine < r[0] || yLine > r[1])
continue;
return r[2] * yLine + r[3];
}
lineStatus = LINE_STATUS_NOLINE;
return 0;
}
/**
* Finds the intersection between the <CODE>yLine</CODE> and the two
* column bounds. It will set the <CODE>lineStatus</CODE> apropriatly.
* @return a <CODE>float[2]</CODE>with the x coordinates of the intersection
*/
protected float[] findLimitsOneLine() {
float x1 = findLimitsPoint(leftWall);
if (lineStatus == LINE_STATUS_OFFLIMITS || lineStatus == LINE_STATUS_NOLINE)
return null;
float x2 = findLimitsPoint(rightWall);
if (lineStatus == LINE_STATUS_NOLINE)
return null;
return new float[]{x1, x2};
}
/**
* Finds the intersection between the <CODE>yLine</CODE>,
* the <CODE>yLine-leading</CODE>and the two
* column bounds. It will set the <CODE>lineStatus</CODE> apropriatly.
* @return a <CODE>float[4]</CODE>with the x coordinates of the intersection
*/
protected float[] findLimitsTwoLines() {
boolean repeat = false;
for (;;) {
if (repeat && currentLeading == 0)
return null;
repeat = true;
float x1[] = findLimitsOneLine();
if (lineStatus == LINE_STATUS_OFFLIMITS)
return null;
yLine -= currentLeading;
if (lineStatus == LINE_STATUS_NOLINE) {
continue;
}
float x2[] = findLimitsOneLine();
if (lineStatus == LINE_STATUS_OFFLIMITS)
return null;
if (lineStatus == LINE_STATUS_NOLINE) {
yLine -= currentLeading;
continue;
}
if (x1[0] >= x2[1] || x2[0] >= x1[1])
continue;
return new float[]{x1[0], x1[1], x2[0], x2[1]};
}
}
/**
* Sets the columns bounds. Each column bound is described by a
* <CODE>float[]</CODE> with the line points [x1,y1,x2,y2,...].
* The array must have at least 4 elements.
* @param leftLine the left column bound
* @param rightLine the right column bound
*/
public void setColumns(float leftLine[], float rightLine[]) {
maxY = -10e20f;
minY = 10e20f;
rightWall = convertColumn(rightLine);
leftWall = convertColumn(leftLine);
rectangularWidth = -1;
rectangularMode = false;
}
/**
* Simplified method for rectangular columns.
* @param phrase a <CODE>Phrase</CODE>
* @param llx the lower left x corner
* @param lly the lower left y corner
* @param urx the upper right x corner
* @param ury the upper right y corner
* @param leading the leading
* @param alignment the column alignment
*/
public void setSimpleColumn(Phrase phrase, float llx, float lly, float urx, float ury, float leading, int alignment) {
addText(phrase);
setSimpleColumn(llx, lly, urx, ury, leading, alignment);
}
/**
* Simplified method for rectangular columns.
* @param llx the lower left x corner
* @param lly the lower left y corner
* @param urx the upper right x corner
* @param ury the upper right y corner
* @param leading the leading
* @param alignment the column alignment
*/
public void setSimpleColumn(float llx, float lly, float urx, float ury, float leading, int alignment) {
setLeading(leading);
this.alignment = alignment;
setSimpleColumn(llx, lly, urx, ury);
}
/**
* Simplified method for rectangular columns.
* @param llx
* @param lly
* @param urx
* @param ury
*/
public void setSimpleColumn(float llx, float lly, float urx, float ury) {
leftX = Math.min(llx, urx);
maxY = Math.max(lly, ury);
minY = Math.min(lly, ury);
rightX = Math.max(llx, urx);
yLine = maxY;
rectangularWidth = rightX - leftX;
if (rectangularWidth < 0)
rectangularWidth = 0;
rectangularMode = true;
}
/**
* Sets the leading to fixed
* @param leading the leading
*/
public void setLeading(float leading) {
fixedLeading = leading;
multipliedLeading = 0;
}
/**
* Sets the leading fixed and variable. The resultant leading will be
* fixedLeading+multipliedLeading*maxFontSize where maxFontSize is the
* size of the bigest font in the line.
* @param fixedLeading the fixed leading
* @param multipliedLeading the variable leading
*/
public void setLeading(float fixedLeading, float multipliedLeading) {
this.fixedLeading = fixedLeading;
this.multipliedLeading = multipliedLeading;
}
/**
* Gets the fixed leading
* @return the leading
*/
public float getLeading() {
return fixedLeading;
}
/**
* Gets the variable leading
* @return the leading
*/
public float getMultipliedLeading() {
return multipliedLeading;
}
/**
* Sets the yLine. The line will be written to yLine-leading.
* @param yLine the yLine
*/
public void setYLine(float yLine) {
this.yLine = yLine;
}
/**
* Gets the yLine.
* @return the yLine
*/
public float getYLine() {
return yLine;
}
/**
* Sets the alignment.
* @param alignment the alignment
*/
public void setAlignment(int alignment) {
this.alignment = alignment;
}
/**
* Gets the alignment.
* @return the alignment
*/
public int getAlignment() {
return alignment;
}
/**
* Sets the first paragraph line indent.
* @param indent the indent
*/
public void setIndent(float indent) {
this.indent = indent;
lastWasNewline = true;
}
/**
* Gets the first paragraph line indent.
* @return the indent
*/
public float getIndent() {
return indent;
}
/**
* Sets the following paragraph lines indent.
* @param indent the indent
*/
public void setFollowingIndent(float indent) {
this.followingIndent = indent;
lastWasNewline = true;
}
/**
* Gets the following paragraph lines indent.
* @return the indent
*/
public float getFollowingIndent() {
return followingIndent;
}
/**
* Sets the right paragraph lines indent.
* @param indent the indent
*/
public void setRightIndent(float indent) {
this.rightIndent = indent;
lastWasNewline = true;
}
/**
* Gets the right paragraph lines indent.
* @return the indent
*/
public float getRightIndent() {
return rightIndent;
}
/**
* Outputs the lines to the document. It is equivalent to <CODE>go(false)</CODE>.
* @return returns the result of the operation. It can be <CODE>NO_MORE_TEXT</CODE>
* and/or <CODE>NO_MORE_COLUMN</CODE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -