use of org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary in project pdfbox by apache.
the class AppearanceGeneratorHelper method setAppearanceValue.
/**
* This is the public method for setting the appearance stream.
*
* @param apValue the String value which the appearance should represent
* @throws IOException If there is an error creating the stream.
*/
public void setAppearanceValue(String apValue) throws IOException {
value = apValue;
// see PDFBOX-3911
if (field instanceof PDTextField && !((PDTextField) field).isMultiline()) {
value = apValue.replaceAll("\\u000D\\u000A|[\\u000A\\u000B\\u000C\\u000D\\u0085\\u2028\\u2029]", " ");
}
for (PDAnnotationWidget widget : field.getWidgets()) {
// some fields have the /Da at the widget level if the
// widgets differ in layout.
PDDefaultAppearanceString acroFormAppearance = defaultAppearance;
if (widget.getCOSObject().getDictionaryObject(COSName.DA) != null) {
defaultAppearance = getWidgetDefaultAppearanceString(widget);
}
PDRectangle rect = widget.getRectangle();
if (rect == null) {
widget.getCOSObject().removeItem(COSName.AP);
LOG.warn("widget of field " + field.getFullyQualifiedName() + " has no rectangle, no appearance stream created");
continue;
}
PDFormFieldAdditionalActions actions = field.getActions();
// when it is opened. See FreedomExpressions.pdf for an example of this.
if (actions == null || actions.getF() == null || widget.getCOSObject().getDictionaryObject(COSName.AP) != null) {
PDAppearanceDictionary appearanceDict = widget.getAppearance();
if (appearanceDict == null) {
appearanceDict = new PDAppearanceDictionary();
widget.setAppearance(appearanceDict);
}
PDAppearanceEntry appearance = appearanceDict.getNormalAppearance();
// TODO support appearances other than "normal"
PDAppearanceStream appearanceStream;
if (appearance != null && appearance.isStream()) {
appearanceStream = appearance.getAppearanceStream();
} else {
appearanceStream = new PDAppearanceStream(field.getAcroForm().getDocument());
// Calculate the entries for the bounding box and the transformation matrix
// settings for the appearance stream
int rotation = resolveRotation(widget);
Matrix matrix = Matrix.getRotateInstance(Math.toRadians(rotation), 0, 0);
Point2D.Float point2D = matrix.transformPoint(rect.getWidth(), rect.getHeight());
PDRectangle bbox = new PDRectangle(Math.abs((float) point2D.getX()), Math.abs((float) point2D.getY()));
appearanceStream.setBBox(bbox);
appearanceStream.setMatrix(calculateMatrix(bbox, rotation));
appearanceStream.setFormType(1);
appearanceStream.setResources(new PDResources());
appearanceDict.setNormalAppearance(appearanceStream);
// TODO support appearances other than "normal"
}
/*
* Adobe Acrobat always recreates the complete appearance stream if there is an appearance characteristics
* entry (the widget dictionaries MK entry). In addition if there is no content yet also create the appearance
* stream from the entries.
*
*/
if (widget.getAppearanceCharacteristics() != null || appearanceStream.getContentStream().getLength() == 0) {
initializeAppearanceContent(widget, appearanceStream);
}
setAppearanceContent(widget, appearanceStream);
}
// restore the field level appearance
defaultAppearance = acroFormAppearance;
}
}
Aggregations