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

📄 pdfannotation.java

📁 处理PDF
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        used = true;    }        public HashMap getTemplates() {        return templates;    }        /** Getter for property form.     * @return Value of property form.     */    public boolean isForm() {        return form;    }        /** Getter for property annotation.     * @return Value of property annotation.     */    public boolean isAnnotation() {        return annotation;    }        public void setPage(int page) {        put(PdfName.P, writer.getPageReference(page));    }        public void setPage() {        put(PdfName.P, writer.getCurrentPage());    }        /** Getter for property placeInPage.     * @return Value of property placeInPage.     */    public int getPlaceInPage() {        return placeInPage;    }        /** Places the annotation in a specified page that must be greater     * or equal to the current one. With <code>PdfStamper</code> the page     * can be any. The first page is 1.     * @param placeInPage New value of property placeInPage.     */    public void setPlaceInPage(int placeInPage) {        this.placeInPage = placeInPage;    }        public void setRotate(int v) {        put(PdfName.ROTATE, new PdfNumber(v));    }        PdfDictionary getMK() {        PdfDictionary mk = (PdfDictionary)get(PdfName.MK);        if (mk == null) {            mk = new PdfDictionary();            put(PdfName.MK, mk);        }        return mk;    }        public void setMKRotation(int rotation) {        getMK().put(PdfName.R, new PdfNumber(rotation));    }        public static PdfArray getMKColor(Color color) {        PdfArray array = new PdfArray();        int type = ExtendedColor.getType(color);        switch (type) {            case ExtendedColor.TYPE_GRAY: {                array.add(new PdfNumber(((GrayColor)color).getGray()));                break;            }            case ExtendedColor.TYPE_CMYK: {                CMYKColor cmyk = (CMYKColor)color;                array.add(new PdfNumber(cmyk.getCyan()));                array.add(new PdfNumber(cmyk.getMagenta()));                array.add(new PdfNumber(cmyk.getYellow()));                array.add(new PdfNumber(cmyk.getBlack()));                break;            }            case ExtendedColor.TYPE_SEPARATION:            case ExtendedColor.TYPE_PATTERN:            case ExtendedColor.TYPE_SHADING:                throw new RuntimeException("Separations, patterns and shadings are not allowed in MK dictionary.");            default:                array.add(new PdfNumber(color.getRed() / 255f));                array.add(new PdfNumber(color.getGreen() / 255f));                array.add(new PdfNumber(color.getBlue() / 255f));        }        return array;    }        public void setMKBorderColor(Color color) {        if (color == null)            getMK().remove(PdfName.BC);        else            getMK().put(PdfName.BC, getMKColor(color));    }        public void setMKBackgroundColor(Color color) {        if (color == null)            getMK().remove(PdfName.BG);        else            getMK().put(PdfName.BG, getMKColor(color));    }        public void setMKNormalCaption(String caption) {        getMK().put(PdfName.CA, new PdfString(caption, PdfObject.TEXT_UNICODE));    }        public void setMKRolloverCaption(String caption) {        getMK().put(PdfName.RC, new PdfString(caption, PdfObject.TEXT_UNICODE));    }        public void setMKAlternateCaption(String caption) {        getMK().put(PdfName.AC, new PdfString(caption, PdfObject.TEXT_UNICODE));    }        public void setMKNormalIcon(PdfTemplate template) {        getMK().put(PdfName.I, template.getIndirectReference());    }        public void setMKRolloverIcon(PdfTemplate template) {        getMK().put(PdfName.RI, template.getIndirectReference());    }        public void setMKAlternateIcon(PdfTemplate template) {        getMK().put(PdfName.IX, template.getIndirectReference());    }        public void setMKIconFit(PdfName scale, PdfName scalingType, float leftoverLeft, float leftoverBottom, boolean fitInBounds) {        PdfDictionary dic = new PdfDictionary();        if (!scale.equals(PdfName.A))            dic.put(PdfName.SW, scale);        if (!scalingType.equals(PdfName.P))            dic.put(PdfName.S, scalingType);        if (leftoverLeft != 0.5f || leftoverBottom != 0.5f) {            PdfArray array = new PdfArray(new PdfNumber(leftoverLeft));            array.add(new PdfNumber(leftoverBottom));            dic.put(PdfName.A, array);        }        if (fitInBounds)            dic.put(PdfName.FB, PdfBoolean.PDFTRUE);        getMK().put(PdfName.IF, dic);    }        public void setMKTextPosition(int tp) {        getMK().put(PdfName.TP, new PdfNumber(tp));    }        /**     * Sets the layer this annotation belongs to.     * @param layer the layer this annotation belongs to     */        public void setLayer(PdfOCG layer) {        put(PdfName.OC, layer.getRef());    }        /**     * Sets the name of the annotation.     * With this name the annotation can be identified among     * all the annotations on a page (it has to be unique).     */    public void setName(String name) {    	put(PdfName.NM, new PdfString(name));    }        /**     * This class processes links from imported pages so that they may be active. The following example code reads a group     * of files and places them all on the output PDF, four pages in a single page, keeping the links active.     * <pre>     * String[] files = new String[] {&quot;input1.pdf&quot;, &quot;input2.pdf&quot;};     * String outputFile = &quot;output.pdf&quot;;     * int firstPage=1;     * Document document = new Document();     * PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputFile));     * document.setPageSize(PageSize.A4);     * float W = PageSize.A4.getWidth() / 2;     * float H = PageSize.A4.getHeight() / 2;     * document.open();     * PdfContentByte cb = writer.getDirectContent();     * for (int i = 0; i &lt; files.length; i++) {     *    PdfReader currentReader = new PdfReader(files[i]);     *    currentReader.consolidateNamedDestinations();     *    for (int page = 1; page &lt;= currentReader.getNumberOfPages(); page++) {     *        PdfImportedPage importedPage = writer.getImportedPage(currentReader, page);     *        float a = 0.5f;     *        float e = (page % 2 == 0) ? W : 0;     *        float f = (page % 4 == 1 || page % 4 == 2) ? H : 0;     *        ArrayList links = currentReader.getLinks(page);     *        cb.addTemplate(importedPage, a, 0, 0, a, e, f);     *        for (int j = 0; j &lt; links.size(); j++) {     *            PdfAnnotation.PdfImportedLink link = (PdfAnnotation.PdfImportedLink)links.get(j);     *            if (link.isInternal()) {     *                int dPage = link.getDestinationPage();     *                int newDestPage = (dPage-1)/4 + firstPage;     *                float ee = (dPage % 2 == 0) ? W : 0;     *                float ff = (dPage % 4 == 1 || dPage % 4 == 2) ? H : 0;     *                link.setDestinationPage(newDestPage);     *                link.transformDestination(a, 0, 0, a, ee, ff);     *            }     *            link.transformRect(a, 0, 0, a, e, f);     *            writer.addAnnotation(link.createAnnotation(writer));     *        }     *        if (page % 4 == 0)     *        document.newPage();     *    }     *    if (i &lt; files.length - 1)     *    document.newPage();     *    firstPage += (currentReader.getNumberOfPages()+3)/4;     * }     * document.close();     * </pre>     */    public static class PdfImportedLink {    	float llx, lly, urx, ury;    	HashMap parameters = new HashMap();    	PdfArray destination = null;    	int newPage=0;    	    	PdfImportedLink(PdfDictionary annotation) {    		parameters.putAll(annotation.hashMap);    		try {    			destination = (PdfArray) parameters.remove(PdfName.DEST);    		} catch (ClassCastException ex) {    			throw new IllegalArgumentException("You have to consolidate the named destinations of your reader.");    		}    		if (destination != null) {    			destination = new PdfArray(destination);    		}        	PdfArray rc = (PdfArray) parameters.remove(PdfName.RECT);        	llx = rc.getAsNumber(0).floatValue();    		lly = rc.getAsNumber(1).floatValue();        	urx = rc.getAsNumber(2).floatValue();    		ury = rc.getAsNumber(3).floatValue();    	}    	    	public boolean isInternal() {    		return destination != null;    	}    	    	public int getDestinationPage() {    		if (!isInternal()) return 0;    		    		// here destination is something like    		// [132 0 R, /XYZ, 29.3898, 731.864502, null]    		PdfIndirectReference ref = destination.getAsIndirectObject(0);    		    		PRIndirectReference pr = (PRIndirectReference) ref;    		PdfReader r = pr.getReader();    		for (int i = 1; i <= r.getNumberOfPages(); i++) {    			PRIndirectReference pp = r.getPageOrigRef(i);    			if (pp.getGeneration() == pr.getGeneration() && pp.getNumber() == pr.getNumber()) return i;    		}    		throw new IllegalArgumentException("Page not found.");    	}    	    	public void setDestinationPage(int newPage) {    		if (!isInternal()) throw new IllegalArgumentException("Cannot change destination of external link");    		this.newPage=newPage;    	}    	    	public void transformDestination(float a, float b, float c, float d, float e, float f) {    		if (!isInternal()) throw new IllegalArgumentException("Cannot change destination of external link");    		if (destination.getAsName(1).equals(PdfName.XYZ)) {    			float x = destination.getAsNumber(2).floatValue();    			float y = destination.getAsNumber(3).floatValue();        		float xx = x * a + y * c + e;        		float yy = x * b + y * d + f;        		destination.getArrayList().set(2, new PdfNumber(xx));        		destination.getArrayList().set(3, new PdfNumber(yy));    		}    	}    	    	public void transformRect(float a, float b, float c, float d, float e, float f) {    		float x = llx * a + lly * c + e;    		float y = llx * b + lly * d + f;    		llx = x;    		lly = y;    		x = urx * a + ury * c + e;    		y = urx * b + ury * d + f;    		urx = x;    		ury = y;    	}    	    	public PdfAnnotation createAnnotation(PdfWriter writer) {    		PdfAnnotation annotation = new PdfAnnotation(writer, new Rectangle(llx, lly, urx, ury));    		if (newPage != 0) {    	        PdfIndirectReference ref = writer.getPageReference(newPage);    	        destination.getArrayList().set(0, ref);    		}    		if (destination != null) annotation.put(PdfName.DEST, destination);    		annotation.hashMap.putAll(parameters);    		return annotation;    	}    }}

⌨️ 快捷键说明

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