Class AppearanceGeneratorHelper


  • class AppearanceGeneratorHelper
    extends java.lang.Object
    Create the AcroForms field appearance helper.
    • Field Detail

      • LOG

        private static final org.apache.commons.logging.Log LOG
      • BMC

        private static final Operator BMC
      • EMC

        private static final Operator EMC
      • value

        private java.lang.String value
      • HIGHLIGHT_COLOR

        private static final float[] HIGHLIGHT_COLOR
        The highlight color The color setting is used by Adobe to display the highlight box for selected entries in a list box. Regardless of other settings in an existing appearance stream Adobe will always use this value.
      • FONTSCALE

        private static final int FONTSCALE
        The scaling factor for font units to PDF units
        See Also:
        Constant Field Values
      • DEFAULT_FONT_SIZE

        private static final float DEFAULT_FONT_SIZE
        The default font size used for multiline text
        See Also:
        Constant Field Values
      • MINIMUM_FONT_SIZE

        private static final float MINIMUM_FONT_SIZE
        The minimum/maximum font sizes used for multiline text auto sizing
        See Also:
        Constant Field Values
      • DEFAULT_PADDING

        private static final float DEFAULT_PADDING
        The default padding applied by Acrobat to the fields bbox.
        See Also:
        Constant Field Values
    • Constructor Detail

      • AppearanceGeneratorHelper

        AppearanceGeneratorHelper​(PDVariableText field)
                           throws java.io.IOException
        Constructs a COSAppearance from the given field.
        Parameters:
        field - the field which you wish to control the appearance of
        Throws:
        java.io.IOException
    • Method Detail

      • validateAndEnsureAcroFormResources

        private void validateAndEnsureAcroFormResources()
      • setAppearanceValue

        public void setAppearanceValue​(java.lang.String apValue)
                                throws java.io.IOException
        This is the public method for setting the appearance stream.
        Parameters:
        apValue - the String value which the appearance should represent
        Throws:
        java.io.IOException - If there is an error creating the stream.
      • getFormattedValue

        private java.lang.String getFormattedValue​(java.lang.String apValue)
      • isValidAppearanceStream

        private static boolean isValidAppearanceStream​(PDAppearanceEntry appearance)
      • initializeAppearanceContent

        private void initializeAppearanceContent​(PDAnnotationWidget widget,
                                                 PDAppearanceStream appearanceStream)
                                          throws java.io.IOException
        Initialize the content of the appearance stream. Get settings like border style, border width and colors to be used to draw a rectangle and background color around the widget
        Parameters:
        widget - the field widget
        appearanceStream - the appearance stream to be used
        Throws:
        java.io.IOException - in case we can't write to the appearance stream
      • tokenize

        private java.util.List<java.lang.Object> tokenize​(PDAppearanceStream appearanceStream)
                                                   throws java.io.IOException
        Parses an appearance stream into tokens.
        Throws:
        java.io.IOException
      • setAppearanceContent

        private void setAppearanceContent​(PDAnnotationWidget widget,
                                          PDAppearanceStream appearanceStream)
                                   throws java.io.IOException
        Constructs and sets new contents for given appearance stream.
        Throws:
        java.io.IOException
      • insertGeneratedAppearance

        private void insertGeneratedAppearance​(PDAnnotationWidget widget,
                                               PDAppearanceStream appearanceStream,
                                               java.io.OutputStream output)
                                        throws java.io.IOException
        Generate and insert text content and clipping around it.
        Throws:
        java.io.IOException
      • calculateMatrix

        private java.awt.geom.AffineTransform calculateMatrix​(PDRectangle bbox,
                                                              int rotation)
      • isMultiLine

        private boolean isMultiLine()
      • shallComb

        private boolean shallComb()
        Determine if the appearance shall provide a comb output.

        May be set only if the MaxLen entry is present in the text field dictionary and if the Multiline, Password, and FileSelect flags are clear. If set, the field shall be automatically divided into as many equally spaced positions, or combs, as the value of MaxLen, and the text is laid out into those combs.

        Returns:
        the comb state
      • insertGeneratedCombAppearance

        private void insertGeneratedCombAppearance​(PDPageContentStream contents,
                                                   PDAppearanceStream appearanceStream,
                                                   PDFont font,
                                                   float fontSize)
                                            throws java.io.IOException
        Generate the appearance for comb fields.
        Parameters:
        contents - the content stream to write to
        appearanceStream - the appearance stream used
        font - the font to be used
        fontSize - the font size to be used
        Throws:
        java.io.IOException
      • insertGeneratedListboxSelectionHighlight

        private void insertGeneratedListboxSelectionHighlight​(PDPageContentStream contents,
                                                              PDAppearanceStream appearanceStream,
                                                              PDFont font,
                                                              float fontSize)
                                                       throws java.io.IOException
        Throws:
        java.io.IOException
      • writeToStream

        private void writeToStream​(byte[] data,
                                   PDAppearanceStream appearanceStream)
                            throws java.io.IOException
        Writes the stream to the actual stream in the COSStream.
        Throws:
        java.io.IOException - If there is an error writing to the stream
      • calculateFontSize

        private float calculateFontSize​(PDFont font,
                                        PDRectangle contentRect)
                                 throws java.io.IOException
        My "not so great" method for calculating the fontsize. It does not work superb, but it handles ok.
        Returns:
        the calculated font-size
        Throws:
        java.io.IOException - If there is an error getting the font information.
      • resolveCapHeight

        private float resolveCapHeight​(PDFont font)
                                throws java.io.IOException
        Throws:
        java.io.IOException
      • resolveDescent

        private float resolveDescent​(PDFont font)
                              throws java.io.IOException
        Throws:
        java.io.IOException
      • resolveGlyphHeight

        private float resolveGlyphHeight​(PDFont font,
                                         int code)
                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • resolveBoundingBox

        private PDRectangle resolveBoundingBox​(PDAnnotationWidget fieldWidget,
                                               PDAppearanceStream appearanceStream)
        Resolve the bounding box.
        Parameters:
        fieldWidget - the annotation widget.
        appearanceStream - the annotations appearance stream.
        Returns:
        the resolved boundingBox.
      • applyPadding

        private PDRectangle applyPadding​(PDRectangle box,
                                         float padding)
        Apply padding to a box.
        Parameters:
        box - box
        Returns:
        the padded box.