Class AppearanceGeneratorHelper
- java.lang.Object
-
- org.apache.pdfbox.pdmodel.interactive.form.AppearanceGeneratorHelper
-
class AppearanceGeneratorHelper extends java.lang.Object
Create the AcroForms field appearance helper.
-
-
Field Summary
Fields Modifier and Type Field Description private static Operator
BMC
private static float
DEFAULT_FONT_SIZE
The default font size used for multiline textprivate static float
DEFAULT_PADDING
The default padding applied by Acrobat to the fields bbox.private PDDefaultAppearanceString
defaultAppearance
private static Operator
EMC
private PDVariableText
field
private static int
FONTSCALE
The scaling factor for font units to PDF unitsprivate static 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.private static org.apache.commons.logging.Log
LOG
private static float
MAXIMUM_FONT_SIZE
private static float
MINIMUM_FONT_SIZE
The minimum/maximum font sizes used for multiline text auto sizingprivate java.lang.String
value
-
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 PDRectangle
applyPadding(PDRectangle box, float padding)
Apply padding to a box.private float
calculateFontSize(PDFont font, PDRectangle contentRect)
My "not so great" method for calculating the fontsize.private java.awt.geom.AffineTransform
calculateMatrix(PDRectangle bbox, int rotation)
private java.lang.String
getFormattedValue(java.lang.String apValue)
private int
getTextAlign(PDAnnotationWidget widget)
private PDDefaultAppearanceString
getWidgetDefaultAppearanceString(PDAnnotationWidget widget)
private void
initializeAppearanceContent(PDAnnotationWidget widget, PDAppearanceStream appearanceStream)
Initialize the content of the appearance stream.private void
insertGeneratedAppearance(PDAnnotationWidget widget, PDAppearanceStream appearanceStream, java.io.OutputStream output)
Generate and insert text content and clipping around it.private void
insertGeneratedCombAppearance(PDPageContentStream contents, PDAppearanceStream appearanceStream, PDFont font, float fontSize)
Generate the appearance for comb fields.private void
insertGeneratedListboxAppearance(PDPageContentStream contents, PDAppearanceStream appearanceStream, PDRectangle contentRect, PDFont font, float fontSize)
private void
insertGeneratedListboxSelectionHighlight(PDPageContentStream contents, PDAppearanceStream appearanceStream, PDFont font, float fontSize)
private boolean
isMultiLine()
private static boolean
isValidAppearanceStream(PDAppearanceEntry appearance)
private PDAppearanceStream
prepareNormalAppearanceStream(PDAnnotationWidget widget)
private PDRectangle
resolveBoundingBox(PDAnnotationWidget fieldWidget, PDAppearanceStream appearanceStream)
Resolve the bounding box.private float
resolveCapHeight(PDFont font)
private float
resolveDescent(PDFont font)
private float
resolveGlyphHeight(PDFont font, int code)
private int
resolveRotation(PDAnnotationWidget widget)
private void
setAppearanceContent(PDAnnotationWidget widget, PDAppearanceStream appearanceStream)
Constructs and sets new contents for given appearance stream.void
setAppearanceValue(java.lang.String apValue)
This is the public method for setting the appearance stream.private boolean
shallComb()
Determine if the appearance shall provide a comb output.private java.util.List<java.lang.Object>
tokenize(PDAppearanceStream appearanceStream)
Parses an appearance stream into tokens.private void
validateAndEnsureAcroFormResources()
private void
writeToStream(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.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)
-
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, 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 widgetappearanceStream
- 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
-
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(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 toappearanceStream
- the appearance stream usedfont
- the font to be usedfontSize
- 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
-
insertGeneratedListboxAppearance
private void insertGeneratedListboxAppearance(PDPageContentStream 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.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.
-
-