📄 pdfdocument.java
字号:
/** * Opens the document. * <P> * You have to open the document before you can begin to add content * to the body of the document. */ public void open() { if (!open) { super.open(); writer.open(); outlines = new ArrayList(); PdfOutline outline = new PdfOutline(); outlines.add(outline); currentOutline = outline; } try { initPage(); } catch(DocumentException de) { } } /** * Closes the document. * <B> * Once all the content has been written in the body, you have to close * the body. After that nothing can be written to the body anymore. */ public void close() { if (close) { return; } try { newPage(); newPage(); } catch(DocumentException pe) { } PdfPageEvent pageEvent = writer.getPageEvent(); if (pageEvent != null) pageEvent.onCloseDocument(writer, this); super.close(); try { writer.addLocalDestinations(localDestinations); } catch(Exception pe) { } if (outlines.size() > 1) { int objectNumber = writer.size(); int level = 0; // telling each outline what its indirect reference is for (Iterator i = outlines.iterator(); i.hasNext(); ) { PdfOutline o = (PdfOutline) i.next(); if (o.level() > level) { level = o.level(); } PdfIndirectReference reference = new PdfIndirectReference(PdfObject.DICTIONARY, objectNumber); o.setIndirectReference(reference); objectNumber++; } // setting al the navigation keys (First, Last, Next, Previous) for (int l = 0; l <= level; l++) { PdfOutline levelOutline = null; for (Iterator i = outlines.iterator(); i.hasNext(); ) { PdfOutline o = (PdfOutline) i.next(); if (o.level() == l) { if (levelOutline == null) { levelOutline = o; } else { if (o.parent().indirectReference().getNumber() == levelOutline.parent().indirectReference().getNumber()) { levelOutline.put(PdfName.NEXT, o.indirectReference()); o.put(PdfName.PREV, levelOutline.indirectReference()); } levelOutline = o; } } else if (o.level() == (l + 1)) { if (o.parent().get(PdfName.FIRST) == null) { o.parent().put(PdfName.FIRST, o.indirectReference()); } o.parent().put(PdfName.LAST, o.indirectReference()); } } } // write everything to the PdfWriter try { for (Iterator i = outlines.iterator(); i.hasNext(); ) { writer.add((PdfOutline) i.next()); } } catch(Exception pe) { System.err.println("Error: " + pe.getMessage()); } } writer.close(); } /** Adds a font to the current page. * @param name the name of the font * @param ref the indirect reference to this font */ public void addFont(PdfName name, PdfIndirectReference ref) { fontDictionary.put(name, ref); } /** Adds a <CODE>PdfPTable</CODE> to the document. * @param ptable the <CODE>PdfPTable</CODE> to be added to the document. * @param xWidth the width the <CODE>PdfPTable</CODE> occupies in the page * @throws DocumentException on error */ void addPTable(PdfPTable ptable, float xWidth) throws DocumentException { if (ptable.getHeaderRows() >= ptable.size()) return; float headerHeight = ptable.getHeaderHeight(); float bottom = indentBottom(); float baseY = indentTop() - currentHeight; float currentY = baseY; int startRow = ptable.getHeaderRows(); int currentRow = startRow; PdfContentByte cv[] = null; float eventY = 0; int eventRow = 0; int eventHeader = 0; float absoluteWidths[] = ptable.getAbsoluteWidths(); PdfPTableEvent event = ptable.getTableEvent(); ptable.setTableEvent(null); float heights[] = new float[ptable.size()]; int heightsIdx = 0; for (currentRow = startRow; currentRow < ptable.size(); ++currentRow) { if (currentRow == startRow && currentY - ptable.getRowHeight(currentRow) - headerHeight < bottom) { if (currentHeight == 0) ++startRow; else { newPage(); startRow = currentRow; --currentRow; bottom = indentBottom(); baseY = indentTop() - currentHeight; currentY = baseY; } continue; } if (currentY - ptable.getRowHeight(currentRow) < bottom) { if (cv != null) { if (event != null) { float finalHeights[] = new float[heightsIdx + 1]; finalHeights[0] = eventY; for (int k = 0; k < heightsIdx; ++k) finalHeights[k + 1] = finalHeights[k] - heights[k]; float widths[] = new float[absoluteWidths.length + 1]; widths[0] = xWidth; for (int k = 0; k < absoluteWidths.length; ++k) widths[k + 1] = widths[k] + absoluteWidths[k]; event.tableLayout(ptable, widths, finalHeights, eventHeader, eventRow, cv); } PdfPTable.endWritingRows(cv); cv = null; } newPage(); startRow = currentRow; --currentRow; bottom = indentBottom(); baseY = indentTop() - currentHeight; currentY = baseY; } else { if (cv == null) { cv = PdfPTable.beginWritingRows(writer.getDirectContent()); if (event != null) { heightsIdx = 0; eventHeader = ptable.getHeaderRows(); for (int k = 0; k < eventHeader; ++k) heights[heightsIdx++] = ptable.getRowHeight(k); eventY = currentY; eventRow = currentRow; } currentY = ptable.writeSelectedRows(0, ptable.getHeaderRows(), xWidth, currentY, cv); } if (event != null) { heights[heightsIdx++] = ptable.getRowHeight(currentRow); } currentY = ptable.writeSelectedRows(currentRow, currentRow + 1, xWidth, currentY, cv); } } if (cv != null) { if (event != null) { float finalHeights[] = new float[heightsIdx + 1]; finalHeights[0] = eventY; for (int k = 0; k < heightsIdx; ++k) finalHeights[k + 1] = finalHeights[k] - heights[k]; float widths[] = new float[absoluteWidths.length + 1]; widths[0] = xWidth; for (int k = 0; k < absoluteWidths.length; ++k) widths[k + 1] = widths[k] + absoluteWidths[k]; event.tableLayout(ptable, widths, finalHeights, eventHeader, eventRow, cv); } PdfPTable.endWritingRows(cv); text.moveText(0, currentY - baseY); currentHeight = indentTop() - currentY; } ptable.setTableEvent(event); } /** * Signals that an <CODE>Element</CODE> was added to the <CODE>Document</CODE>. * * @param element the element to add * @return <CODE>true</CODE> if the element was added, <CODE>false</CODE> if not. * @throws DocumentException when a document isn't open yet, or has been closed */ public boolean add(Element element) throws DocumentException { if (writer != null && writer.isPaused()) { return false; } try { switch(element.type()) { // Information (headers) case Element.HEADER: // other headers than those below are not supported return false; case Element.TITLE: info.addTitle(((Meta)element).content()); break; case Element.SUBJECT: info.addSubject(((Meta)element).content()); break; case Element.KEYWORDS: info.addKeywords(((Meta)element).content()); break; case Element.AUTHOR: info.addAuthor(((Meta)element).content()); break; case Element.CREATOR: info.addCreator(((Meta)element).content()); break; case Element.PRODUCER: // you can not change the name of the producer info.addProducer(); break; case Element.CREATIONDATE: // you can not set the creation date, only reset it info.addCreationDate(); break; // content (text) case Element.CHUNK: { // if there isn't a current line available, we make one if (line == null) { carriageReturn(); } // we cast the element to a chunk PdfChunk chunk = new PdfChunk((Chunk) element, currentAction); // we try to add the chunk to the line, until we succeed { PdfChunk overflow; while ((overflow = line.add(chunk)) != null) { carriageReturn(); chunk = overflow; } } pageEmpty = false; if (chunk.isAttribute(Chunk.NEWPAGE)) { newPage(); } break; } case Element.ANCHOR: { Anchor anchor = (Anchor) element; String url = anchor.reference(); leading = anchor.leading(); if (url != null) { currentAction = new PdfAction(url); } // we process the element element.process(this); currentAction = null; break; } case Element.ANNOTATION: { if (line == null) { carriageReturn(); } Annotation annot = (Annotation) element; annotations.add(new PdfAnnotation(indentRight() - line.widthLeft(), indentTop() - currentHeight, indentRight() - line.widthLeft() + 20, indentTop() - currentHeight - 20, new PdfString(annot.title()), new PdfString(annot.content()))); pageEmpty = false; break; } case Element.PHRASE: { // we cast the element to a phrase and set the leading of the document leading = ((Phrase) element).leading(); // we process the element element.process(this); break; } case Element.PARAGRAPH: { // we cast the element to a paragraph Paragraph paragraph = (Paragraph) element; //add by Jin-Hsia Yang isParagraphE = true; paraIndent = paragraph.indentationLeft(); //end add by Jin-Hsia Yang // we adjust the parameters of the document alignment = paragraph.alignment(); indentLeft += paragraph.indentationLeft(); indentRight += paragraph.indentationRight(); leading = paragraph.leading(); carriageReturn(); // we don't want to make orphans/widows if (currentHeight + line.height() + leading > indentTop() - indentBottom()) { newPage(); } PdfPageEvent pageEvent = writer.getPageEvent(); if (pageEvent != null && isParagraph) pageEvent.onParagraph(writer, this, indentTop() - currentHeight); // we process the paragraph element.process(this); // if the last line is justified, it should be aligned to the left // if (line.hasToBeJustified()) { // line.resetAlignment(); // }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -