pushbuttonfield.java
来自「有关对pdf操作的代码」· Java 代码 · 共 657 行 · 第 1/2 页
JAVA
657 行
BaseFont ufont = getRealFont(); boolean borderExtra = borderStyle == PdfBorderDictionary.STYLE_BEVELED || borderStyle == PdfBorderDictionary.STYLE_INSET; float h = box.getHeight() - borderWidth * 2; float bw2 = borderWidth; if (borderExtra) { h -= borderWidth * 2; bw2 *= 2; } float offsetX = (borderExtra ? 2 * borderWidth : borderWidth); offsetX = Math.max(offsetX, 1); float offX = Math.min(bw2, offsetX); tp = null; float textX = Float.NaN; float textY = 0; float fsize = fontSize; float wt = box.getWidth() - 2 * offX - 2; float ht = box.getHeight() - 2 * offX; float adj = (iconFitToBounds ? 0 : offX + 1); int nlayout = layout; if (image == null && template == null && iconReference == null) nlayout = LAYOUT_LABEL_ONLY; Rectangle iconBox = null; while (true) { switch (nlayout) { case LAYOUT_LABEL_ONLY: case LAYOUT_LABEL_OVER_ICON: if (text != null && text.length() > 0 && wt > 0 && ht > 0) { fsize = calculateFontSize(wt, ht); textX = (box.getWidth() - ufont.getWidthPoint(text, fsize)) / 2; textY = (box.getHeight() - ufont.getFontDescriptor(BaseFont.ASCENT, fsize)) / 2; } case LAYOUT_ICON_ONLY: if (nlayout == LAYOUT_LABEL_OVER_ICON || nlayout == LAYOUT_ICON_ONLY) iconBox = new Rectangle(box.getLeft() + adj, box.getBottom() + adj, box.getRight() - adj, box.getTop() - adj); break; case LAYOUT_ICON_TOP_LABEL_BOTTOM: if (text == null || text.length() == 0 || wt <= 0 || ht <= 0) { nlayout = LAYOUT_ICON_ONLY; continue; } float nht = box.getHeight() * 0.35f - offX; if (nht > 0) fsize = calculateFontSize(wt, nht); else fsize = 4; textX = (box.getWidth() - ufont.getWidthPoint(text, fsize)) / 2; textY = offX - ufont.getFontDescriptor(BaseFont.DESCENT, fsize); iconBox = new Rectangle(box.getLeft() + adj, textY + fsize, box.getRight() - adj, box.getTop() - adj); break; case LAYOUT_LABEL_TOP_ICON_BOTTOM: if (text == null || text.length() == 0 || wt <= 0 || ht <= 0) { nlayout = LAYOUT_ICON_ONLY; continue; } nht = box.getHeight() * 0.35f - offX; if (nht > 0) fsize = calculateFontSize(wt, nht); else fsize = 4; textX = (box.getWidth() - ufont.getWidthPoint(text, fsize)) / 2; textY = box.getHeight() - offX - fsize; if (textY < offX) textY = offX; iconBox = new Rectangle(box.getLeft() + adj, box.getBottom() + adj, box.getRight() - adj, textY + ufont.getFontDescriptor(BaseFont.DESCENT, fsize)); break; case LAYOUT_LABEL_LEFT_ICON_RIGHT: if (text == null || text.length() == 0 || wt <= 0 || ht <= 0) { nlayout = LAYOUT_ICON_ONLY; continue; } float nw = box.getWidth() * 0.35f - offX; if (nw > 0) fsize = calculateFontSize(wt, nw); else fsize = 4; if (ufont.getWidthPoint(text, fsize) >= wt) { nlayout = LAYOUT_LABEL_ONLY; fsize = fontSize; continue; } textX = offX + 1; textY = (box.getHeight() - ufont.getFontDescriptor(BaseFont.ASCENT, fsize)) / 2; iconBox = new Rectangle(textX + ufont.getWidthPoint(text, fsize), box.getBottom() + adj, box.getRight() - adj, box.getTop() - adj); break; case LAYOUT_ICON_LEFT_LABEL_RIGHT: if (text == null || text.length() == 0 || wt <= 0 || ht <= 0) { nlayout = LAYOUT_ICON_ONLY; continue; } nw = box.getWidth() * 0.35f - offX; if (nw > 0) fsize = calculateFontSize(wt, nw); else fsize = 4; if (ufont.getWidthPoint(text, fsize) >= wt) { nlayout = LAYOUT_LABEL_ONLY; fsize = fontSize; continue; } textX = box.getWidth() - ufont.getWidthPoint(text, fsize) - offX - 1; textY = (box.getHeight() - ufont.getFontDescriptor(BaseFont.ASCENT, fsize)) / 2; iconBox = new Rectangle(box.getLeft() + adj, box.getBottom() + adj, textX - 1, box.getTop() - adj); break; } break; } if (textY < box.getBottom() + offX) textY = box.getBottom() + offX; if (iconBox != null && (iconBox.getWidth() <= 0 || iconBox.getHeight() <= 0)) iconBox = null; boolean haveIcon = false; float boundingBoxWidth = 0; float boundingBoxHeight = 0; PdfArray matrix = null; if (iconBox != null) { if (image != null) { tp = new PdfTemplate(writer); tp.setBoundingBox(new Rectangle(image)); writer.addDirectTemplateSimple(tp, PdfName.FRM); tp.addImage(image, image.getWidth(), 0, 0, image.getHeight(), 0, 0); haveIcon = true; boundingBoxWidth = tp.getBoundingBox().getWidth(); boundingBoxHeight = tp.getBoundingBox().getHeight(); } else if (template != null) { tp = new PdfTemplate(writer); tp.setBoundingBox(new Rectangle(template.getWidth(), template.getHeight())); writer.addDirectTemplateSimple(tp, PdfName.FRM); tp.addTemplate(template, template.getBoundingBox().getLeft(), template.getBoundingBox().getBottom()); haveIcon = true; boundingBoxWidth = tp.getBoundingBox().getWidth(); boundingBoxHeight = tp.getBoundingBox().getHeight(); } else if (iconReference != null) { PdfDictionary dic = (PdfDictionary)PdfReader.getPdfObject(iconReference); if (dic != null) { Rectangle r2 = PdfReader.getNormalizedRectangle((PdfArray)PdfReader.getPdfObject(dic.get(PdfName.BBOX))); matrix = (PdfArray)PdfReader.getPdfObject(dic.get(PdfName.MATRIX)); haveIcon = true; boundingBoxWidth = r2.getWidth(); boundingBoxHeight = r2.getHeight(); } } } if (haveIcon) { float icx = iconBox.getWidth() / boundingBoxWidth; float icy = iconBox.getHeight() / boundingBoxHeight; if (proportionalIcon) { switch (scaleIcon) { case SCALE_ICON_IS_TOO_BIG: icx = Math.min(icx, icy); icx = Math.min(icx, 1); break; case SCALE_ICON_IS_TOO_SMALL: icx = Math.min(icx, icy); icx = Math.max(icx, 1); break; case SCALE_ICON_NEVER: icx = 1; break; default: icx = Math.min(icx, icy); break; } icy = icx; } else { switch (scaleIcon) { case SCALE_ICON_IS_TOO_BIG: icx = Math.min(icx, 1); icy = Math.min(icy, 1); break; case SCALE_ICON_IS_TOO_SMALL: icx = Math.max(icx, 1); icy = Math.max(icy, 1); break; case SCALE_ICON_NEVER: icx = icy = 1; break; default: break; } } float xpos = iconBox.getLeft() + (iconBox.getWidth() - (boundingBoxWidth * icx)) * iconHorizontalAdjustment; float ypos = iconBox.getBottom() + (iconBox.getHeight() - (boundingBoxHeight * icy)) * iconVerticalAdjustment; app.saveState(); app.rectangle(iconBox.getLeft(), iconBox.getBottom(), iconBox.getWidth(), iconBox.getHeight()); app.clip(); app.newPath(); if (tp != null) app.addTemplate(tp, icx, 0, 0, icy, xpos, ypos); else { float cox = 0; float coy = 0; if (matrix != null && matrix.size() == 6) { PdfNumber nm = (PdfNumber)PdfReader.getPdfObject((PdfObject)matrix.getArrayList().get(4)); if (nm != null) cox = nm.floatValue(); nm = (PdfNumber)PdfReader.getPdfObject((PdfObject)matrix.getArrayList().get(5)); if (nm != null) coy = nm.floatValue(); } app.addTemplateReference(iconReference, PdfName.FRM, icx, 0, 0, icy, xpos - cox * icx, ypos - coy * icy); } app.restoreState(); } if (!Float.isNaN(textX)) { app.saveState(); app.rectangle(offX, offX, box.getWidth() - 2 * offX, box.getHeight() - 2 * offX); app.clip(); app.newPath(); if (textColor == null) app.resetGrayFill(); else app.setColorFill(textColor); app.beginText(); app.setFontAndSize(ufont, fsize); app.setTextMatrix(textX, textY); app.showText(text); app.endText(); app.restoreState(); } return app; } /** * Gets the pushbutton field. * @throws IOException on error * @throws DocumentException on error * @return the pushbutton field */ public PdfFormField getField() throws IOException, DocumentException { PdfFormField field = PdfFormField.createPushButton(writer); field.setWidget(box, PdfAnnotation.HIGHLIGHT_INVERT); if (fieldName != null) { field.setFieldName(fieldName); if ((options & READ_ONLY) != 0) field.setFieldFlags(PdfFormField.FF_READ_ONLY); if ((options & REQUIRED) != 0) field.setFieldFlags(PdfFormField.FF_REQUIRED); } if (text != null) field.setMKNormalCaption(text); if (rotation != 0) field.setMKRotation(rotation); field.setBorderStyle(new PdfBorderDictionary(borderWidth, borderStyle, new PdfDashPattern(3))); PdfAppearance tpa = getAppearance(); field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, tpa); PdfAppearance da = (PdfAppearance)tpa.getDuplicate(); da.setFontAndSize(getRealFont(), fontSize); if (textColor == null) da.setGrayFill(0); else da.setColorFill(textColor); field.setDefaultAppearanceString(da); if (borderColor != null) field.setMKBorderColor(borderColor); if (backgroundColor != null) field.setMKBackgroundColor(backgroundColor); switch (visibility) { case HIDDEN: field.setFlags(PdfAnnotation.FLAGS_PRINT | PdfAnnotation.FLAGS_HIDDEN); break; case VISIBLE_BUT_DOES_NOT_PRINT: break; case HIDDEN_BUT_PRINTABLE: field.setFlags(PdfAnnotation.FLAGS_PRINT | PdfAnnotation.FLAGS_NOVIEW); break; default: field.setFlags(PdfAnnotation.FLAGS_PRINT); break; } if (tp != null) field.setMKNormalIcon(tp); field.setMKTextPosition(layout - 1); PdfName scale = PdfName.A; if (scaleIcon == SCALE_ICON_IS_TOO_BIG) scale = PdfName.B; else if (scaleIcon == SCALE_ICON_IS_TOO_SMALL) scale = PdfName.S; else if (scaleIcon == SCALE_ICON_NEVER) scale = PdfName.N; field.setMKIconFit(scale, proportionalIcon ? PdfName.P : PdfName.A, iconHorizontalAdjustment, iconVerticalAdjustment, iconFitToBounds); return field; } /** * Getter for property iconFitToBounds. * @return Value of property iconFitToBounds. */ public boolean isIconFitToBounds() { return this.iconFitToBounds; } /** * If <CODE>true</CODE> the icon will be scaled to fit fully within the bounds of the annotation, * if <CODE>false</CODE> the border width will be taken into account. The default * is <CODE>false</CODE>. * @param iconFitToBounds if <CODE>true</CODE> the icon will be scaled to fit fully within the bounds of the annotation, * if <CODE>false</CODE> the border width will be taken into account */ public void setIconFitToBounds(boolean iconFitToBounds) { this.iconFitToBounds = iconFitToBounds; } /** * Holds value of property iconReference. */ private PRIndirectReference iconReference; /** * Gets the reference to an existing icon. * @return the reference to an existing icon. */ public PRIndirectReference getIconReference() { return this.iconReference; } /** * Sets the reference to an existing icon. * @param iconReference the reference to an existing icon */ public void setIconReference(PRIndirectReference iconReference) { this.iconReference = iconReference; } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?