Search in sources :

Example 1 with PDFormFieldAdditionalActions

use of com.tom_roush.pdfbox.pdmodel.interactive.action.PDFormFieldAdditionalActions in project PdfBox-Android by TomRoush.

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.w("PdfBox-Android", "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 (isValidAppearanceStream(appearance)) {
                appearanceStream = appearance.getAppearanceStream();
            } else {
                appearanceStream = prepareNormalAppearanceStream(widget);
                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;
    }
}
Also used : PDAppearanceEntry(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceEntry) PDFormFieldAdditionalActions(com.tom_roush.pdfbox.pdmodel.interactive.action.PDFormFieldAdditionalActions) PDAppearanceDictionary(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary) PDAppearanceStream(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream) PDAnnotationWidget(com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget) PDRectangle(com.tom_roush.pdfbox.pdmodel.common.PDRectangle)

Aggregations

PDRectangle (com.tom_roush.pdfbox.pdmodel.common.PDRectangle)1 PDFormFieldAdditionalActions (com.tom_roush.pdfbox.pdmodel.interactive.action.PDFormFieldAdditionalActions)1 PDAnnotationWidget (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget)1 PDAppearanceDictionary (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary)1 PDAppearanceEntry (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceEntry)1 PDAppearanceStream (com.tom_roush.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream)1