pdfstamperimp.java

来自「处理PDF」· Java 代码 · 共 1,603 行 · 第 1/5 页

JAVA
1,603
字号
        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()");    }        /**     * Reads the OCProperties dictionary from the catalog of the existing document     * and fills the documentOCG, documentOCGorder and OCGRadioGroup variables in PdfWriter.     * Note that the original OCProperties of the existing document can contain more information.     * @since	2.1.2     */    protected void readOCProperties() {    	if (!documentOCG.isEmpty()) {    		return;    	}    	PdfDictionary dict = reader.getCatalog().getAsDict(PdfName.OCPROPERTIES);    	if (dict == null) {    		return;    	}    	PdfArray ocgs = dict.getAsArray(PdfName.OCGS);    	PdfIndirectReference ref;    	PdfLayer layer;    	HashMap ocgmap = new HashMap();    	for (Iterator i = ocgs.listIterator(); i.hasNext(); ) {    		ref = (PdfIndirectReference)i.next();    		layer = new PdfLayer(null);    		layer.setRef(ref);    		layer.setOnPanel(false);			layer.merge((PdfDictionary)PdfReader.getPdfObject(ref));    		ocgmap.put(ref.toString(), layer);    	}    	PdfDictionary d = dict.getAsDict(PdfName.D);    	PdfArray off = d.getAsArray(PdfName.OFF);    	if (off != null) {    		for (Iterator i = off.listIterator(); i.hasNext(); ) {    			ref = (PdfIndirectReference)i.next();    			layer = (PdfLayer)ocgmap.get(ref.toString());    			layer.setOn(false);    		}    	}    	PdfArray order = d.getAsArray(PdfName.ORDER);    	if (order != null) {    		addOrder(null, order, ocgmap);    	}    	documentOCG.addAll(ocgmap.values());    	OCGRadioGroup = d.getAsArray(PdfName.RBGROUPS);    	OCGLocked = d.getAsArray(PdfName.LOCKED);    }        /**     * Recursive method to reconstruct the documentOCGorder variable in the writer.     * @param	parent	a parent PdfLayer (can be null)     * @param	arr		an array possibly containing children for the parent PdfLayer     * @param	ocgmap	a HashMap with indirect reference Strings as keys and PdfLayer objects as values.     * @since	2.1.2     */    private void addOrder(PdfLayer parent, PdfArray arr, Map ocgmap) {    	PdfObject obj;    	PdfLayer layer;    	for (int i = 0; i < arr.size(); i++) {    		obj = arr.getPdfObject(i);    		if (obj.isIndirect()) {    			layer = (PdfLayer)ocgmap.get(obj.toString());    			layer.setOnPanel(true);    			registerLayer(layer);    			if (parent != null) {    				parent.addChild(layer);    			}    			if (arr.size() > i + 1 && arr.getPdfObject(i + 1).isArray()) {    				i++;    				addOrder(layer, (PdfArray)arr.getPdfObject(i), ocgmap);    			}    		}    		else if (obj.isArray()) {    			ArrayList sub = ((PdfArray)obj).getArrayList();    			if (sub.isEmpty()) return;    			obj = (PdfObject)sub.get(0);    			if (obj.isString()) {    				layer = new PdfLayer(sub.get(0).toString());    				layer.setOnPanel(true);    				registerLayer(layer);    				if (parent != null) {    					parent.addChild(layer);    				}    				PdfArray array = new PdfArray();    				for (Iterator j = sub.iterator(); j.hasNext(); ) {    					array.add((PdfObject)j.next());    				}    				addOrder(layer, array, ocgmap);    			}    			else {    				addOrder(parent, (PdfArray)obj, ocgmap);    			}    		}    	}    }        /**     * Gets the PdfLayer objects in an existing document as a Map     * with the names/titles of the layers as keys.     * @return	a Map with all the PdfLayers in the document (and the name/title of the layer as 

⌨️ 快捷键说明

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