Package org.pdfbox.pdmodel.annotation.interactive

Source Code of org.pdfbox.pdmodel.annotation.interactive.PDField

/**
* Copyright (c) 2003, www.pdfbox.org
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
*    this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
*    this list of conditions and the following disclaimer in the documentation
*    and/or other materials provided with the distribution.
* 3. Neither the name of pdfbox; nor the names of its
*    contributors may be used to endorse or promote products derived from this
*    software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* http://www.pdfbox.org
*
*/
package org.pdfbox.pdmodel.annotation.interactive;

import org.pdfbox.pdmodel.annotation.PDAnnotation;

import org.pdfbox.cos.COSBase;
import org.pdfbox.cos.COSDictionary;
import org.pdfbox.cos.COSInteger;
import org.pdfbox.cos.COSName;
import org.pdfbox.cos.COSString;

import java.io.IOException;

/**
* This is the superclass for a Field element in a PDF.
* Based on the COS object model from PDFBox.
*
* @author sug
* @version $Revision: 1.1 $
*/
public abstract class PDField extends PDAnnotation
{
    /**
     * Creates a COSField from a COSDictionary, expected to be
     * a correct object definition for a field in PDF.
     *
     * @param field the PDF objet to represent as a field.
     */
    public PDField(COSDictionary field)
    {
        super( field );
    }

    /**
     * Returns the name of the field
     * @return the name of the field
     */
    public String getName()
    {
        return ((COSString)getDictionary().getDictionaryObject(COSName.getPDFName("T"))).getString();
    }

    /**
     * This will set the name of the field.
     *
     * @param name The new name for the field.
     */
    public void setName( String name )
    {
        getDictionary().setItem( COSName.getPDFName( "T" ), new COSString( name ) );
    }


    /**
     * setValue sets the fields value to a given string.
     *
     * @param value the string value
     *
     * @throws IOException If there is an error creating the appearance stream.
     */
    public abstract void setValue(String value) throws IOException;

    /**
     * sets the field to be read-only
     *
     * @param readonly The new flag for readonly.
     */
    public void setReadonly(boolean readonly)
    {
        setFfFlag(1, readonly);
    }

    /**
     *
     * @return true if the field is readonly
     */
    public boolean isReadonly()
    {
        return getFfFlag(1);
    }

    /**
     * This will get the flags for this field.
     *
     * @return flags The set of flags.
     */
    public int getFlags()
    {
        int retval = 0;
        COSInteger ff = (COSInteger)getDictionary().getDictionaryObject( COSName.getPDFName( "Ff" ) );
        if( ff != null )
        {
            retval = (int)ff.intValue();
        }
        return retval;
    }

    /**
     * This will set the flags for this field.
     *
     * @param flags The new flags.
     */
    public void setFlags( int flags )
    {
        COSInteger ff = new COSInteger( flags );
        getDictionary().setItem( COSName.getPDFName( "Ff" ), ff );
    }

    /**
     * Sets the given boolean value at bitPos in the Ff flags
     *
     * @param bitPos the bit position to set the value in
     * @param value the value the bit position should have
     */
    protected void setFfFlag(int bitPos, boolean value)
    {
        long ffval = getFlags();
        char[] bits = Long.toBinaryString(ffval).toCharArray();
        bits[bits.length-bitPos] = value ? '1' : '0';
        String s = new String(bits);
        COSInteger ff = new COSInteger(Integer.parseInt(s,2));
        getDictionary().setItem( COSName.getPDFName( "Ff" ), ff );
    }

    /**
     * Gets the boolean value from the Ff flags at the given bit
     * position.
     * @param bitPos the bitPosition to get the value from
     * @return true if the number at bitPos is '1'
     */
    protected boolean getFfFlag(int bitPos)
    {
        COSInteger ff = (COSInteger)getDictionary().getDictionaryObject( COSName.getPDFName( "Ff" ) );
        if (ff != null)
        {
            int bitMask = (int) Math.pow(2,bitPos-1);
            return (( ff.intValue() & bitMask) == bitMask);
        }
        else
        {
            return false;
        }
    }

    /**
     * This will import a fdf field from a fdf document.
     *
     * @param fdfField The fdf field to import.
     *
     * @throws IOException If there is an error importing the data for this field.
     */
    public void importFDF( COSDictionary fdfField ) throws IOException
    {
        COSBase fieldValue = fdfField.getDictionaryObject( COSName.getPDFName( "V" ) );
        int docFlags = getFlags();
        int annotFlags = getAnnotationFlags();
        if( fieldValue != null )
        {
            if( fieldValue instanceof COSString )
            {
                COSString string = (COSString)fieldValue;
                setValue( string.getString() );
            }
            else if( fieldValue instanceof COSName )
            {
                COSName name = (COSName)fieldValue;
                setValue( name.getName() );
            }
            else
            {
                throw new IOException( "Uknown field type:" + fieldValue.getClass().getName() );
            }
        }
        COSInteger ff = (COSInteger)fdfField.getDictionaryObject( COSName.getPDFName( "Ff" ) );
        if( ff != null )
        {
            setFlags( (int)ff.intValue() );
        }
        else
        {
            //these are suppose to be ignored if the Ff is set.
            COSInteger setFf = (COSInteger)fdfField.getDictionaryObject( COSName.getPDFName( "SetFf" ) );

            if( setFf != null )
            {
                int setFfInt = (int)setFf.intValue();
                docFlags = docFlags | setFfInt;
                setFlags( docFlags );
            }

            COSInteger clrFf = (COSInteger)fdfField.getDictionaryObject( COSName.getPDFName( "ClrFf" ) );
            if( clrFf != null )
            {
                //we have to clear the bits of the document fields for every bit that is
                //set in this field.
                //
                //Example:
                //docFf = 1011
                //clrFf = 1101
                //clrFfValue = 0010;
                //newValue = 1011 & 0010 which is 0010
                int clrFfValue = (int)clrFf.intValue();
                clrFfValue ^= 0xFFFFFFFF;
                docFlags = docFlags & clrFfValue;
                setFlags( docFlags );
            }
        }
        COSInteger f = (COSInteger)fdfField.getDictionaryObject( COSName.getPDFName( "F" ) );
        if( f != null )
        {
            setAnnotationFlags( (int)f.intValue() );
        }
        else
        {
            //these are suppose to be ignored if the F is set.
            COSInteger setF = (COSInteger)fdfField.getDictionaryObject( COSName.getPDFName( "SetF" ) );
            if( setF != null )
            {
                annotFlags = annotFlags | (int)setF.intValue();
                setAnnotationFlags( annotFlags );
            }

            COSInteger clrF = (COSInteger)fdfField.getDictionaryObject( COSName.getPDFName( "ClrF" ) );
            if( clrF != null )
            {
                //we have to clear the bits of the document fields for every bit that is
                //set in this field.
                //
                //Example:
                //docF = 1011
                //clrF = 1101
                //clrFValue = 0010;
                //newValue = 1011 & 0010 which is 0010
                int clrFValue = (int)clrF.intValue();
                clrFValue ^= 0xFFFFFFFFL;
                annotFlags = annotFlags & clrFValue;
                setAnnotationFlags( annotFlags );
            }
        }
    }

    /**
     * This will return a string representation of this field.
     *
     * @return A string representation of this field.
     */
    public String toString()
    {
        return "" + getDictionary().getDictionaryObject( COSName.getPDFName( "V" ) );
    }

}
TOP

Related Classes of org.pdfbox.pdmodel.annotation.interactive.PDField

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.