pdfdocument.java
来自「源码包含生成 PDF 和 HTML 的类库」· Java 代码 · 共 1,590 行 · 第 1/5 页
JAVA
1,590 行
catch(DocumentException de) { // maybe this never happens, but it's better to check. throw new ExceptionConverter(de); } catch (IOException ioe) { throw new ExceptionConverter(ioe); } return true; }// [L4] DocListener interface /** * Sets the pagesize. * * @param pageSize the new pagesize * @return <CODE>true</CODE> if the page size was set */ public boolean setPageSize(Rectangle pageSize) { if (writer != null && writer.isPaused()) { return false; } nextPageSize = new Rectangle(pageSize); return true; }// [L5] DocListener interface /** margin in x direction starting from the left. Will be valid in the next page */ protected float nextMarginLeft; /** margin in x direction starting from the right. Will be valid in the next page */ protected float nextMarginRight; /** margin in y direction starting from the top. Will be valid in the next page */ protected float nextMarginTop; /** margin in y direction starting from the bottom. Will be valid in the next page */ protected float nextMarginBottom; /** * 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. */ 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 {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?