pdfdocument.java
来自「源码包含生成 PDF 和 HTML 的类库」· Java 代码 · 共 1,590 行 · 第 1/5 页
JAVA
1,590 行
leading = listItem.getTotalLeading(); carriageReturn(); // we prepare the current line to be able to show us the listsymbol line.setListItem(listItem); // we process the item element.process(this); addSpacing(listItem.spacingAfter(), listItem.getTotalLeading(), listItem.getFont()); // if the last line is justified, it should be aligned to the left if (line.hasToBeJustified()) { line.resetAlignment(); } // some parameters are set back to normal again carriageReturn(); indentation.listIndentLeft -= listItem.getIndentationLeft(); indentation.indentRight -= listItem.getIndentationRight(); leadingCount--; break; } case Element.RECTANGLE: { Rectangle rectangle = (Rectangle) element; graphics.rectangle(rectangle); pageEmpty = false; break; } case Element.PTABLE: { PdfPTable ptable = (PdfPTable)element; if (ptable.size() <= ptable.getHeaderRows()) break; //nothing to do // before every table, we add a new line and flush all lines ensureNewLine(); flushLines(); addPTable(ptable); pageEmpty = false; newLine(); break; } case Element.MULTI_COLUMN_TEXT: { ensureNewLine(); flushLines(); MultiColumnText multiText = (MultiColumnText) element; float height = multiText.write(writer.getDirectContent(), this, indentTop() - currentHeight); currentHeight += height; text.moveText(0, -1f* height); pageEmpty = false; break; } case Element.TABLE : { if (element instanceof SimpleTable) { PdfPTable ptable = ((SimpleTable)element).createPdfPTable(); if (ptable.size() <= ptable.getHeaderRows()) break; //nothing to do // before every table, we add a new line and flush all lines ensureNewLine(); flushLines(); addPTable(ptable); pageEmpty = false; break; } else if (element instanceof Table) { try { PdfPTable ptable = ((Table)element).createPdfPTable(); if (ptable.size() <= ptable.getHeaderRows()) break; //nothing to do // before every table, we add a new line and flush all lines ensureNewLine(); flushLines(); addPTable(ptable); pageEmpty = false; break; } catch(BadElementException bee) { // constructing the PdfTable // Before the table, add a blank line using offset or default leading float offset = ((Table)element).getOffset(); if (Float.isNaN(offset)) offset = leading; carriageReturn(); lines.add(new PdfLine(indentLeft(), indentRight(), alignment, offset)); currentHeight += offset; addPdfTable((Table)element); } } else { return false; } break; } case Element.JPEG: case Element.JPEG2000: case Element.IMGRAW: case Element.IMGTEMPLATE: { //carriageReturn(); suggestion by Marc Campforts add((Image) element); break; } case Element.YMARK: { DrawInterface zh = (DrawInterface)element; zh.draw(graphics, indentLeft(), indentBottom(), indentRight(), indentTop(), indentTop() - currentHeight - (leadingCount > 0 ? leading : 0)); pageEmpty = false; break; } case Element.MARKED: { MarkedObject mo; if (element instanceof MarkedSection) { mo = ((MarkedSection)element).getTitle(); if (mo != null) { mo.process(this); } } mo = (MarkedObject)element; mo.process(this); break; } default: return false; } lastElementType = element.type(); return true; } catch(Exception e) { throw new DocumentException(e); } }// [L1] DocListener interface /** * 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(); rootOutline = new PdfOutline(writer); currentOutline = rootOutline; } try { initPage(); } catch(DocumentException de) { throw new ExceptionConverter(de); } }// [L2] DocListener interface /** * 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 { boolean wasImage = (imageWait != null); newPage(); if (imageWait != null || wasImage) newPage(); if (annotationsImp.hasUnusedAnnotations()) throw new RuntimeException("Not all annotations could be added to the document (the document doesn't have enough pages)."); PdfPageEvent pageEvent = writer.getPageEvent(); if (pageEvent != null) pageEvent.onCloseDocument(writer, this); super.close(); writer.addLocalDestinations(localDestinations); calculateOutlineCount(); writeOutlines(); } catch(Exception e) { throw new ExceptionConverter(e); } writer.close(); }// [L3] DocListener interface protected int textEmptySize; // [C9] Metadata for the page /** XMP Metadata for the page. */ protected byte[] xmpMetadata = null; /** * Use this method to set the XMP Metadata. * @param xmpMetadata The xmpMetadata to set. */ public void setXmpMetadata(byte[] xmpMetadata) { this.xmpMetadata = xmpMetadata; } /** * Makes a new page and sends it to the <CODE>PdfWriter</CODE>. * * @return a <CODE>boolean</CODE> */ public boolean newPage() { lastElementType = -1; if (writer == null || (writer.getDirectContent().size() == 0 && writer.getDirectContentUnder().size() == 0 && (pageEmpty || writer.isPaused()))) { setNewPageSizeAndMargins(); return false; } if (!open || close) { throw new RuntimeException("The document isn't open."); } PdfPageEvent pageEvent = writer.getPageEvent(); if (pageEvent != null) pageEvent.onEndPage(writer, this); //Added to inform any listeners that we are moving to a new page (added by David Freels) super.newPage(); // the following 2 lines were added by Pelikan Stephan indentation.imageIndentLeft = 0; indentation.imageIndentRight = 0; try { // we flush the arraylist with recently written lines flushLines(); // we prepare the elements of the page dictionary // [U1] page size and rotation int rotation = pageSize.getRotation(); // [C10] if (writer.isPdfX()) { if (thisBoxSize.containsKey("art") && thisBoxSize.containsKey("trim")) throw new PdfXConformanceException("Only one of ArtBox or TrimBox can exist in the page."); if (!thisBoxSize.containsKey("art") && !thisBoxSize.containsKey("trim")) { if (thisBoxSize.containsKey("crop")) thisBoxSize.put("trim", thisBoxSize.get("crop")); else thisBoxSize.put("trim", new PdfRectangle(pageSize, pageSize.getRotation())); } } // [M1] pageResources.addDefaultColorDiff(writer.getDefaultColorspace()); if (writer.isRgbTransparencyBlending()) { PdfDictionary dcs = new PdfDictionary(); dcs.put(PdfName.CS, PdfName.DEVICERGB); pageResources.addDefaultColorDiff(dcs); } PdfDictionary resources = pageResources.getResources(); // we create the page dictionary PdfPage page = new PdfPage(new PdfRectangle(pageSize, rotation), thisBoxSize, resources, rotation); // we complete the page dictionary // [C9] if there is XMP data to add: add it if (xmpMetadata != null) { PdfStream xmp = new PdfStream(xmpMetadata); xmp.put(PdfName.TYPE, PdfName.METADATA); xmp.put(PdfName.SUBTYPE, PdfName.XML); PdfEncryption crypto = writer.getEncryption(); if (crypto != null && !crypto.isMetadataEncrypted()) { PdfArray ar = new PdfArray(); ar.add(PdfName.CRYPT); xmp.put(PdfName.FILTER, ar); } page.put(PdfName.METADATA, writer.addToBody(xmp).getIndirectReference()); } // [U3] page actions: transition, duration, additional actions if (this.transition!=null) { page.put(PdfName.TRANS, this.transition.getTransitionDictionary()); transition = null; } if (this.duration>0) { page.put(PdfName.DUR,new PdfNumber(this.duration)); duration = 0; } if (pageAA != null) { page.put(PdfName.AA, writer.addToBody(pageAA).getIndirectReference()); pageAA = null; } // [U4] we add the thumbs if (thumb != null) { page.put(PdfName.THUMB, thumb); thumb = null; } // [U8] we check if the userunit is defined if (writer.getUserunit() > 0f) { page.put(PdfName.USERUNIT, new PdfNumber(writer.getUserunit())); } // [C5] and [C8] we add the annotations if (annotationsImp.hasUnusedAnnotations()) { PdfArray array = annotationsImp.rotateAnnotations(writer, pageSize); if (array.size() != 0) page.put(PdfName.ANNOTS, array); } // [F12] we add tag info if (writer.isTagged()) page.put(PdfName.STRUCTPARENTS, new PdfNumber(writer.getCurrentPageNumber() - 1)); if (text.size() > textEmptySize) text.endText(); else text = null; writer.add(page, new PdfContents(writer.getDirectContentUnder(), graphics, text, writer.getDirectContent(), pageSize)); // we initialize the new page initPage(); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?