Class AppearanceGeneratorHelper
- java.lang.Object
-
- org.apache.pdfbox.pdmodel.interactive.form.AppearanceGeneratorHelper
-
class AppearanceGeneratorHelper extends java.lang.ObjectCreate the AcroForms field appearance helper.
-
-
Field Summary
Fields Modifier and Type Field Description private static OperatorBMCprivate static floatDEFAULT_FONT_SIZEThe default font size used for multiline textprivate static floatDEFAULT_PADDINGThe default padding applied by Acrobat to the fields bbox.private PDDefaultAppearanceStringdefaultAppearanceprivate static OperatorEMCprivate PDVariableTextfieldprivate static intFONTSCALEThe scaling factor for font units to PDF unitsprivate static float[]HIGHLIGHT_COLORThe highlight color The color setting is used by Adobe to display the highlight box for selected entries in a list box.private static org.apache.commons.logging.LogLOGprivate static floatMAXIMUM_FONT_SIZEprivate static floatMINIMUM_FONT_SIZEThe minimum/maximum font sizes used for multiline text auto sizingprivate java.lang.Stringvalue
-
Constructor Summary
Constructors Constructor Description AppearanceGeneratorHelper(PDVariableText field)Constructs a COSAppearance from the given field.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private PDRectangleapplyPadding(PDRectangle box, float padding)Apply padding to a box.private floatcalculateFontSize(PDFont font, PDRectangle contentRect)My "not so great" method for calculating the fontsize.private java.awt.geom.AffineTransformcalculateMatrix(PDRectangle bbox, int rotation)private java.lang.StringgetFormattedValue(java.lang.String apValue)private intgetTextAlign(PDAnnotationWidget widget)private PDDefaultAppearanceStringgetWidgetDefaultAppearanceString(PDAnnotationWidget widget)private voidinitializeAppearanceContent(PDAnnotationWidget widget, PDAppearanceCharacteristicsDictionary appearanceCharacteristics, PDAppearanceStream appearanceStream)Initialize the content of the appearance stream.private voidinsertGeneratedAppearance(PDAnnotationWidget widget, PDAppearanceStream appearanceStream, java.io.OutputStream output)Generate and insert text content and clipping around it.private voidinsertGeneratedCombAppearance(PDAppearanceContentStream contents, PDAppearanceStream appearanceStream, PDFont font, float fontSize)Generate the appearance for comb fields.private voidinsertGeneratedListboxAppearance(PDAppearanceContentStream contents, PDAppearanceStream appearanceStream, PDRectangle contentRect, PDFont font, float fontSize)private voidinsertGeneratedListboxSelectionHighlight(PDAppearanceContentStream contents, PDAppearanceStream appearanceStream, PDFont font, float fontSize)private booleanisMultiLine()private static booleanisValidAppearanceStream(PDAppearanceEntry appearance)private PDAppearanceStreamprepareNormalAppearanceStream(PDAnnotationWidget widget)private PDRectangleresolveBoundingBox(PDAnnotationWidget fieldWidget, PDAppearanceStream appearanceStream)Resolve the bounding box.private floatresolveCapHeight(PDFont font)private floatresolveDescent(PDFont font)private floatresolveGlyphHeight(PDFont font, int code)private intresolveRotation(PDAnnotationWidget widget)private voidsetAppearanceContent(PDAnnotationWidget widget, PDAppearanceStream appearanceStream)Constructs and sets new contents for given appearance stream.voidsetAppearanceValue(java.lang.String apValue)This is the public method for setting the appearance stream.private booleanshallComb()Determine if the appearance shall provide a comb output.private voidvalidateAndEnsureAcroFormResources()private voidwriteToStream(byte[] data, PDAppearanceStream appearanceStream)Writes the stream to the actual stream in the COSStream.
-
-
-
Field Detail
-
LOG
private static final org.apache.commons.logging.Log LOG
-
BMC
private static final Operator BMC
-
EMC
private static final Operator EMC
-
field
private final PDVariableText field
-
defaultAppearance
private PDDefaultAppearanceString defaultAppearance
-
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
-
MAXIMUM_FONT_SIZE
private static final float MAXIMUM_FONT_SIZE
- 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.IOExceptionThis 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)
-
prepareNormalAppearanceStream
private PDAppearanceStream prepareNormalAppearanceStream(PDAnnotationWidget widget)
-
getWidgetDefaultAppearanceString
private PDDefaultAppearanceString getWidgetDefaultAppearanceString(PDAnnotationWidget widget) throws java.io.IOException
- Throws:
java.io.IOException
-
resolveRotation
private int resolveRotation(PDAnnotationWidget widget)
-
initializeAppearanceContent
private void initializeAppearanceContent(PDAnnotationWidget widget, PDAppearanceCharacteristicsDictionary appearanceCharacteristics, 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 widgetappearanceCharacteristics- the appearance characteristics dictionary from the widget or nullappearanceStream- the appearance stream to be used- Throws:
java.io.IOException- in case we can't write to the appearance stream
-
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
-
getTextAlign
private int getTextAlign(PDAnnotationWidget widget)
-
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(PDAppearanceContentStream contents, PDAppearanceStream appearanceStream, PDFont font, float fontSize) throws java.io.IOException
Generate the appearance for comb fields.- Parameters:
contents- the content stream to write toappearanceStream- the appearance stream usedfont- the font to be usedfontSize- the font size to be used- Throws:
java.io.IOException
-
insertGeneratedListboxSelectionHighlight
private void insertGeneratedListboxSelectionHighlight(PDAppearanceContentStream contents, PDAppearanceStream appearanceStream, PDFont font, float fontSize) throws java.io.IOException
- Throws:
java.io.IOException
-
insertGeneratedListboxAppearance
private void insertGeneratedListboxAppearance(PDAppearanceContentStream contents, PDAppearanceStream appearanceStream, PDRectangle contentRect, PDFont font, float fontSize) throws java.io.IOException
- Throws:
java.io.IOException
-
writeToStream
private void writeToStream(byte[] data, PDAppearanceStream appearanceStream) throws java.io.IOExceptionWrites 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.
-
-