public static void main(String[] args) throws IOException { // Load the PDF document created by SimpleForm.java try (PDDocument document = PDDocument.load(new File("target/SimpleForm.pdf"))) { PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm(); // Get the field and the widget associated to it. // Note: there might be multiple widgets PDField field = acroForm.getField("SampleField"); PDAnnotationWidget widget = field.getWidgets().get(0); // Create the definition for a green border PDAppearanceCharacteristicsDictionary fieldAppearance = new PDAppearanceCharacteristicsDictionary(new COSDictionary()); PDColor green = new PDColor(new float[] { 0, 1, 0 }, PDDeviceRGB.INSTANCE); fieldAppearance.setBorderColour(green); // Set the information to be used by the widget which is responsible // for the visual style of the form field. widget.setAppearanceCharacteristics(fieldAppearance); document.save("target/AddBorderToField.pdf"); } } }
/** * This will retrieve the normal caption. * * @return the normal caption. */ public String getNormalCaption() { return this.getCOSObject().getString(COSName.CA); }
/** * This will retrieve the border color. * * @return the border color. */ public PDColor getBorderColour() { return getColor(COSName.BC); }
= new PDAppearanceCharacteristicsDictionary(new COSDictionary()); fieldAppearance1.setBorderColour(new PDColor(new float[]{0,1,0}, PDDeviceRGB.INSTANCE)); fieldAppearance1.setBackground(new PDColor(new float[]{1,1,0}, PDDeviceRGB.INSTANCE)); widget1.setAppearanceCharacteristics(fieldAppearance1); = new PDAppearanceCharacteristicsDictionary(new COSDictionary()); fieldAppearance2.setBorderColour(new PDColor(new float[]{1,0,0}, PDDeviceRGB.INSTANCE)); fieldAppearance2.setBackground(new PDColor(new float[]{0,1,0}, PDDeviceRGB.INSTANCE)); widget2.setAppearanceCharacteristics(fieldAppearance2);
widget.setPrinted(true); PDAppearanceCharacteristicsDictionary appearanceCharacteristics = new PDAppearanceCharacteristicsDictionary(new COSDictionary()); appearanceCharacteristics.setBorderColour(new PDColor(new float[]{1, 0, 0}, PDDeviceRGB.INSTANCE)); appearanceCharacteristics.setBackground(new PDColor(new float[]{1, 1, 0}, PDDeviceRGB.INSTANCE)); appearanceCharacteristics.setNormalCaption("4"); widget.setAppearanceCharacteristics(appearanceCharacteristics);
PDColor backgroundColour = appearanceCharacteristics.getBackground(); if (backgroundColour != null) PDColor borderColour = appearanceCharacteristics.getBorderColour(); if (borderColour != null)
PDAppearanceCharacteristicsDictionary appearanceCharacteristics = new PDAppearanceCharacteristicsDictionary(new COSDictionary()); appearanceCharacteristics.setNormalCaption(String.valueOf((char) style.caption)); widget.setAppearanceCharacteristics(appearanceCharacteristics);
PDColor backgroundColor = appearanceCharacteristics.getBackground(); PDColor borderColor = appearanceCharacteristics.getBorderColour(); float lineWidth = getLineWidth(widget); yesAPCS.setBorderLine(lineWidth, widget.getBorderStyle(), widget.getBorder()); yesAPCS.clip(); String normalCaption = appearanceCharacteristics.getNormalCaption(); if (normalCaption == null)
borderColorArray.add(new COSFloat((float) (226f/255f))); PDColor blue = new PDColor(borderColorArray, PDDeviceRGB.INSTANCE); fieldAppearance.setBorderColour(blue); fieldAppearance.setBackground(blue); fieldAppearance.setNormalCaption("Click Me");
/** * Returns the appearance characteristics dictionary. * * @return the appearance characteristics dictionary */ public PDAppearanceCharacteristicsDictionary getAppearanceCharacteristics() { COSBase mk = this.getCOSObject().getDictionaryObject(COSName.MK); if (mk instanceof COSDictionary) { return new PDAppearanceCharacteristicsDictionary((COSDictionary) mk); } return null; }
PDAppearanceCharacteristicsDictionary fieldAppearance = new PDAppearanceCharacteristicsDictionary(new COSDictionary()); fieldAppearance.setBorderColour(colourBlack); COSArray arr = new COSArray(); arr.add(new COSFloat(227/255f)); arr.add(new COSFloat(239/255f)); arr.add(new COSFloat(1f)); fieldAppearance.setBackground(new PDGamma(arr));
private int resolveRotation(PDAnnotationWidget widget) { PDAppearanceCharacteristicsDictionary characteristicsDictionary = widget.getAppearanceCharacteristics(); if (characteristicsDictionary != null) { // 0 is the default value if the R key doesn't exist return characteristicsDictionary.getRotation(); } return 0; }
PDAppearanceCharacteristicsDictionary fieldAppearance = new PDAppearanceCharacteristicsDictionary(new COSDictionary()); fieldAppearance.setBorderColour(colourBlack); button.getWidget().setAppearanceCharacteristics(fieldAppearance);
= new PDAppearanceCharacteristicsDictionary(new COSDictionary()); fieldAppearance.setBorderColour(new PDColor(new float[]{0,1,0}, PDDeviceRGB.INSTANCE)); fieldAppearance.setBackground(new PDColor(new float[]{1,1,0}, PDDeviceRGB.INSTANCE)); widget.setAppearanceCharacteristics(fieldAppearance);
private static PDAppearanceStream createCheckBoxAppearanceStream( final PDDocument document, PDAnnotationWidget widget, boolean on) throws IOException { PDRectangle rect = widget.getRectangle(); PDAppearanceStream onAP = new PDAppearanceStream(document); onAP.setBBox(new PDRectangle(rect.getWidth(), rect.getHeight())); try (PDAppearanceContentStream onAPCS = new PDAppearanceContentStream(onAP)) { PDAppearanceCharacteristicsDictionary appearanceCharacteristics = widget.getAppearanceCharacteristics(); PDColor backgroundColor = appearanceCharacteristics.getBackground(); PDColor borderColor = appearanceCharacteristics.getBorderColour(); float lineWidth = getLineWidth(widget); onAPCS.setBorderLine(lineWidth, widget.getBorderStyle(), widget.getBorder()); onAPCS.setNonStrokingColor(backgroundColor); float radius = Math.min(rect.getWidth() / 2, rect.getHeight() / 2); drawCircle(onAPCS, rect.getWidth() / 2, rect.getHeight() / 2, radius); onAPCS.fill(); onAPCS.setStrokingColor(borderColor); drawCircle(onAPCS, rect.getWidth() / 2, rect.getHeight() / 2, radius - lineWidth / 2); onAPCS.stroke(); if (on) { onAPCS.setNonStrokingColor(0f); drawCircle(onAPCS, rect.getWidth() / 2, rect.getHeight() / 2, (radius - lineWidth) / 2); onAPCS.fill(); } } return onAP; }
PDAppearanceCharacteristicsDictionary appearanceCharacteristics = new PDAppearanceCharacteristicsDictionary(new COSDictionary()); appearanceCharacteristics.setNormalCaption(String.valueOf((char) style.caption)); widget.setAppearanceCharacteristics(appearanceCharacteristics);
/** * Returns the appearance characteristics dictionary. * * @return the appearance characteristics dictionary */ public PDAppearanceCharacteristicsDictionary getAppearanceCharacteristics() { COSBase mk = this.getCOSObject().getDictionaryObject(COSName.MK); if (mk instanceof COSDictionary) { return new PDAppearanceCharacteristicsDictionary((COSDictionary) mk); } return null; }
private int resolveRotation(PDAnnotationWidget widget) { PDAppearanceCharacteristicsDictionary characteristicsDictionary = widget.getAppearanceCharacteristics(); if (characteristicsDictionary != null) { // 0 is the default value if the R key doesn't exist return characteristicsDictionary.getRotation(); } return 0; }
radioButton.setExportValues(options); PDAppearanceCharacteristicsDictionary appearanceCharacteristics = new PDAppearanceCharacteristicsDictionary(new COSDictionary()); appearanceCharacteristics.setBorderColour(new PDColor(new float[] { 1, 0, 0 }, PDDeviceRGB.INSTANCE)); appearanceCharacteristics.setBackground(new PDColor(new float[]{0, 1, 0.3f}, PDDeviceRGB.INSTANCE));
/** * This will retrieve the alternate caption. * * @return the alternate caption. */ public String getAlternateCaption() { return this.getCOSObject().getString(COSName.AC); }