pdfcell.java
来自「有关对pdf操作的代码」· Java 代码 · 共 911 行 · 第 1/2 页
JAVA
911 行
} } /** * 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; if (!header) { if (aboveBottom) { lines = new ArrayList(); contentHeight = 0f; } else { size = result.size(); for (int i = 0; i < size; i++) { line = removeLine(0); difference += line.height(); } } } if (difference > 0) { Image image; for (Iterator i = images.iterator(); i.hasNext();) { image = (Image) i.next(); image.setAbsolutePosition(image.getAbsoluteX(), image.getAbsoluteY() - difference - leading); } } return result; } /** * Gets the images 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>Image</CODE>s */ public ArrayList getImages(float top, float bottom) { // if the bottom of the page is higher than the top of the cell: do nothing if (getTop() < bottom) { return new ArrayList(); } top = Math.min(getTop(), top); // initializations Image image; float height; ArrayList result = new ArrayList(); // we loop over the images for (Iterator i = images.iterator(); i.hasNext() && !header;) { image = (Image) i.next(); height = image.getAbsoluteY(); // if the currentPosition is higher than the bottom, we add the line to the result if (top - height > (bottom + cellpadding)) { image.setAbsolutePosition(image.getAbsoluteX(), top - height); result.add(image); i.remove(); } } return result; } /** * Checks if this cell belongs to the header of a <CODE>PdfTable</CODE>. * * @return <CODE>void</CODE> */ boolean isHeader() { return header; } /** * Indicates that this cell belongs to the header of a <CODE>PdfTable</CODE>. */ void setHeader() { header = true; } /** * Checks if the cell may be removed. * <P> * Headers may always be removed, even if they are drawn only partially: * they will be repeated on each following page anyway! * * @return <CODE>true</CODE> if all the lines are already drawn; <CODE>false</CODE> otherwise. */ boolean mayBeRemoved() { return (header || (lines.isEmpty() && images.isEmpty())); } /** * Returns the number of lines in the cell. * * @return a value */ public int size() { return lines.size(); } /** * Returns the total height of all the lines in the cell. * * @return a value */ private float remainingLinesHeight() { if (lines.isEmpty()) return 0; float result = 0; int size = lines.size(); PdfLine line; for (int i = 0; i < size; i++) { line = (PdfLine) lines.get(i); result += line.height(); } return result; } /** * Returns the height needed to draw the remaining text. * * @return a height */ public float remainingHeight() { float result = 0f; for (Iterator i = images.iterator(); i.hasNext();) { Image image = (Image) i.next(); result += image.getScaledHeight(); } return remainingLinesHeight() + cellspacing + 2 * cellpadding + result; } // methods to retrieve membervariables /** * Gets the leading of a cell. * * @return the leading of the lines is the cell. */ public float leading() { return leading; } /** * Gets the number of the row this cell is in.. * * @return a number */ public int rownumber() { return rownumber; } /** * Gets the rowspan of a cell. * * @return the rowspan of the cell */ public int rowspan() { return rowspan; } /** * Gets the cellspacing of a cell. * * @return a value */ public float cellspacing() { return cellspacing; } /** * Gets the cellpadding of a cell.. * * @return a value */ public float cellpadding() { return cellpadding; } /** * Processes all actions contained in the cell. * @param element an element in the cell * @param action an action that should be coupled to the cell * @param allActions */ protected void processActions(Element element, PdfAction action, ArrayList allActions) { if (element.type() == Element.ANCHOR) { String url = ((Anchor) element).getReference(); if (url != null) { action = new PdfAction(url); } } Iterator i; switch (element.type()) { case Element.PHRASE: case Element.SECTION: case Element.ANCHOR: case Element.CHAPTER: case Element.LISTITEM: case Element.PARAGRAPH: for (i = ((ArrayList) element).iterator(); i.hasNext();) { processActions((Element) i.next(), action, allActions); } break; case Element.CHUNK: allActions.add(action); break; case Element.LIST: for (i = ((List) element).getItems().iterator(); i.hasNext();) { processActions((Element) i.next(), action, allActions); } break; default: int n = element.getChunks().size(); while (n-- > 0) allActions.add(action); break; } } /** * This is the number of the group the cell is in. */ private int groupNumber; /** * Gets the number of the group this cell is in.. * * @return a number */ public int getGroupNumber() { return groupNumber; } /** * Sets the group number. * @param number */ void setGroupNumber(int number) { groupNumber = number; } /** * Gets a Rectangle that is altered to fit on the page. * * @param top the top position * @param bottom the bottom position * @return a <CODE>Rectangle</CODE> */ public Rectangle rectangle(float top, float bottom) { Rectangle tmp = new Rectangle(getLeft(), getBottom(), getRight(), getTop()); tmp.cloneNonPositionParameters(this); if (getTop() > top) { tmp.setTop(top); tmp.setBorder(border - (border & TOP)); } if (getBottom() < bottom) { tmp.setBottom(bottom); tmp.setBorder(border - (border & BOTTOM)); } return tmp; } /** * Sets the value of useAscender. * @param use use ascender height if true */ public void setUseAscender(boolean use) { useAscender = use; } /** * Gets the value of useAscender * @return useAscender */ public boolean isUseAscender() { return useAscender; } /** * Sets the value of useDescender. * @param use use descender height if true */ public void setUseDescender(boolean use) { useDescender = use; } /** * gets the value of useDescender * @return useDescender */ public boolean isUseDescender() { return useDescender; } /** * Sets the value of useBorderPadding. * @param use adjust layout for borders if true */ public void setUseBorderPadding(boolean use) { useBorderPadding = use; } /** * Gets the value of useBorderPadding. * @return useBorderPadding */ public boolean isUseBorderPadding() { return useBorderPadding; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?