pushbuttonfield.java
来自「有关对pdf操作的代码」· Java 代码 · 共 657 行 · 第 1/2 页
JAVA
657 行
/* * Copyright 2005 by Paulo Soares. * * The contents of this file are subject to the Mozilla Public License Version 1.1 * (the "License"); you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the License. * * The Original Code is 'iText, a free JAVA-PDF library'. * * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. * All Rights Reserved. * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. * * Contributor(s): all the names of the contributors are added in the source code * where applicable. * * Alternatively, the contents of this file may be used under the terms of the * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the * provisions of LGPL are applicable instead of those above. If you wish to * allow use of your version of this file only under the terms of the LGPL * License and not to allow others to use your version of this file under * the MPL, indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by the LGPL. * If you do not delete the provisions above, a recipient may use your version * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. * * This library is free software; you can redistribute it and/or modify it * under the terms of the MPL as stated above or under the terms of the GNU * Library General Public License as published by the Free Software Foundation; * either version 2 of the License, or any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more * details. * * If you didn't download this code from the following link, you should check if * you aren't using an obsolete version: * http://www.lowagie.com/iText/ */package com.lowagie.text.pdf;import java.io.IOException;import com.lowagie.text.DocumentException;import com.lowagie.text.Image;import com.lowagie.text.Rectangle;/** * Creates a pushbutton field. It supports all the text and icon alignments. * The icon may be an image or a template. * <p> * Example usage: * <p> * <PRE> * Document document = new Document(PageSize.A4, 50, 50, 50, 50); * PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("output.pdf")); * document.open(); * PdfContentByte cb = writer.getDirectContent(); * Image img = Image.getInstance("image.png"); * PushbuttonField bt = new PushbuttonField(writer, new Rectangle(100, 100, 200, 200), "Button1"); * bt.setText("My Caption"); * bt.setFontSize(0); * bt.setImage(img); * bt.setLayout(PushbuttonField.LAYOUT_ICON_TOP_LABEL_BOTTOM); * bt.setBackgroundColor(Color.cyan); * bt.setBorderStyle(PdfBorderDictionary.STYLE_SOLID); * bt.setBorderColor(Color.red); * bt.setBorderWidth(3); * PdfFormField ff = bt.getField(); * PdfAction ac = PdfAction.createSubmitForm("http://www.submit-site.com", null, 0); * ff.setAction(ac); * writer.addAnnotation(ff); * document.close(); * </PRE> * @author Paulo Soares (psoares@consiste.pt) */public class PushbuttonField extends BaseField { /** A layout option */ public static final int LAYOUT_LABEL_ONLY = 1; /** A layout option */ public static final int LAYOUT_ICON_ONLY = 2; /** A layout option */ public static final int LAYOUT_ICON_TOP_LABEL_BOTTOM = 3; /** A layout option */ public static final int LAYOUT_LABEL_TOP_ICON_BOTTOM = 4; /** A layout option */ public static final int LAYOUT_ICON_LEFT_LABEL_RIGHT = 5; /** A layout option */ public static final int LAYOUT_LABEL_LEFT_ICON_RIGHT = 6; /** A layout option */ public static final int LAYOUT_LABEL_OVER_ICON = 7; /** An icon scaling option */ public static final int SCALE_ICON_ALWAYS = 1; /** An icon scaling option */ public static final int SCALE_ICON_NEVER = 2; /** An icon scaling option */ public static final int SCALE_ICON_IS_TOO_BIG = 3; /** An icon scaling option */ public static final int SCALE_ICON_IS_TOO_SMALL = 4; /** * Holds value of property layout. */ private int layout = LAYOUT_LABEL_ONLY; /** * Holds value of property image. */ private Image image; /** * Holds value of property template. */ private PdfTemplate template; /** * Holds value of property scaleIcon. */ private int scaleIcon = SCALE_ICON_ALWAYS; /** * Holds value of property proportionalIcon. */ private boolean proportionalIcon = true; /** * Holds value of property iconVerticalAdjustment. */ private float iconVerticalAdjustment = 0.5f; /** * Holds value of property iconHorizontalAdjustment. */ private float iconHorizontalAdjustment = 0.5f; /** * Holds value of property iconFitToBounds. */ private boolean iconFitToBounds; private PdfTemplate tp; /** * Creates a new instance of PushbuttonField * @param writer the document <CODE>PdfWriter</CODE> * @param box the field location and dimensions * @param fieldName the field name. If <CODE>null</CODE> only the widget keys * will be included in the field allowing it to be used as a kid field. */ public PushbuttonField(PdfWriter writer, Rectangle box, String fieldName) { super(writer, box, fieldName); } /** * Getter for property layout. * @return Value of property layout. */ public int getLayout() { return this.layout; } /** * Sets the icon and label layout. Possible values are <CODE>LAYOUT_LABEL_ONLY</CODE>, * <CODE>LAYOUT_ICON_ONLY</CODE>, <CODE>LAYOUT_ICON_TOP_LABEL_BOTTOM</CODE>, * <CODE>LAYOUT_LABEL_TOP_ICON_BOTTOM</CODE>, <CODE>LAYOUT_ICON_LEFT_LABEL_RIGHT</CODE>, * <CODE>LAYOUT_LABEL_LEFT_ICON_RIGHT</CODE> and <CODE>LAYOUT_LABEL_OVER_ICON</CODE>. * The default is <CODE>LAYOUT_LABEL_ONLY</CODE>. * @param layout New value of property layout. */ public void setLayout(int layout) { if (layout < LAYOUT_LABEL_ONLY || layout > LAYOUT_LABEL_OVER_ICON) throw new IllegalArgumentException("Layout out of bounds."); this.layout = layout; } /** * Getter for property image. * @return Value of property image. */ public Image getImage() { return this.image; } /** * Sets the icon as an image. * @param image the image */ public void setImage(Image image) { this.image = image; template = null; } /** * Getter for property template. * @return Value of property template. */ public PdfTemplate getTemplate() { return this.template; } /** * Sets the icon as a template. * @param template the template */ public void setTemplate(PdfTemplate template) { this.template = template; image = null; } /** * Getter for property scaleIcon. * @return Value of property scaleIcon. */ public int getScaleIcon() { return this.scaleIcon; } /** * Sets the way the icon will be scaled. Possible values are * <CODE>SCALE_ICON_ALWAYS</CODE>, <CODE>SCALE_ICON_NEVER</CODE>, * <CODE>SCALE_ICON_IS_TOO_BIG</CODE> and <CODE>SCALE_ICON_IS_TOO_SMALL</CODE>. * The default is <CODE>SCALE_ICON_ALWAYS</CODE>. * @param scaleIcon the way the icon will be scaled */ public void setScaleIcon(int scaleIcon) { if (scaleIcon < SCALE_ICON_ALWAYS || scaleIcon > SCALE_ICON_IS_TOO_SMALL) scaleIcon = SCALE_ICON_ALWAYS; this.scaleIcon = scaleIcon; } /** * Getter for property proportionalIcon. * @return Value of property proportionalIcon. */ public boolean isProportionalIcon() { return this.proportionalIcon; } /** * Sets the way the icon is scaled. If <CODE>true</CODE> the icon is scaled proportionally, * if <CODE>false</CODE> the scaling is done anamorphicaly. * @param proportionalIcon the way the icon is scaled */ public void setProportionalIcon(boolean proportionalIcon) { this.proportionalIcon = proportionalIcon; } /** * Getter for property iconVerticalAdjustment. * @return Value of property iconVerticalAdjustment. */ public float getIconVerticalAdjustment() { return this.iconVerticalAdjustment; } /** * A number between 0 and 1 indicating the fraction of leftover space to allocate at the bottom of the icon. * A value of 0 positions the icon at the bottom of the annotation rectangle. * A value of 0.5 centers it within the rectangle. The default is 0.5. * @param iconVerticalAdjustment a number between 0 and 1 indicating the fraction of leftover space to allocate at the bottom of the icon */ public void setIconVerticalAdjustment(float iconVerticalAdjustment) { if (iconVerticalAdjustment < 0) iconVerticalAdjustment = 0; else if (iconVerticalAdjustment > 1) iconVerticalAdjustment = 1; this.iconVerticalAdjustment = iconVerticalAdjustment; } /** * Getter for property iconHorizontalAdjustment. * @return Value of property iconHorizontalAdjustment. */ public float getIconHorizontalAdjustment() { return this.iconHorizontalAdjustment; } /** * A number between 0 and 1 indicating the fraction of leftover space to allocate at the left of the icon. * A value of 0 positions the icon at the left of the annotation rectangle. * A value of 0.5 centers it within the rectangle. The default is 0.5. * @param iconHorizontalAdjustment a number between 0 and 1 indicating the fraction of leftover space to allocate at the left of the icon */ public void setIconHorizontalAdjustment(float iconHorizontalAdjustment) { if (iconHorizontalAdjustment < 0) iconHorizontalAdjustment = 0; else if (iconHorizontalAdjustment > 1) iconHorizontalAdjustment = 1; this.iconHorizontalAdjustment = iconHorizontalAdjustment; } private float calculateFontSize(float w, float h) throws IOException, DocumentException { BaseFont ufont = getRealFont(); float fsize = fontSize; if (fsize == 0) { float bw = ufont.getWidthPoint(text, 1); if (bw == 0) fsize = 12; else fsize = w / bw; float nfsize = h / (1 - ufont.getFontDescriptor(BaseFont.DESCENT, 1)); fsize = Math.min(fsize, nfsize); if (fsize < 4) fsize = 4; } return fsize; } /** * Gets the button appearance. * @throws IOException on error * @throws DocumentException on error * @return the button appearance */ public PdfAppearance getAppearance() throws IOException, DocumentException { PdfAppearance app = getBorderAppearance(); Rectangle box = new Rectangle(app.getBoundingBox()); if ((text == null || text.length() == 0) && (layout == LAYOUT_LABEL_ONLY || (image == null && template == null && iconReference == null))) { return app; } if (layout == LAYOUT_ICON_ONLY && image == null && template == null && iconReference == null) return app;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?