📄 multicolumntext.java
字号:
} currentHeight = Math.max(currentHeight, getHeight(left, right)); if (currentDef.isSimple()) { columnText.setSimpleColumn(left[2], left[3], right[0], right[1]); } else { columnText.setColumns(left, right); } int result = columnText.go(); if ((result & ColumnText.NO_MORE_TEXT) != 0) { done = true; top = columnText.getYLine(); } else if (shiftCurrentColumn()) { top = nextY; } else { // check if we are done because of height totalHeight += currentHeight; if ((desiredHeight != AUTOMATIC) && (totalHeight >= desiredHeight)) { overflow = true; break; } else { // need to start new page and reset the columns documentY = nextY; newPage(); currentHeight = 0; } } } } catch (DocumentException ex) { ex.printStackTrace(); throw ex; } if (desiredHeight == AUTOMATIC && columnDefs.size() == 1) { currentHeight = documentY - columnText.getYLine(); } return currentHeight; } private void newPage() throws DocumentException { resetCurrentColumn(); if (desiredHeight == AUTOMATIC) { top = nextY = AUTOMATIC; } else { top = nextY; } totalHeight = 0; if (document != null) { document.newPage(); } } /** * Figure out the height of a column from the border extents * * @param left left border * @param right right border * @return height */ private float getHeight(float[] left, float[] right) { float max = Float.MIN_VALUE; float min = Float.MAX_VALUE; for (int i = 0; i < left.length; i += 2) { min = Math.min(min, left[i + 1]); max = Math.max(max, left[i + 1]); } for (int i = 0; i < right.length; i += 2) { min = Math.min(min, right[i + 1]); max = Math.max(max, right[i + 1]); } return max - min; } /** * Processes the element by adding it to an * <CODE>ElementListener</CODE>. * * @param listener an <CODE>ElementListener</CODE> * @return <CODE>true</CODE> if the element was processed successfully */ public boolean process(ElementListener listener) { try { return listener.add(this); } catch (DocumentException de) { return false; } } /** * Gets the type of the text element. * * @return a type */ public int type() { return Element.MULTI_COLUMN_TEXT; } /** * Returns null - not used * * @return null */ public ArrayList getChunks() { return null; } /** * @see com.lowagie.text.Element#isContent() * @since iText 2.0.8 */ public boolean isContent() { return true; } /** * @see com.lowagie.text.Element#isNestable() * @since iText 2.0.8 */ public boolean isNestable() { return false; } /** * Calculates the appropriate y position for the bottom * of the columns on this page. * * @return the y position of the bottom of the columns */ private float getColumnBottom() { if (desiredHeight == AUTOMATIC) { return pageBottom; } else { return Math.max(top - (desiredHeight - totalHeight), pageBottom); } } /** * Moves the text insertion point to the beginning of the next column, issuing a page break if * needed. * @throws DocumentException on error */ public void nextColumn() throws DocumentException { currentColumn = (currentColumn + 1) % columnDefs.size(); top = nextY; if (currentColumn == 0) { newPage(); } } /** * Gets the current column. * @return the current column */ public int getCurrentColumn() { if (columnsRightToLeft) { return (columnDefs.size() - currentColumn - 1); } return currentColumn; } /** * Resets the current column. */ public void resetCurrentColumn() { currentColumn = 0; } /** * Shifts the current column. * @return true if the current column has changed */ public boolean shiftCurrentColumn() { if (currentColumn + 1 < columnDefs.size()) { currentColumn++; return true; } return false; } /** * Sets the direction of the columns. * @param direction true = right2left; false = left2right */ public void setColumnsRightToLeft(boolean direction) { columnsRightToLeft = direction; } /** Sets the ratio between the extra word spacing and the extra character spacing * when the text is fully justified. * Extra word spacing will grow <CODE>spaceCharRatio</CODE> times more than extra character spacing. * If the ratio is <CODE>PdfWriter.NO_SPACE_CHAR_RATIO</CODE> then the extra character spacing * will be zero. * @param spaceCharRatio the ratio between the extra word spacing and the extra character spacing */ public void setSpaceCharRatio(float spaceCharRatio) { columnText.setSpaceCharRatio(spaceCharRatio); } /** Sets the run direction. * @param runDirection the run direction */ public void setRunDirection(int runDirection) { columnText.setRunDirection(runDirection); } /** Sets the arabic shaping options. The option can be AR_NOVOWEL, * AR_COMPOSEDTASHKEEL and AR_LIG. * @param arabicOptions the arabic shaping options */ public void setArabicOptions(int arabicOptions) { columnText.setArabicOptions(arabicOptions); } /** Sets the default alignment * @param alignment the default alignment */ public void setAlignment(int alignment) { columnText.setAlignment(alignment); } /** * Inner class used to define a column */ private class ColumnDef { private float[] left; private float[] right; ColumnDef(float[] newLeft, float[] newRight) { left = newLeft; right = newRight; } ColumnDef(float leftPosition, float rightPosition) { left = new float[4]; left[0] = leftPosition; // x1 left[1] = top; // y1 left[2] = leftPosition; // x2 if (desiredHeight == AUTOMATIC || top == AUTOMATIC) { left[3] = AUTOMATIC; } else { left[3] = top - desiredHeight; } right = new float[4]; right[0] = rightPosition; // x1 right[1] = top; // y1 right[2] = rightPosition; // x2 if (desiredHeight == AUTOMATIC || top == AUTOMATIC) { right[3] = AUTOMATIC; } else { right[3] = top - desiredHeight; } } /** * Resolves the positions for the specified side of the column * into real numbers once the top of the column is known. * * @param side either <CODE>Rectangle.LEFT</CODE> * or <CODE>Rectangle.RIGHT</CODE> * @return the array of floats for the side */ float[] resolvePositions(int side) { if (side == Rectangle.LEFT) { return resolvePositions(left); } else { return resolvePositions(right); } } private float[] resolvePositions(float[] positions) { if (!isSimple()) { return positions; } if (top == AUTOMATIC) { // this is bad - must be programmer error throw new RuntimeException("resolvePositions called with top=AUTOMATIC (-1). " + "Top position must be set befure lines can be resolved"); } positions[1] = top; positions[3] = getColumnBottom(); return positions; } /** * Checks if column definition is a simple rectangle * @return true if it is a simple column */ private boolean isSimple() { return (left.length == 4 && right.length == 4) && (left[0] == left[2] && right[0] == right[2]); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -