pdfstamperimp.java

来自「有关对pdf操作的代码」· Java 代码 · 共 1,499 行 · 第 1/5 页

JAVA
1,499
字号
            }            PdfReader.killIndirect(outlineR.get(PdfName.DEST));            PdfReader.killIndirect(outlineR.get(PdfName.A));            PdfReader.killIndirect(outline);            outline = (PRIndirectReference)outlineR.get(PdfName.NEXT);        }    }    void deleteOutlines() {        PdfDictionary catalog = reader.getCatalog();        PRIndirectReference outlines = (PRIndirectReference)catalog.get(PdfName.OUTLINES);        if (outlines == null)            return;        outlineTravel(outlines);        PdfReader.killIndirect(outlines);        catalog.remove(PdfName.OUTLINES);        markUsed(catalog);    }        void setJavaScript() throws IOException {        HashMap djs = pdf.getDocumentLevelJS();        if (djs.isEmpty())            return;        PdfDictionary catalog = reader.getCatalog();        PdfDictionary names = (PdfDictionary)PdfReader.getPdfObject(catalog.get(PdfName.NAMES), catalog);        if (names == null) {            names = new PdfDictionary();            catalog.put(PdfName.NAMES, names);            markUsed(catalog);        }        markUsed(names);        PdfDictionary tree = PdfNameTree.writeTree(djs, this);        names.put(PdfName.JAVASCRIPT, addToBody(tree).getIndirectReference());    }    void addFileAttachments() throws IOException {        HashMap fs = pdf.getDocumentFileAttachment();        if (fs.isEmpty())            return;        PdfDictionary catalog = reader.getCatalog();        PdfDictionary names = (PdfDictionary)PdfReader.getPdfObject(catalog.get(PdfName.NAMES), catalog);        if (names == null) {            names = new PdfDictionary();            catalog.put(PdfName.NAMES, names);            markUsed(catalog);        }        markUsed(names);        HashMap old = PdfNameTree.readTree((PdfDictionary)PdfReader.getPdfObjectRelease(names.get(PdfName.EMBEDDEDFILES)));        for (Iterator it = fs.entrySet().iterator(); it.hasNext();) {            Map.Entry entry = (Map.Entry) it.next();            String name = (String) entry.getKey();            int k = 0;            String nn = name;            while (old.containsKey(nn)) {                ++k;                nn += " " + k;            }            old.put(nn, entry.getValue());        }        PdfDictionary tree = PdfNameTree.writeTree(old, this);        names.put(PdfName.EMBEDDEDFILES, addToBody(tree).getIndirectReference());    }    /**     * Adds or replaces the Collection Dictionary in the Catalog.     * @param	collection	the new collection dictionary.     */    void makePackage( PdfCollection collection ) {        PdfDictionary catalog = reader.getCatalog();       	catalog.put( PdfName.COLLECTION, collection );    }     void setOutlines() throws IOException {        if (newBookmarks == null)            return;        deleteOutlines();        if (newBookmarks.isEmpty())            return;        PdfDictionary catalog = reader.getCatalog();        boolean namedAsNames = (catalog.get(PdfName.DESTS) != null);        writeOutlines(catalog, namedAsNames);        markUsed(catalog);    }            /**     * Sets the viewer preferences.     * @param preferences the viewer preferences     * @see PdfWriter#setViewerPreferences(int)     */    public void setViewerPreferences(int preferences) {        useVp = true;        this.viewerPreferences.setViewerPreferences(preferences);    }        /** Adds a viewer preference     * @param key a key for a viewer preference     * @param value the value for the viewer preference     * @see PdfViewerPreferences#addViewerPreference     */    public void addViewerPreference(PdfName key, PdfObject value) {    	useVp = true;    	this.viewerPreferences.addViewerPreference(key, value);    }        /**     * Set the signature flags.     * @param f the flags. This flags are ORed with current ones     */    public void setSigFlags(int f) {        sigFlags |= f;    }        /** Always throws an <code>UnsupportedOperationException</code>.     * @param actionType ignore     * @param action ignore     * @throws PdfException ignore     * @see PdfStamper#setPageAction(PdfName, PdfAction, int)     */        public void setPageAction(PdfName actionType, PdfAction action) throws PdfException {        throw new UnsupportedOperationException("Use setPageAction(PdfName actionType, PdfAction action, int page)");    }    /**     * Sets the open and close page additional action.     * @param actionType the action type. It can be <CODE>PdfWriter.PAGE_OPEN</CODE>     * or <CODE>PdfWriter.PAGE_CLOSE</CODE>     * @param action the action to perform     * @param page the page where the action will be applied. The first page is 1     * @throws PdfException if the action type is invalid     */        void setPageAction(PdfName actionType, PdfAction action, int page) throws PdfException {        if (!actionType.equals(PAGE_OPEN) && !actionType.equals(PAGE_CLOSE))            throw new PdfException("Invalid page additional action type: " + actionType.toString());        PdfDictionary pg = reader.getPageN(page);        PdfDictionary aa = (PdfDictionary)PdfReader.getPdfObject(pg.get(PdfName.AA), pg);        if (aa == null) {            aa = new PdfDictionary();            pg.put(PdfName.AA, aa);            markUsed(pg);        }        aa.put(actionType, action);        markUsed(aa);    }    /**     * Always throws an <code>UnsupportedOperationException</code>.     * @param seconds ignore     */    public void setDuration(int seconds) {        throw new UnsupportedOperationException("Use setPageAction(PdfName actionType, PdfAction action, int page)");    }        /**     * Always throws an <code>UnsupportedOperationException</code>.     * @param transition ignore     */    public void setTransition(PdfTransition transition) {        throw new UnsupportedOperationException("Use setPageAction(PdfName actionType, PdfAction action, int page)");    }    /**     * Sets the display duration for the page (for presentations)     * @param seconds   the number of seconds to display the page. A negative value removes the entry     * @param page the page where the duration will be applied. The first page is 1     */    void setDuration(int seconds, int page) {        PdfDictionary pg = reader.getPageN(page);        if (seconds < 0)            pg.remove(PdfName.DUR);        else            pg.put(PdfName.DUR, new PdfNumber(seconds));        markUsed(pg);    }        /**     * Sets the transition for the page     * @param transition   the transition object. A <code>null</code> removes the transition     * @param page the page where the transition will be applied. The first page is 1     */    void setTransition(PdfTransition transition, int page) {        PdfDictionary pg = reader.getPageN(page);        if (transition == null)            pg.remove(PdfName.TRANS);        else            pg.put(PdfName.TRANS, transition.getTransitionDictionary());        markUsed(pg);    }    protected void markUsed(PdfObject obj) {        if (append && obj != null) {            PRIndirectReference ref = null;            if (obj.type() == PdfObject.INDIRECT)                ref = (PRIndirectReference)obj;            else                ref = obj.getIndRef();            if (ref != null)                marked.put(ref.getNumber(), 1);        }    }        protected void markUsed(int num) {        if (append)            marked.put(num, 1);    }        /**     * Getter for property append.     * @return Value of property append.     */    boolean isAppend() {        return append;    }            /** Additional-actions defining the actions to be taken in     * response to various trigger events affecting the document     * as a whole. The actions types allowed are: <CODE>DOCUMENT_CLOSE</CODE>,     * <CODE>WILL_SAVE</CODE>, <CODE>DID_SAVE</CODE>, <CODE>WILL_PRINT</CODE>     * and <CODE>DID_PRINT</CODE>.     *     * @param actionType the action type     * @param action the action to execute in response to the trigger     * @throws PdfException on invalid action type     */    public void setAdditionalAction(PdfName actionType, PdfAction action) throws PdfException {        if (!(actionType.equals(DOCUMENT_CLOSE) ||        actionType.equals(WILL_SAVE) ||        actionType.equals(DID_SAVE) ||        actionType.equals(WILL_PRINT) ||        actionType.equals(DID_PRINT))) {            throw new PdfException("Invalid additional action type: " + actionType.toString());        }        PdfDictionary aa = (PdfDictionary)PdfReader.getPdfObject(reader.getCatalog().get(PdfName.AA));        if (aa == null) {            if (action == null)                return;            aa = new PdfDictionary();            reader.getCatalog().put(PdfName.AA, aa);        }        markUsed(aa);        if (action == null)            aa.remove(actionType);        else            aa.put(actionType, action);    }    /**     * @see com.lowagie.text.pdf.PdfWriter#setOpenAction(com.lowagie.text.pdf.PdfAction)     */    public void setOpenAction(PdfAction action) {        openAction = action;    }        /**     * @see com.lowagie.text.pdf.PdfWriter#setOpenAction(java.lang.String)     */    public void setOpenAction(String name) {        throw new UnsupportedOperationException("Open actions by name are not supported.");    }        /**     * @see com.lowagie.text.pdf.PdfWriter#setThumbnail(com.lowagie.text.Image)     */    public void setThumbnail(com.lowagie.text.Image image) {        throw new UnsupportedOperationException("Use PdfStamper.setThumbnail().");    }        void setThumbnail(Image image, int page) throws PdfException, DocumentException {        PdfIndirectReference thumb = getImageReference(addDirectImageSimple(image));        reader.resetReleasePage();        PdfDictionary dic = reader.getPageN(page);        dic.put(PdfName.THUMB, thumb);        reader.resetReleasePage();    }    public PdfContentByte getDirectContentUnder() {        throw new UnsupportedOperationException("Use PdfStamper.getUnderContent() or PdfStamper.getOverContent()");    }    public PdfContentByte getDirectContent() {        throw new UnsupportedOperationException("Use PdfStamper.getUnderContent() or PdfStamper.getOverContent()");    }        static class PageStamp {                PdfDictionary pageN;        StampContent under;        StampContent over;        PageResources pageResources;        int replacePoint = 0;                PageStamp(PdfStamperImp stamper, PdfReader reader, PdfDictionary pageN) {            this.pageN = pageN;            pageResources = new PageResources();            PdfDictionary resources = (PdfDictionary)PdfReader.getPdfObject(pageN.get(PdfName.RESOURCES));            pageResources.setOriginalResources(resources, stamper.namePtr);        }    }}

⌨️ 快捷键说明

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