Search in sources :

Example 11 with PDAppearanceDictionary

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;
    }
}
Also used : PDFormFieldAdditionalActions(org.apache.pdfbox.pdmodel.interactive.action.PDFormFieldAdditionalActions) PDAppearanceStream(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream) PDResources(org.apache.pdfbox.pdmodel.PDResources) PDAppearanceEntry(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceEntry) Matrix(org.apache.pdfbox.util.Matrix) PDAppearanceDictionary(org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary) Point2D(java.awt.geom.Point2D) PDAnnotationWidget(org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle)

Aggregations

PDAppearanceDictionary (org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary)11 PDAppearanceEntry (org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceEntry)5 PDAppearanceStream (org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream)4 COSStream (org.apache.pdfbox.cos.COSStream)3 PDResources (org.apache.pdfbox.pdmodel.PDResources)3 PDRectangle (org.apache.pdfbox.pdmodel.common.PDRectangle)3 PDAnnotationWidget (org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget)3 COSDictionary (org.apache.pdfbox.cos.COSDictionary)2 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)2 PDPage (org.apache.pdfbox.pdmodel.PDPage)2 PDFormXObject (org.apache.pdfbox.pdmodel.graphics.form.PDFormXObject)2 PDImageXObject (org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject)2 Matrix (org.apache.pdfbox.util.Matrix)2 Point2D (java.awt.geom.Point2D)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 COSName (org.apache.pdfbox.cos.COSName)1