Class AppearanceGeneratorHelper
java.lang.Object
org.apache.pdfbox.pdmodel.interactive.form.AppearanceGeneratorHelper
Create the AcroForms field appearance helper.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Operatorprivate static final floatThe default font size used for multiline textprivate static final floatThe default padding applied by Acrobat to the fields bbox.private PDDefaultAppearanceStringprivate static final Operatorprivate final PDVariableTextprivate static final intThe scaling factor for font units to PDF unitsprivate static final float[]The highlight color The color setting is used by Adobe to display the highlight box for selected entries in a list box.private static final org.apache.commons.logging.Logprivate static final floatprivate static final floatThe minimum/maximum font sizes used for multiline text auto sizingprivate String -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a COSAppearance from the given field. -
Method Summary
Modifier and TypeMethodDescriptionprivate 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 AffineTransformcalculateMatrix(PDRectangle bbox, int rotation) private StringgetFormattedValue(String apValue) private intgetTextAlign(PDAnnotationWidget widget) private PDDefaultAppearanceStringprivate voidinitializeAppearanceContent(PDAnnotationWidget widget, PDAppearanceCharacteristicsDictionary appearanceCharacteristics, PDAppearanceStream appearanceStream) Initialize the content of the appearance stream.private voidinsertGeneratedAppearance(PDAnnotationWidget widget, PDAppearanceStream appearanceStream, OutputStream output) Generate and insert text content and clipping around it.private voidinsertGeneratedCombAppearance(PDPageContentStream contents, PDAppearanceStream appearanceStream, PDFont font, float fontSize) Generate the appearance for comb fields.private voidinsertGeneratedListboxAppearance(PDPageContentStream contents, PDAppearanceStream appearanceStream, PDRectangle contentRect, PDFont font, float fontSize) private voidinsertGeneratedListboxSelectionHighlight(PDPageContentStream contents, PDAppearanceStream appearanceStream, PDFont font, float fontSize) private booleanprivate static booleanisValidAppearanceStream(PDAppearanceEntry appearance) private PDAppearanceStreamprivate 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(String apValue) This is the public method for setting the appearance stream.private booleanDetermine if the appearance shall provide a comb output.tokenize(PDAppearanceStream appearanceStream) Parses an appearance stream into tokens.private voidprivate voidwriteToStream(byte[] data, PDAppearanceStream appearanceStream) Writes the stream to the actual stream in the COSStream.
-
Field Details
-
LOG
private static final org.apache.commons.logging.Log LOG -
BMC
-
EMC
-
field
-
defaultAppearance
-
value
-
HIGHLIGHT_COLOR
private static final float[] HIGHLIGHT_COLORThe 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 FONTSCALEThe scaling factor for font units to PDF units- See Also:
-
DEFAULT_FONT_SIZE
private static final float DEFAULT_FONT_SIZEThe default font size used for multiline text- See Also:
-
MINIMUM_FONT_SIZE
private static final float MINIMUM_FONT_SIZEThe minimum/maximum font sizes used for multiline text auto sizing- See Also:
-
MAXIMUM_FONT_SIZE
private static final float MAXIMUM_FONT_SIZE- See Also:
-
DEFAULT_PADDING
private static final float DEFAULT_PADDINGThe default padding applied by Acrobat to the fields bbox.- See Also:
-
-
Constructor Details
-
AppearanceGeneratorHelper
AppearanceGeneratorHelper(PDVariableText field) throws IOException Constructs a COSAppearance from the given field.- Parameters:
field- the field which you wish to control the appearance of- Throws:
IOException
-
-
Method Details
-
validateAndEnsureAcroFormResources
private void validateAndEnsureAcroFormResources() -
setAppearanceValue
This is the public method for setting the appearance stream.- Parameters:
apValue- the String value which the appearance should represent- Throws:
IOException- If there is an error creating the stream.
-
getFormattedValue
-
isValidAppearanceStream
-
prepareNormalAppearanceStream
-
getWidgetDefaultAppearanceString
private PDDefaultAppearanceString getWidgetDefaultAppearanceString(PDAnnotationWidget widget) throws IOException - Throws:
IOException
-
resolveRotation
-
initializeAppearanceContent
private void initializeAppearanceContent(PDAnnotationWidget widget, PDAppearanceCharacteristicsDictionary appearanceCharacteristics, PDAppearanceStream appearanceStream) throws 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 usedappearanceCharacteristics- the appearance characteristics dictionary from the widget or null- Throws:
IOException- in case we can't write to the appearance stream
-
tokenize
Parses an appearance stream into tokens.- Throws:
IOException
-
setAppearanceContent
private void setAppearanceContent(PDAnnotationWidget widget, PDAppearanceStream appearanceStream) throws IOException Constructs and sets new contents for given appearance stream.- Throws:
IOException
-
insertGeneratedAppearance
private void insertGeneratedAppearance(PDAnnotationWidget widget, PDAppearanceStream appearanceStream, OutputStream output) throws IOException Generate and insert text content and clipping around it.- Throws:
IOException
-
getTextAlign
-
calculateMatrix
-
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 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:
IOException
-
insertGeneratedListboxSelectionHighlight
private void insertGeneratedListboxSelectionHighlight(PDPageContentStream contents, PDAppearanceStream appearanceStream, PDFont font, float fontSize) throws IOException - Throws:
IOException
-
insertGeneratedListboxAppearance
private void insertGeneratedListboxAppearance(PDPageContentStream contents, PDAppearanceStream appearanceStream, PDRectangle contentRect, PDFont font, float fontSize) throws IOException - Throws:
IOException
-
writeToStream
Writes the stream to the actual stream in the COSStream.- Throws:
IOException- If there is an error writing to the stream
-
calculateFontSize
My "not so great" method for calculating the fontsize. It does not work superb, but it handles ok.- Returns:
- the calculated font-size
- Throws:
IOException- If there is an error getting the font information.
-
resolveCapHeight
- Throws:
IOException
-
resolveDescent
- Throws:
IOException
-
resolveGlyphHeight
- Throws:
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
Apply padding to a box.- Parameters:
box- box- Returns:
- the padded box.
-