⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pdfdocument.java

📁 一个java操作pdf文件的开发包,很好用的.
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
     */        public void open() {        if (!open) {            super.open();            writer.open();            rootOutline = new PdfOutline(writer);            currentOutline = rootOutline;        }        try {            initPage();        }        catch(DocumentException de) {            throw new ExceptionConverter(de);        }    }        void outlineTree(PdfOutline outline) throws IOException {        outline.setIndirectReference(writer.getPdfIndirectReference());        if (outline.parent() != null)            outline.put(PdfName.PARENT, outline.parent().indirectReference());        ArrayList kids = outline.getKids();        int size = kids.size();        for (int k = 0; k < size; ++k)            outlineTree((PdfOutline)kids.get(k));        for (int k = 0; k < size; ++k) {            if (k > 0)                ((PdfOutline)kids.get(k)).put(PdfName.PREV, ((PdfOutline)kids.get(k - 1)).indirectReference());            if (k < size - 1)                ((PdfOutline)kids.get(k)).put(PdfName.NEXT, ((PdfOutline)kids.get(k + 1)).indirectReference());        }        if (size > 0) {            outline.put(PdfName.FIRST, ((PdfOutline)kids.get(0)).indirectReference());            outline.put(PdfName.LAST, ((PdfOutline)kids.get(size - 1)).indirectReference());        }        for (int k = 0; k < size; ++k) {            PdfOutline kid = (PdfOutline)kids.get(k);            writer.addToBody(kid, kid.indirectReference());        }    }        void writeOutlines() throws IOException {        if (rootOutline.getKids().size() == 0)            return;        outlineTree(rootOutline);        writer.addToBody(rootOutline, rootOutline.indirectReference());    }        void traverseOutlineCount(PdfOutline outline) {        ArrayList kids = outline.getKids();        PdfOutline parent = outline.parent();        if (kids.size() == 0) {            if (parent != null) {                parent.setCount(parent.getCount() + 1);            }        }        else {            for (int k = 0; k < kids.size(); ++k) {                traverseOutlineCount((PdfOutline)kids.get(k));            }            if (parent != null) {                if (outline.isOpen()) {                    parent.setCount(outline.getCount() + parent.getCount() + 1);                }                else {                    parent.setCount(parent.getCount() + 1);                    outline.setCount(-outline.getCount());                }            }        }    }        void calculateOutlineCount() {        if (rootOutline.getKids().size() == 0)            return;        traverseOutlineCount(rootOutline);    }    /**     * 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();            if (imageWait != null) newPage();            if (annotations.size() > 0)                throw new RuntimeException(annotations.size() + " annotations had invalid placement 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();    }        /** 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);    }        public void addColor(PdfName name, PdfIndirectReference ref) {        colorDictionary.put(name, ref);    }        public PdfName addPatternToPage(PdfPatternPainter painter) {        PdfName name = writer.addSimplePattern(painter);        patternDictionary.put(name, painter.getIndirectReference());        return name;    }        public void addShadingPatternToPage(PdfShadingPattern shading) {        writer.addSimpleShadingPattern(shading);        patternDictionary.put(shading.getPatternName(), shading.getPatternReference());    }        public void addShadingToPage(PdfShading shading) {        writer.addSimpleShading(shading);        shadingDictionary.put(shading.getShadingName(), shading.getShadingReference());    }        /** 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;        boolean skipHeader = ptable.getSkipFirstHeader();        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;                    skipHeader = false;                }                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];                        event.tableLayout(ptable, ptable.getEventWidths(xWidth, eventRow, eventRow + heightsIdx - eventHeader, true), 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 && !skipHeader) {                        heightsIdx = 0;                        eventHeader = ptable.getHeaderRows();                        for (int k = 0; k < eventHeader; ++k)                            heights[heightsIdx++] = ptable.getRowHeight(k);                        eventY = currentY;                        eventRow = currentRow;                    }                    if (!skipHeader)                        currentY = ptable.writeSelectedRows(0, ptable.getHeaderRows(), xWidth, currentY, cv);                    else                        skipHeader = false;                }                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];                event.tableLayout(ptable, ptable.getEventWidths(xWidth, eventRow, eventRow + heightsIdx - eventHeader, true), 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:                    info.addkey(((Header)element).name(), ((Header)element).content());                    break;                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;                    switch(annot.annotationType()) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -