pdfdocument.java
来自「有关对pdf操作的代码」· Java 代码 · 共 1,599 行 · 第 1/5 页
JAVA
1,599 行
/** * Sets the margins. * * @param marginLeft the margin on the left * @param marginRight the margin on the right * @param marginTop the margin on the top * @param marginBottom the margin on the bottom * @return a <CODE>boolean</CODE> */ public boolean setMargins(float marginLeft, float marginRight, float marginTop, float marginBottom) { if (writer != null && writer.isPaused()) { return false; } nextMarginLeft = marginLeft; nextMarginRight = marginRight; nextMarginTop = marginTop; nextMarginBottom = marginBottom; return true; }// [L6] DocListener interface /** * @see com.lowagie.text.DocListener#setMarginMirroring(boolean) */ public boolean setMarginMirroring(boolean MarginMirroring) { if (writer != null && writer.isPaused()) { return false; } return super.setMarginMirroring(MarginMirroring); }// [L7] DocListener interface /** * Sets the page number. * * @param pageN the new page number */ public void setPageCount(int pageN) { if (writer != null && writer.isPaused()) { return; } super.setPageCount(pageN); }// [L8] DocListener interface /** * Sets the page number to 0. */ public void resetPageCount() { if (writer != null && writer.isPaused()) { return; } super.resetPageCount(); }// [L9] DocListener interface /** * Changes the header of this document. * * @param header the new header */ public void setHeader(HeaderFooter header) { if (writer != null && writer.isPaused()) { return; } super.setHeader(header); }// [L10] DocListener interface /** * Resets the header of this document. */ public void resetHeader() { if (writer != null && writer.isPaused()) { return; } super.resetHeader(); }// [L11] DocListener interface /** * Changes the footer of this document. * * @param footer the new footer */ public void setFooter(HeaderFooter footer) { if (writer != null && writer.isPaused()) { return; } super.setFooter(footer); }// [L12] DocListener interface /** * Resets the footer of this document. */ public void resetFooter() { if (writer != null && writer.isPaused()) { return; } super.resetFooter(); } // DOCLISTENER METHODS END /** Signals that OnOpenDocument should be called. */ protected boolean firstPageEvent = true; /** * Initializes a page. * <P> * If the footer/header is set, it is printed. * @throws DocumentException on error */ protected void initPage() throws DocumentException { // the pagenumber is incremented pageN++; // initialization of some page objects annotationsImp.resetAnnotations(); pageResources = new PageResources(); writer.resetContent(); graphics = new PdfContentByte(writer); text = new PdfContentByte(writer); text.reset(); text.beginText(); textEmptySize = text.size(); markPoint = 0; setNewPageSizeAndMargins(); imageEnd = -1; indentation.imageIndentRight = 0; indentation.imageIndentLeft = 0; indentation.indentBottom = 0; indentation.indentTop = 0; currentHeight = 0; // backgroundcolors, etc... thisBoxSize = new HashMap(boxSize); if (pageSize.getBackgroundColor() != null || pageSize.hasBorders() || pageSize.getBorderColor() != null) { add(pageSize); } float oldleading = leading; int oldAlignment = alignment; // if there is a footer, the footer is added doFooter(); // we move to the left/top position of the page text.moveText(left(), top()); doHeader(); pageEmpty = true; // if there is an image waiting to be drawn, draw it try { if (imageWait != null) { add(imageWait); imageWait = null; } } catch(Exception e) { throw new ExceptionConverter(e); } leading = oldleading; alignment = oldAlignment; carriageReturn(); PdfPageEvent pageEvent = writer.getPageEvent(); if (pageEvent != null) { if (firstPageEvent) { pageEvent.onOpenDocument(writer, this); } pageEvent.onStartPage(writer, this); } firstPageEvent = false; } /** The line that is currently being written. */ protected PdfLine line = null; /** The lines that are written until now. */ protected ArrayList lines = new ArrayList(); /** * Adds the current line to the list of lines and also adds an empty line. * @throws DocumentException on error */ protected void newLine() throws DocumentException { lastElementType = -1; carriageReturn(); if (lines != null && !lines.isEmpty()) { lines.add(line); currentHeight += line.height(); } line = new PdfLine(indentLeft(), indentRight(), alignment, leading); } /** * If the current line is not empty or null, it is added to the arraylist * of lines and a new empty line is added. * @throws DocumentException on error */ protected void carriageReturn() { // the arraylist with lines may not be null if (lines == null) { lines = new ArrayList(); } // If the current line is not null if (line != null) { // we check if the end of the page is reached (bugfix by Francois Gravel) if (currentHeight + line.height() + leading < indentTop() - indentBottom()) { // if so nonempty lines are added and the height is augmented if (line.size() > 0) { currentHeight += line.height(); lines.add(line); pageEmpty = false; } } // if the end of the line is reached, we start a new page else { newPage(); } } if (imageEnd > -1 && currentHeight > imageEnd) { imageEnd = -1; indentation.imageIndentRight = 0; indentation.imageIndentLeft = 0; } // a new current line is constructed line = new PdfLine(indentLeft(), indentRight(), alignment, leading); } /** * Gets the current vertical page position. * @param ensureNewLine Tells whether a new line shall be enforced. This may cause side effects * for elements that do not terminate the lines they've started because those lines will get * terminated. * @return The current vertical page position. */ public float getVerticalPosition(boolean ensureNewLine) { // ensuring that a new line has been started. if (ensureNewLine) { ensureNewLine(); } return top() - currentHeight - indentation.indentTop; } /** Holds the type of the last element, that has been added to the document. */ protected int lastElementType = -1; /** * Ensures that a new line has been started. */ protected void ensureNewLine() { try { if ((lastElementType == Element.PHRASE) || (lastElementType == Element.CHUNK)) { newLine(); flushLines(); } } catch (DocumentException ex) { throw new ExceptionConverter(ex); } } /** * Writes all the lines to the text-object. * * @return the displacement that was caused * @throws DocumentException on error */ protected float flushLines() throws DocumentException { // checks if the ArrayList with the lines is not null if (lines == null) { return 0; } // checks if a new Line has to be made. if (line != null && line.size() > 0) { lines.add(line); line = new PdfLine(indentLeft(), indentRight(), alignment, leading); } // checks if the ArrayList with the lines is empty if (lines.isEmpty()) { return 0; } // initialization of some parameters Object currentValues[] = new Object[2]; PdfFont currentFont = null; float displacement = 0; PdfLine l; Float lastBaseFactor = new Float(0); currentValues[1] = lastBaseFactor; // looping over all the lines for (Iterator i = lines.iterator(); i.hasNext(); ) { // this is a line in the loop l = (PdfLine) i.next(); float moveTextX = l.indentLeft() - indentLeft() + indentation.indentLeft + indentation.listIndentLeft + indentation.sectionIndentLeft; text.moveText(moveTextX, -l.height()); // is the line preceded by a symbol? if (l.listSymbol() != null) { ColumnText.showTextAligned(graphics, Element.ALIGN_LEFT, new Phrase(l.listSymbol()), text.getXTLM() - l.listIndent(), text.getYTLM(), 0); } currentValues[0] = currentFont; writeLineToContent(l, text, graphics, currentValues, writer.getSpaceCharRatio()); currentFont = (PdfFont)currentValues[0]; displacement += l.height();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?